ResizeObserver: The Complete Guide to Watching Element Size in JavaScript and React

You build a dashboard card that shows a chart. On a wide screen it looks great. Then a user collapses the sidebar, and the chart stays the same width, spilling out of its container. You reach for the window resize event, and it does nothing, because the browser window never changed size. Only the card did.

This is the exact gap that ResizeObserver fills. It is a browser API that tells you when an individual element changes size, no matter what caused the change: a sidebar toggle, new content, a font loading late, a CSS class swap, or a user dragging a resize handle.

This guide explains how ResizeObserver works, what it reports, when it fires, and how to use it safely in plain JavaScript and in React. We cover the options most tutorials skip, the infamous "loop completed with undelivered notifications" error, performance, testing, and the situations where CSS alone is the better answer.

What Is ResizeObserver?

ResizeObserver is a web platform API that reports changes to the size of DOM elements. You give it a callback and one or more elements to watch. The browser calls your callback whenever an observed element's size changes.

According to MDN's documentation, it can report changes to the content box or border box of an element, or the bounding box of an SVG element. It is marked as and has been available across major browsers since July 2020, so you can use it in production without a polyfill for most audiences. The formal definition lives in the from the CSS Working Group.