Favorites

DOM Element.getBoundingClientRect() Method

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
Element.getBoundingClientRect()
DOM Element MethodThis method is supported by all modern browsers.
Updated 28th November 2026

The getBoundingClientRect() method returns the size of an element and its position relative to the viewport.

Description

The getBoundingClientRect() method returns a DOMRect describing the element's size and position relative to the viewport, including padding and border, and taking CSS transforms into account. Because it's relative to the viewport, the values change as the page scrolls; add window.scrollX and scrollY to get positions relative to the document.

Calling it forces the browser to calculate layout, so avoid calling it repeatedly inside loops or scroll handlers; IntersectionObserver and ResizeObserver are more efficient for watching changes.

The width and height are the rendered size, so a transform: scale(2) doubles them, unlike offsetWidth. For elements that aren't displayed, all values are 0. For inline elements that wrap across several lines, getClientRects() returns a rectangle for each line.

Syntax

element.getBoundingClientRect()

Return Value

A DOMRect with x, y, width, height, top, right, bottom and left, in pixels relative to the viewport.

Example

<div style="height:40px"></div>
<div id="box" style="width:200px;height:60px;margin-left:30px;background:#c7d2fe"></div>
const box = document.getElementById('box');
const r = box.getBoundingClientRect();
console.log(Math.round(r.width), Math.round(r.height));
console.log('top:', Math.round(r.top), 'left:', Math.round(r.left));

Browser Support

The following information will show you the current browser support for the Element.getBoundingClientRect() method. Hover over a browser icon to see the version that first introduced support for this method.

This method is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee