Web APIs
ResizeObserver.observe()
Registers an element box for resize observation. Select content-box, border-box or supported device-pixel-content-box as needed.
Formal Syntax & Structure
javascript syntaxjavascript
observer.observe(target, [options])Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| target | Element | Observed element. |
| options | ResizeObserverOptions | Observed box type. |
Return Value
undefined
Registers size observation.
Working Example
Example Snippetjavascript
(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();}})()