CKTIS / JavaScript
ResizeObserver — observing element size
ResizeObserver reports element sizes even when the window does not resize. It lets a component respond to available space without repeatedly polling the DOM.
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 observer = new ResizeObserver((entries, observer) => { /* handle sizes */ });
observer.observe(element, { box: "content-box" });
observer.unobserve(element);
observer.disconnect();ResizeObserver is a browser API, not an ECMAScript feature; Node 24 does not establish its availability. The resize event is dispatched on Window when the window changes size, whereas ResizeObserver reports size changes of an observed element. Notifications are asynchronous and batched during rendering.
observe() watches content-box by default. border-box includes padding and borders, while device-pixel-content-box measures content in integer device pixels. The box option selects the trigger, not a filter on result fields.
The callback receives entries and the observer. An entry contains target, contentRect, and size arrays such as contentBoxSize; their inlineSize and blockSize depend on writing mode. contentRect.width and contentRect.height describe physical content dimensions.
Avoid callback size changes that create feedback loops: the browser can report a resize-observation loop error. CSS transforms alone do not trigger notifications. unobserve(element) stops one observation; disconnect() stops all. Check API availability and required options in target browsers.
Examples
(() => {
if (typeof ResizeObserver !== "function") {
throw new Error("ResizeObserver unavailable");
}
const box = document.createElement("div");
box.style.cssText = "all: initial; display: block; position: fixed; width: 120px; height: 40px; padding: 0; border: 0; box-sizing: content-box; writing-mode: horizontal-tb;";
document.body.append(box);
const observer = new ResizeObserver(([entry]) => {
console.log(`${entry.contentRect.width} x ${entry.contentRect.height}`);
observer.disconnect();
box.remove();
});
observer.observe(box, { box: "content-box" });
})();Result
120 x 40
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.