ResizeObserver

You’re reading the English version of this content since no translation exists yet for this locale. Help us translate this article!

The ResizeObserver interface reports changes to the dimensions of an Element's content or border box, or the bounding box of an SVGElement.

Note: The content box is the box in which content can be placed, meaning the border box minus the padding and border width. The border box encompasses the content, padding, and border. See The box model for further explanation.

ResizeObserver avoids infinite callback loops and cyclic dependencies that are often created when resizing via a callback function. It does this by only processing elements deeper in the DOM in subsequent frames. Implementations should, if they follow the specification, invoke resize events before paint and after layout.

Constructor

ResizeObserver()
Creates and returns a new ResizeObserver object.

Properties

None.

Methods

ResizeObserver.disconnect()
Unobserves all observed Element targets of a particular observer.
ResizeObserver.observe()
Initiates the observing of a specified Element.
ResizeObserver.unobserve()
Ends the observing of a specified Element.

Examples

In the resize-observer-text.html (see source) example, we use the resize observer to change the font-size of a header and paragraph as a slider’s value is changed causing the containing <div> to change width. This shows that you can respond to changes in an element’s size, even if they have nothing to do with the viewport.

We also provide a checkbox to turn the observer off and on. If it is turned off, the text will not change in response to the <div>'s width changing.

The JavaScript looks like so:

if(ResizeObserver) {
  const h1Elem = document.querySelector('h1');
  const pElem = document.querySelector('p');
  const divElem = document.querySelector('body > div');
  const slider = document.querySelector('input[type="range"]');
  const checkbox = document.querySelector('input[type="checkbox"]');

  divElem.style.width = '600px';

  slider.addEventListener('input', () => {
    divElem.style.width = slider.value + 'px';
  })

  const resizeObserver = new ResizeObserver(entries => {
    for (let entry of entries) {
      if(entry.contentBoxSize) {
        h1Elem.style.fontSize = Math.max(1.5, entry.contentBoxSize.inlineSize/200) + 'rem';
        pElem.style.fontSize = Math.max(1, entry.contentBoxSize.inlineSize/600) + 'rem';
      } else {
        h1Elem.style.fontSize = Math.max(1.5, entry.contentRect.width/200) + 'rem';
        pElem.style.fontSize = Math.max(1, entry.contentRect.width/600) + 'rem';
      }
    }
  });

  resizeObserver.observe(divElem);

  checkbox.addEventListener('change', () => {
    if(checkbox.checked) {
      resizeObserver.observe(divElem);
    } else {
      resizeObserver.unobserve(divElem);
    }
  });
}

Specifications

Specification Status Comment
Resize Observer
The definition of 'ResizeObserver' in that specification.
Editor's Draft Initial definition.

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
ResizeObserverChrome Full support 64Edge No support NoFirefox Full support 69IE No support NoOpera Full support 51Safari No support NoWebView Android Full support 64Chrome Android Full support 64Firefox Android No support NoOpera Android Full support 47Safari iOS No support NoSamsung Internet Android Full support Yes
ResizeObserver() constructorChrome Full support 64Edge No support NoFirefox Full support 69IE No support NoOpera Full support 51Safari No support NoWebView Android Full support 64Chrome Android Full support 64Firefox Android No support NoOpera Android Full support 47Safari iOS No support NoSamsung Internet Android Full support Yes
disconnectChrome Full support 64Edge No support NoFirefox Full support 69IE No support NoOpera Full support 51Safari No support NoWebView Android Full support 64Chrome Android Full support 64Firefox Android No support NoOpera Android Full support 47Safari iOS No support NoSamsung Internet Android Full support Yes
observeChrome Full support 64Edge No support NoFirefox Full support 69IE No support NoOpera Full support 51Safari No support NoWebView Android Full support 64Chrome Android Full support 64Firefox Android No support NoOpera Android Full support 47Safari iOS No support NoSamsung Internet Android Full support Yes
unobserveChrome Full support 64Edge No support NoFirefox Full support 69IE No support NoOpera Full support 51Safari No support NoWebView Android Full support 64Chrome Android Full support 64Firefox Android No support NoOpera Android Full support 47Safari iOS No support NoSamsung Internet Android Full support Yes

Legend

Full support  
Full support
No support  
No support

See also