files/en-us/web/api/resizeobserver/observe/index.md
{{APIRef("Resize Observer API")}}
The observe() method of the
{{domxref("ResizeObserver")}} interface starts observing the specified
{{domxref('Element')}} or {{domxref('SVGElement')}}.
observe(target)
observe(target, options)
target
options {{optional_inline}}
box
content-box (the default)
border-box
device-pixel-content-box
None ({{jsxref("undefined")}}).
None.
The following snippet is taken from the resize-observer-text.html (see source) example:
const resizeObserver = new ResizeObserver((entries) => {
for (const entry of entries) {
if (entry.contentBoxSize) {
// Checking for chrome as using a non-standard 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 {
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);
An observe() call with an options object would look like so:
resizeObserver.observe(divElem, { box: "border-box" });
{{Specifications}}
{{Compat}}