Element.getBoundingClientRect() is an instance method that returns a DOMRect describing an element’s size and position relative to the viewport. Learn top, left, width, height, how scrolling changes values, document coordinates, and five try-it labs aligned with MDN.
01
Kind
Instance method
02
Returns
DOMRect
03
Args
None
04
Box
Border box
05
Relative to
Viewport
06
Status
Baseline widely
Fundamentals
Introduction
When you build tooltips, sticky headers, drag-and-drop UIs, or scroll-spy navigation, you often need to know where an element sits on screen and how big it is. getBoundingClientRect() answers both questions in one call.
It returns a DOMRect with pixel values for top, left, right, bottom, x, y, width, and height. Positions are relative to the viewport, not the whole document.
💡
Beginner tip
MDN: the rectangle includes padding and border (the CSS border box). If the page scrolls, top and left change because they are viewport-relative. Add window.scrollY and window.scrollX for document coordinates.
Calling el.getBoundingClientRect() measures the smallest rectangle that contains the entire element’s border box and returns it as a DOMRect.
It is an instance method on Element.
Takes no parameters (MDN).
Returns a DOMRect with size and viewport-relative position.
width / height include padding and border-width.
Scrolling updates top, left, right, bottom.
Related: getClientRects() for per-line boxes (MDN).
Foundation
📝 Syntax
General form of Element.getBoundingClientRect (MDN):
JavaScript
getBoundingClientRect()
Parameters
None.
Return value
A DOMRect object. Its left, top, right, bottom, x, and y are distances from the viewport edges in pixels. width and height describe the border box size.
Read width, height, top, and left from a styled box (MDN basic example).
Example 1 — Basic Size and Position
MDN: width and height include padding and border.
JavaScript
const box = document.querySelector(".demo-box");
const rect = box.getBoundingClientRect();
console.log(rect.width); // includes padding + border
console.log(rect.height);
console.log(rect.top); // distance from viewport top
console.log(rect.left); // distance from viewport left
The returned rectangle is the element’s border box. Exact top and left depend on layout and viewport size.
Example 2 — Values Change When You Scroll
MDN scrolling demo: top decreases as you scroll down.
JavaScript
const box = document.getElementById("example");
const rect = box.getBoundingClientRect();
console.log(rect.top);
// Smaller number as you scroll down the page
Element.getBoundingClientRect() 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.getBoundingClientRect()
Measure element size and viewport position with DOMRect — essential for tooltips, scroll logic, and layout UI.
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
getBoundingClientRect()Excellent
Bottom line: Use getBoundingClientRect() when you need both size and viewport-relative position. Remember to account for scroll when converting to document coordinates.
Wrap Up
Conclusion
Element.getBoundingClientRect() returns a DOMRect with an element’s border-box size and viewport-relative position—the go-to API for layout measurements in the browser.
Re-read on scroll/resize when positioning fixed UI
Add scrollX/scrollY for document coordinates
Use center math for tooltips and popovers
Cache reads inside animation frames when possible
Pair with ResizeObserver when layout changes often
❌ Don’t
Assume top/left are document coordinates
Forget padding and border are included in width/height
Use for margin measurements (margin is outside the box)
Call excessively inside tight loops without throttling
Expect one rect for multi-line inline text (use getClientRects())
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Element.getBoundingClientRect()
Size and viewport position in one call.
5
Core concepts
📝01
Returns
DOMRect
API
📄02
Args
none
Zero
✍️03
Box
border
Model
✅04
Baseline
widely available
Status
⚡05
Scroll
viewport-relative
Coords
❓ Frequently Asked Questions
It returns a DOMRect object with the element's size and its position relative to the viewport. Properties include top, left, right, bottom, x, y, width, and height in pixels.
No. MDN marks Element.getBoundingClientRect() as Baseline Widely available (across browsers since July 2015). It is not Deprecated, Experimental, or Non-standard.
Yes. MDN states the returned rectangle is the border box, including padding and border-width (not only content). With box-sizing: border-box, width and height match the CSS width/height.
Yes. top, left, right, and bottom are relative to the viewport, so they change when the page scrolls. Add window.scrollX and window.scrollY to get document coordinates.
offsetWidth and clientWidth return numbers for size only. getBoundingClientRect() returns a full DOMRect with position and size relative to the viewport.
Use getClientRects() when an element spans multiple lines (like inline text) and you need one rectangle per fragment. getBoundingClientRect() returns the union of all those boxes.
Did you know?
MDN notes that the returned rectangle can be thought of as the union of the rectangles from getClientRects() for the element. For everyday single-box elements, getBoundingClientRect() is the simplest choice.