👀 Live Preview
Resize your browser window — dimensions update below:
Loading…

The onresize attribute is an inline event handler for the resize event on the window. It runs when the user resizes the browser viewport — useful for updating layout labels, repositioning charts, or syncing JavaScript widgets with the new width and height. Read window.innerWidth and window.innerHeight inside your handler. Put onresize on <body> or use window.addEventListener("resize", …). Debounce heavy work because resize fires many times per drag. For individual elements, use ResizeObserver instead. Prefer CSS media queries when possible.
Inline JS.
Window size.
Global scope.
Viewport px.
Performance.
Preferred way.
onresize AttributeThe primary purpose of onresize is to react when the browser window changes size — so JavaScript-driven layouts can adapt when CSS alone is not enough (for example redrawing a canvas chart or updating a custom breakpoint label).
The resize event fires on the window object. It does not fire for every DOM element when that element changes size — for that, use the ResizeObserver API in modern browsers.
Media queries and flexible layouts handle most responsive design without JavaScript. Use onresize when JS must recalculate sizes — charts, maps, or custom components.
Set onresize on body or assign window.onresize in JavaScript:
<body onresize="handleResize()">
…
</body>
<script>
window.onresize = handleResize;
window.addEventListener("resize", handleResize);
</script>resize event fires on the window.<body> as a window event content attribute.window.onresize in script.window.innerWidth / innerHeight for viewport size.window.addEventListener("resize", handler).<div> elements — use ResizeObserver instead.The onresize attribute accepts a string of JavaScript code:
onresize="handleResize()" — Call a named function.onresize="updateChart()" — Redraw JS widgets on resize.window.onresize = () => { … } — property assignment.var timer;
function handleResize() {
clearTimeout(timer);
timer = setTimeout(function () {
status.textContent =
window.innerWidth + " × " + window.innerHeight + " px";
}, 150);
}
window.addEventListener("resize", handleResize);| Property / API | When it runs | Notes |
|---|---|---|
resize | Window viewport changes | onresize |
window.innerWidth | Read anytime | Viewport width in px |
window.innerHeight | Read anytime | Viewport height in px |
matchMedia() | Breakpoint checks | Often better than resize |
ResizeObserver | Element size changes | Not onresize attribute |
| Target | Supported? | Notes |
|---|---|---|
<body> | Yes | Window event on body (HTML) |
window | Yes | Primary target in JavaScript |
<iframe> | Yes (legacy) | Frame resize in some cases |
<div>, <section> | No | Use ResizeObserver |
CSS @media | Related | Prefer for layout when possible |
onresize vs matchMedia vs ResizeObserver| API | What it watches | Typical use |
|---|---|---|
onresize | Browser window viewport | Charts, full-page JS layout |
matchMedia() | CSS media query match | Breakpoint toggles |
ResizeObserver | Specific element size | Component layouts |
CSS @media | Viewport width/height | Responsive styling |
Inline body handler, dynamic assignment, and a debounced breakpoint label with addEventListener.
Resize your browser window — dimensions update below:
Loading…
Show viewport dimensions when the window is resized:
<body onresize="handleResize()">
<p id="size"></p>
</body>
<script>
function handleResize() {
document.getElementById("size").textContent =
window.innerWidth + " × " + window.innerHeight + " px";
}
handleResize();
</script>The browser fires resize on the window when the viewport changes. The body attribute runs handleResize() each time.
Assign the handler on window at runtime:
<script>
window.onresize = function () {
log.textContent =
"Resized to " + window.innerWidth + " × " + window.innerHeight;
};
</script>Assigning window.onresize is equivalent to the inline body attribute for the resize event.
Wait until resizing stops before running expensive layout logic:
var timer;
window.addEventListener("resize", function () {
clearTimeout(timer);
timer = setTimeout(function () {
var w = window.innerWidth;
status.textContent = getBreakpoint(w) + " — " + w + " px";
}, 150);
});Resize fires dozens of times per second while dragging. A 150ms debounce runs your logic once after the user pauses.
Drag edge.
onresize runs.
innerWidth.
Redraw / relabel.
The resize event and onresize handler are supported in all modern browsers, including Internet Explorer 9+.
onresize supportAll major browsers fire resize when the viewport changes size.
Bottom line: Reliable for window resize handling in all modern browsers.
innerWidth / innerHeight for viewport sizeaddEventListener("resize", …) in productionResizeObserver for element-level sizingalert() on every resizeonresize on random divsThe onresize attribute runs JavaScript when the browser window changes size — useful for charts, custom breakpoints, and JS-driven layouts that CSS alone cannot handle.
Debounce your handlers, prefer CSS for styling, use addEventListener in production, and reach for ResizeObserver when you need element-level size changes.
onresizeBookmark these before wiring resize logic.
Viewport.
ScopePerformance.
PatternRead px.
API@media.
PreferResizeObserver.
Gotcharesize event fires on the window — when the user changes the browser viewport size.onresize is for the window (via body or window). Use ResizeObserver to watch when a specific element changes size.onresize runs on every viewport change. matchMedia checks whether a CSS media query matches — often cleaner for breakpoint toggles.window.addEventListener("resize", handler) is preferred in production — easier to debounce and attach multiple listeners.Practice the onresize attribute with dimension display and debounced breakpoints in the Try It editor.
5 people found this page helpful