CKTIS / JavaScript
Element.getBoundingClientRect(): an element’s bounding rectangle
Element.getBoundingClientRect() returns a DOMRect describing an element’s viewport-relative position and size. The measurement includes padding, borders, and CSS transforms; the result is a snapshot.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
const rect = element.getBoundingClientRect();Element.getBoundingClientRect is a Web API method called as element.getBoundingClientRect(), without arguments. It returns a DOMRect: x, y, width, height, and edges top, right, bottom, left. Coordinates are viewport-relative, in CSS pixels; they can be fractional.
For an ordinary CSS element, the measurement includes padding and borders, but not margins. It accounts for transforms on the element and its ancestors. Rotation produces an axis-aligned bounding rectangle, not the rotated shape itself. getClientRects() returns the rectangles of individual fragments. If the list is nonempty and every rectangle has zero width or zero height, getBoundingClientRect() returns the first rectangle in the list. In all other cases with a nonempty list, it returns the smallest rectangle enclosing the listed rectangles except those with both zero width and zero height.
The result does not update automatically: measure again after layout changes or scrolling. Having no layout box, for example with display: none, produces zero position and dimensions. This is geometry, not a visibility or occlusion test.
The example creates an element, reads its measurement, and removes it. width is 124 and height is 64 because padding and borders are added to the content dimensions.
Examples
(() => {
const box = document.createElement('div');
box.style.cssText = 'all: initial; display: block; position: fixed; left: 20px; top: 30px; box-sizing: content-box; width: 100px; height: 40px; padding: 10px; border: 2px solid black; margin: 0;';
document.body.append(box);
try {
const rect = box.getBoundingClientRect();
console.log(JSON.stringify({
x: rect.x,
y: rect.y,
width: rect.width,
height: rect.height
}));
} finally {
box.remove();
}
})();Result
{"x":20,"y":30,"width":124,"height":64}The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.