# ResizeObserver

> The `ResizeObserver` JS object reports changes to the dimensions of elements.

## How do I detect when an element changes size in JavaScript?

The object that calls a callback when the size of the observed element's content or border box changes. Unlike the `resize` event of `window`, it tracks individual elements.

- Topic: Specials
- Type: object
- Browser support: Chrome 64+, Chrome Android 64+, Edge 79+, Firefox 69+, Firefox Android 79+, Safari 13.1+, Safari iOS 13.4+

## Example

Wrong:

```js
window.addEventListener('resize', () => {
  chart.resize(container.offsetWidth);
});
```

Right:

```js
const ro = new ResizeObserver(([entry]) => {
  chart.resize(entry.contentRect.width);
});
ro.observe(container);
```

`ResizeObserver` catches size changes from layout, not just window resizes, and gives the new size without extra reads.

## Common mistakes

- Changing the observed element's size in the callback, causing a loop error in the console.

## Key points

- The observer is stopped with `disconnect()` or `unobserve()` when the component is removed.
- Element size is read from the entry, not with `getBoundingClientRect()` inside the callback.
- Layout that depends only on the container size uses container queries, not a `ResizeObserver`.
- The window `resize` event is not used to track the size of a single element.

## Interview questions

### Why do I see 'ResizeObserver loop completed with undelivered notifications'?

The callback changed sizes that caused more resizes in the same frame. It is usually harmless, but avoid resizing the observed element in its own callback.

### ResizeObserver vs container queries: which should I use?

Use container queries when only styles change. Use `ResizeObserver` when JavaScript must react, like redrawing a canvas or chart.

## Related cards

- [IntersectionObserver](https://wonderium.dev/js/intersection-observer.md)
- [MutationObserver](https://wonderium.dev/js/mutation-observer.md)
- [@container](https://wonderium.dev/css/container.md)
- [requestAnimationFrame()](https://wonderium.dev/js/request-animation-frame.md)

---

- Level: [JavaScript](https://wonderium.dev/js.md)
- HTML version: https://wonderium.dev/js/resize-observer
- Last updated: 2026-11-25
