Respond to element size changes
Quick answer
ResizeObserver reports changes to observed element box sizes. It responds to container size changes even when the window itself does not resize.
How it works
Construct a ResizeObserver with a callback, then observe(element, {box:"content-box"}) (default content box). ResizeObserverEntry exposes contentRect and box-size sequences such as contentBoxSize and borderBoxSize. In simple horizontal writing mode, inlineSize is width; in other writing modes it is the inline dimension, not always width. The border box includes padding and borders. Update independent state or another element from the callback; changing the observed size repeatedly can trigger feedback loops and skipped notifications. Unobserve or disconnect at teardown.
Local examples
Run in a fresh browser document with local synthetic data. Actual ByteMind editor support is unverified.
(async () => {const box=document.createElement("div");box.style.cssText="width:80px;height:20px;padding:5px;border:2px solid";document.body.append(box);let o;try{const widths=await new Promise(resolve=>{o=new ResizeObserver(([e])=>resolve([e.contentRect.width,e.borderBoxSize[0].inlineSize]));o.observe(box);});console.log(JSON.stringify(widths));}finally{o?.disconnect();box.remove();}})()Expected console output:
[80,94]Content width excludes padding and borders; border width includes both sides.
Run in a fresh browser document with local synthetic data. Actual ByteMind editor support is unverified.
(async () => {const box=document.createElement("div");box.style.cssText="width:70px;height:20px";document.body.append(box);let o;try{const sizes=await new Promise(resolve=>{const widths=[];o=new ResizeObserver(([e])=>{widths.push(e.contentRect.width);if(widths.length===1)box.style.width="90px";else resolve(widths);});o.observe(box);});console.log(JSON.stringify(sizes));}finally{o?.disconnect();box.remove();}})()Expected console output:
[70,90]The controlled one-time width change produces another size report.
Common mistakes
Do not use window resize as the only signal for element size changes or blindly change the observed width from inside each callback.
Practice
Observe a padded element, compare content and border sizes, then change its width once from a button and stop observing.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Respond to element size changes — Knowledge Check
Apply the concept and reason about local example output.
Why observe an element instead of relying on window resize?
Which simple box width includes padding and border?
What can repeated size-changing writes in a resize callback cause?
What is the exact console output in Respond to element size changes example 1?
(async () => {const box=document.createElement("div");box.style.cssText="width:80px;height:20px;padding:5px;border:2px solid";document.body.append(box);let o;try{const widths=await new Promise(resolve=>{o=new ResizeObserver(([e])=>resolve([e.contentRect.width,e.borderBoxSize[0].inlineSize]));o.observe(box);});console.log(JSON.stringify(widths));}finally{o?.disconnect();box.remove();}})()What is the exact console output in Respond to element size changes example 2?
(async () => {const box=document.createElement("div");box.style.cssText="width:70px;height:20px";document.body.append(box);let o;try{const sizes=await new Promise(resolve=>{const widths=[];o=new ResizeObserver(([e])=>{widths.push(e.contentRect.width);if(widths.length===1)box.style.width="90px";else resolve(widths);});o.observe(box);});console.log(JSON.stringify(sizes));}finally{o?.disconnect();box.remove();}})()