Element.getClientRects() is an instance method that returns a collection of DOMRect objects—one per CSS border box. Learn multiline inline text, how it compares to getBoundingClientRect(), looping rects, and five try-it labs aligned with MDN.
01
Kind
Instance method
02
Returns
DOMRectList
03
Args
None
04
Per line
Inline wraps
05
vs getBCR
Many vs union
06
Status
Baseline widely
Fundamentals
Introduction
Most elements have one border box, so one rectangle is enough. But when text wraps inside an inline element like <span>, each line gets its own border box. getClientRects() exposes every one of those boxes.
The return value is a DOMRectList you can loop with for...of or index like an array. Each DOMRect has top, left, width, and height relative to the viewport.
💡
Beginner tip
Need one overall box? Use getBoundingClientRect() instead—MDN describes it as the union of all client rects. Use getClientRects() when you need per-line or per-fragment positions.
Calling el.getClientRects() collects every CSS border box for the element and returns them as a list of DOMRect objects.
It is an instance method on Element.
Takes no parameters (MDN).
Returns a DOMRectList (array-like collection).
Multiline inline elements often return multiple rects.
display:none and non-rendered elements return an empty list.
Scrolling updates coordinates, same as getBoundingClientRect().
Foundation
📝 Syntax
General form of Element.getClientRects (MDN):
JavaScript
getClientRects()
Parameters
None.
Return value
A collection of DOMRect objects—one per CSS border box. Each rect’s top and left are relative to the viewport. MDN notes fractional pixel offsets are possible.
Common patterns
JavaScript
const rects = element.getClientRects();
console.log(rects.length);
for (const rect of rects) {
console.log(rect.top, rect.left, rect.width, rect.height);
}
// First line only:
if (rects.length > 0) {
console.log(rects[0]);
}
Cheat Sheet
⚡ Quick Reference
Goal
Code
All border boxes
el.getClientRects()
Count rects
rects.length
Loop rects
for (const r of rects)
Single union box
el.getBoundingClientRect()
Hidden element
Empty list (length === 0)
MDN status
Baseline Widely available (since July 2015)
Snapshot
🔍 At a Glance
Four facts to remember about Element.getClientRects().
DOMRectList is array-like. Use for...of or index with rects[i] to process each fragment.
📈 Practical Patterns
Union vs multiple boxes, list items, and hidden elements.
Example 3 — vs getBoundingClientRect()
Many client rects vs one union rectangle.
JavaScript
const span = document.querySelector("span");
const rects = span.getClientRects();
const union = span.getBoundingClientRect();
console.log(rects.length); // e.g. 3 lines
console.log(union.width); // spans full wrapped width
console.log(union.height); // total height of all lines
Element.getClientRects() is Baseline Widely available (MDN: across browsers since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline Widely available
Element.getClientRects()
Get every CSS border box as DOMRect objects — essential for multiline inline text and per-fragment layout.
BaselineWidely available
Google ChromeSupported · Desktop & Mobile
Yes
Microsoft EdgeSupported · Chromium
Yes
Mozilla FirefoxSupported · Desktop & Mobile
Yes
Apple SafariSupported · macOS & iOS
Yes
OperaSupported · Modern versions
Yes
Internet ExplorerSupported in legacy IE
Yes
getClientRects()Excellent
Bottom line: Use getClientRects() when you need one rectangle per line or fragment. Prefer getBoundingClientRect() when a single union box is enough.
Wrap Up
Conclusion
Element.getClientRects() returns every CSS border box for an element as a DOMRectList—the right tool when wrapped inline text needs per-line measurements.
Prefer getBoundingClientRect() for one overall box
Re-read on scroll/resize when drawing overlays
Handle empty lists for display:none elements
❌ Don’t
Assume exactly one rect for wrapped inline text
Confuse DOMRectList with a plain array (mostly fine with for...of)
Use for margin measurements (margin is outside the box)
Expect child overflow to expand parent rects (MDN)
Call excessively inside tight loops without throttling
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Element.getClientRects()
Multiple border boxes per element.
5
Core concepts
📝01
Returns
DOMRectList
API
📄02
Inline
per line
Wrap
✍️03
Union
getBCR
One box
✅04
Hidden
length 0
Empty
⚡05
Baseline
widely available
Status
❓ Frequently Asked Questions
It returns a collection of DOMRect objects—one for each CSS border box associated with the element. Each rect describes size and viewport-relative position in pixels.
No. MDN marks Element.getClientRects() as Baseline Widely available (across browsers since July 2015). It is not Deprecated, Experimental, or Non-standard.
getClientRects() returns every border box (multiple for wrapped inline text). getBoundingClientRect() returns one DOMRect that is the union of all those boxes.
MDN: inline-level elements like a span that wraps across lines get one border box per line. A block-level paragraph usually has only one.
An empty list (length 0). MDN: elements that are not directly rendered, including display:none, return no rectangles.
Yes. Coordinates are viewport-relative and change when you scroll, same as getBoundingClientRect(). Add scroll offsets for document coordinates.
Did you know?
MDN notes that most elements have one border box, but multiline inline-level elements (like a wrapping <span>) get one per line. That is when getClientRects() shines over a single union rectangle.