W3cubDocs

/Web APIs

ResizeObserverEntry

The ResizeObserverEntry interface represents the object passed to the ResizeObserver() constructor's callback function, which allows you to access the new dimensions of the Element or SVGElement being observed.

Instance properties

ResizeObserverEntry.borderBoxSize Read only

An array of objects containing the new border box size of the observed element when the callback is run.

ResizeObserverEntry.contentBoxSize Read only

An array of objects containing the new content box size of the observed element when the callback is run.

ResizeObserverEntry.devicePixelContentBoxSize Read only

An array of objects containing the new content box size in device pixels of the observed element when the callback is run.

ResizeObserverEntry.contentRect Read only

A DOMRectReadOnly object containing the new size of the observed element when the callback is run. Note that this is better supported than the above two properties, but it is left over from an earlier implementation of the Resize Observer API, is still included in the spec for web compat reasons, and may be deprecated in future versions.

ResizeObserverEntry.target Read only

A reference to the Element or SVGElement being observed.

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.

Instance methods

None.

Examples

The following snippet is taken from the resize-observer-text.html (see source) example.

Note that the code covers three different compatibility cases:

  • Some old browsers may support contentRect but not contentBoxSize.
  • Old versions of Firefox support contentBoxSize, but incorrectly implemented it as a single object rather than an array.
  • Modern browsers support contentBoxSize as an array of objects, to enable them to report box sizes for fragmented elements (for example, in a multi-column scenario).

js

const resizeObserver = new ResizeObserver((entries) => {
  for (let entry of entries) {
    if (entry.contentBoxSize) {
      // The standard makes contentBoxSize an array...
      if (entry.contentBoxSize[0]) {
        h1Elem.style.fontSize =
          Math.max(1.5, entry.contentBoxSize[0].inlineSize / 200) + "rem";
        pElem.style.fontSize =
          Math.max(1, entry.contentBoxSize[0].inlineSize / 600) + "rem";
      } else {
        // ...but old versions of Firefox treat it as a single item
        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";
    }
  }
  console.log("Size changed");
});

resizeObserver.observe(divElem);

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
ResizeObserverEntry 64 79 69 No 51 13.1 64 64 79 47 13.4 9.0
borderBoxSize 84 84 92
69–92Implemented as a single object representing a content box size, rather than an array of content box size objects.
No 70 15.4 84 84 92
79–92Implemented as a single object representing a content box size, rather than an array of content box size objects.
60 15.4 14.0
contentBoxSize 84 84 92
69–92Implemented as a single object representing a content box size, rather than an array of content box size objects.
No 70 15.4 84 84 92
79–92Implemented as a single object representing a content box size, rather than an array of content box size objects.
60 15.4 14.0
contentRect 64 79 69 No 51 13.1 64 64 79 47 13.4 9.0
devicePixelContentBoxSize 84 84 93 No 70 No 84 84 93 60 No 14.0
target 64 79 69 No 51 13.1 64 64 79 47 13.4 9.0

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserverEntry