Web APIs
ResizeObserver()
Creates an observer receiving element box size reports after layout-related updates.
Formal Syntax & Structure
javascript syntaxjavascript
new ResizeObserver(callback)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| callback | function | Receives resize entries and observer. |
Return Value
ResizeObserver
Observer instance.
Working Example
Example Snippetjavascript
(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();}})()