Element: getClientRects() method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

The getClientRects() method of the Element interface returns a collection of DOMRect objects that indicate the bounding rectangles for each CSS border box in a client.

Most elements only have one border box each, but a multiline inline-level element (such as a multiline element, by default) has a border box around each line.

Syntax

js
getClientRects()

Parameters

None.

Return value

The returned value is a collection of DOMRect objects, one for each CSS border box associated with the element. Each DOMRect object describes the border box, in pixels, with the top-left relative to the top-left of the viewport. For tables with captions, the caption is included even though it's outside the border box of the table. When called on SVG elements other than an outer-, the "viewport" that the resulting rectangles are relative to is the viewport that the element's outer- establishes (and to be clear, the rectangles are also transformed by the outer-'s viewBox transform, if any).

The amount of scrolling that has been done of the viewport area (or any other scrollable element) is taken into account when computing the rectangles.

The returned rectangles do not include the bounds of any child elements that might happen to overflow.

For HTML elements, SVG elements that do not render anything themselves, display:none elements, and generally any elements that are not directly rendered, an empty list is returned.

Rectangles are returned even for CSS boxes that have empty border-boxes. The left, top, right, and bottom coordinates can still be meaningful.

Fractional pixel offsets are possible.

Examples

These examples draw client rects in various colors. Note that the JavaScript function that paints the client rects is connected to the markup via the class withClientRectsOverlay.

HTML

Example 1: This HTML creates three paragraphs with a inside, each embedded in a

block. Client rects are painted for the paragraph in the second block, and for the element in the third block.

html

A paragraph with a span inside

Both the span and the paragraph have a border set. The client rects are in red. Note that the p has only one border box, while the span has multiple border boxes.

Original

Paragraph that spans multiple lines

p's rect

Paragraph that spans multiple lines

span's rect

Paragraph that spans multiple lines

Example 2: This HTML creates three ordered lists. Client rects are painted for the

    in the second block, and for each
  1. element in the third block.

    html

    A list

    Note that the border box doesn't include the number, so neither do the client rects.

    Original
    1. Item 1
    2. Item 2
    ol's rect
    1. Item 1
    2. Item 2
    each li's rect
    1. Item 1
    2. Item 2

    Example 3: This HTML creates two tables with captions. Client rects are painted for the

    in the second block.

    html

    A table with a caption

    Although the table's border box doesn't include the caption, the client rects do include the caption.

    Original
    caption
    thead
    tbody
table's rect
caption
thead
tbody