DOM Element.getBoundingClientRect() Method
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
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.
Desktop





Tablets & Mobile





