Skip to content
Web Design & Development

Resize and Mutation Observers, Done Properly

Browser APIs that notify code when an element changes size or when the DOM changes, replacing polling and window resize handlers.

MediaScaleUp Editorial Team 2 min read 9 views
Resize and Mutation Observers, Done Properly

Why does it matter?

They let components respond to real changes efficiently, which matters for responsive components and embedded widgets.

What are the numbers?

  • ResizeObserver reports changes to an element's size
  • MutationObserver reports changes to the DOM tree or attributes
  • Advantage avoids polling and repeated layout queries
  • Browser support all major browsers

What should I do?

  1. Use ResizeObserver for component-level responsiveness
  2. Prefer container queries when CSS can handle layout
  3. Keep observer callbacks light
  4. Disconnect observers when components unmount
  5. Avoid loops where callbacks trigger more changes
What to do and what to avoid with resize and mutation observers, side by side
Good practice against the usual mistakes, from the sources listed below.

What should I avoid?

Avoid:

  • Polling for size changes
  • Heavy work in observer callbacks
  • Observers left running after removal
  • Infinite resize loops

When should I get help?

Short answer Bring in help when building embeddable widgets or complex responsive components.

Where this comes from

The figures and practices above come from the sources listed.

Working on something like this?

We take on Web Design & Development work for teams who want it done once, properly. Tell us what you are building and we will tell you honestly whether we are the right studio for it. Start a project.

Where to go next

Spotted something wrong? Report an error on this page. We correct on the page and say what changed.

All services

The work behind this article, and what it costs.

MediaScaleUp Editorial Team

Articles here are researched, written and checked by the MediaScaleUp studio team before they are published, and dated when they are reviewed.

Keep reading

More in Web Design & Development