Skip to content

Resize Observer

In one line

The Resize Observer API calls a callback when an element's content rectangle changes size, without polling.

In simple words

The Resize Observer API watches elements and fires a callback whenever their size changes — width, height, or both. Unlike the window resize event (which only fires for the whole viewport), the Resize Observer tracks individual elements, including size changes caused by layout shifts, content changes, or container queries.

Before this API, detecting element resize required polling with setInterval and getBoundingClientRect, which was wasteful and inaccurate. The observer is efficient: the browser only fires when the size actually changes and batches notifications. You get the new border-box and content-box sizes in the callback.

Use cases: responsive components that must adapt to their container (not just the viewport), re-rendering a canvas when its CSS size changes, re-measuring for virtualized lists, and building container-query-like behavior before native CQ support.

Example

javascript
const box = document.querySelector(".resizable");
const ro = new ResizeObserver((entries) => {
  for (const entry of entries) {
    const { width, height } = entry.contentRect;
    console.log(`Box is now ${width}x${height}`);
  }
});
ro.observe(box);

When .resizable changes size (from CSS, content, or user drag), the callback fires with the new contentRect dimensions.

Related terms

Related Resources

Related Lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary