ResizeObserver API
TL;DR
ResizeObserver watches element size changes without polling — more efficient than resize event listeners and works for any element, not just the window.
Explanation
ResizeObserver notifies when an element's content or border box dimensions change. Unlike the window resize event, it fires for any element and includes the new size dimensions via ResizeObserverEntry.contentRect. Each entry has contentBoxSize, borderBoxSize, and devicePixelContentBoxSize. Use cases: responsive components that adapt to container size (container queries alternative), chart redraws, virtual scrolling recalculation, dynamic layout adjustments. Disconnect when component unmounts. Unlike Intersection Observer, it fires immediately on observation with current size.
Common Misconception
✗ ResizeObserver is the same as the window resize event — it observes any element's size, not just window, and provides exact dimensions.
Why It Matters
ResizeObserver enables container-query-like component behaviour without CSS container queries, crucial for truly responsive UI components.
Common Mistakes
- Not disconnecting observer on component unmount — memory leak.
- Reading layout inside callback causing layout thrashing — read, don't write.
- Not handling the initial callback that fires immediately on observe().
Code Examples
✗ Vulnerable
window.addEventListener('resize', () => recalculateLayout(element));
✓ Fixed
const observer = new ResizeObserver(entries => {
for (const entry of entries) {
const { width, height } = entry.contentRect;
updateComponent(width, height);
}
});
observer.observe(element);
// Cleanup:
observer.disconnect();
Tags
🤝 Adopt this term
£79/year · your link shown here
Added
23 Mar 2026
Edited
5 Apr 2026
Views
21
🤖 AI Guestbook educational data only
|
|
Last 30 days
Agents 0
No pings yet today
No pings yesterday
Amazonbot 6
Unknown AI 3
Perplexity 3
Google 2
ChatGPT 1
Majestic 1
Ahrefs 1
Also referenced
How they use it
crawler 15
crawler_json 1
pre-tracking 1
Related categories
⚡
DEV INTEL
Tools & Severity
🔵 Info
⚙ Fix effort: Low
⚡ Quick Fix
Replace window resize + getBoundingClientRect polling with ResizeObserver. Always call disconnect() on cleanup.
📦 Applies To
javascript HTML5
web
🔗 Prerequisites
🔍 Detection Hints
ResizeObserver
Auto-detectable:
✗ No
🤖 AI Agent
Confidence: Low
False Positives: High
✗ Manual fix
Fix: Low
Context: Function