CKTIS / JavaScript
ResizeObserverEntry: observed element and content rectangle
Use `target` to identify which element an entry describes and `contentRect` to read its reported dimensions, excluding padding and borders for an HTML element.
Version: Current web platform; runtime support varies
Syntax
entry.target
entry.contentRect
entry.contentRect.width
entry.contentRect.heightResizeObserverEntry.target/contentRect pairs the observed element with its reported geometry. During rendering, the browser passes an array of entries to the ResizeObserver callback. The read-only entry.target property refers to the actual observed element, not a copy. If you observe several elements, compare target with a known element reference using ===.
entry.contentRect is a DOMRectReadOnly snapshot from the notification, not a live measurement. For an HTML element, width and height are the physical content-box dimensions in CSS pixels, excluding padding and borders. For SVG graphics without a CSS layout box, the measurement describes the SVG bounding box. Do not confuse it with getBoundingClientRect(), which for HTML reports a rectangle enclosing the border boxes relative to the viewport.
contentRect is retained for backward compatibility. If you need logical dimensions, use contentBoxSize where supported: its inlineSize and blockSize values follow the writing mode. The box option of observe() selects which box changes trigger notifications; setting border-box does not turn contentRect into a border-box measurement. ECMAScript 2024 is only the JavaScript language baseline. The Resize Observer specification, not ECMAScript, defines both properties, and their availability depends on the runtime.
Examples
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>ResizeObserverEntry example</title>
</head>
<body>
<script>
(() => {
const box = document.createElement('div');
Object.assign(box.style, {
boxSizing: 'content-box',
width: '120px',
height: '60px',
padding: '10px',
border: '2px solid black'
});
document.body.append(box);
const observer = new ResizeObserver(([entry]) => {
const { width, height } = entry.contentRect;
console.log(`${entry.target === box}: ${width} × ${height} CSS px`);
observer.disconnect();
box.remove();
});
observer.observe(box);
})();
</script>
</body>
</html>Result
true: 120 × 60 CSS px
Save the example as an `.html` file and open it in a browser that supports `ResizeObserver`. Open the developer console and reload the page. The initial notification logs `true: 120 × 60 CSS px`: `true` confirms the element identity, and the dimensions exclude padding and borders. The snippet then stops observing and removes the element.
Related lessons
- JavaScript · Lesson 32 · Intermediate · Version: 3