HTML onresize Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The onresize attribute is an event-handler attribute for the window resize event. It runs JavaScript when the document view (browser window) has been resized. In HTML, place it on <body> — that maps to the Window object, same as window.onresize.

The event is not cancelable and does not bubble. Modern browsers fire resize only on window. For an element’s own size changes, use ResizeObserver instead. Related window UI events: onscroll and onload.

Baseline · Widely available.

Not deprecated and not experimental. The window resize event is stable across modern browsers since about July 2015.

Prefer addEventListener in apps.

Inline onresize is fine for learning. Production code usually uses window.addEventListener("resize", handler). Debounce heavy work — resize can fire many times while dragging.

HTML Syntax

syntax.html
<body onresize=" script">

script is JavaScript that runs when the window is resized. Equivalent JS: window.onresize = handler or window.addEventListener("resize", handler).

Attribute Values

The attribute value is always a script. Common patterns:

Value / patternDescriptionTry it
scriptAny JavaScript run when the window size changesTry it
onresize="fn()"Call a named function from <body>Try it
addEventListener("resize")Preferred modern listener on windowTry it
Debounced handlerLimit how often heavy work runs during drag-resizeTry it
innerWidth / innerHeightRead the viewport size inside the handlerTry it

Live Preview

Resize this browser window (or DevTools dock). Width and height update on resize.

Viewport

Uses window.innerWidth and window.innerHeight for this page.

Size

—

resize events: 0

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor — open the result in a full window to resize easily.

Example 1 — body onresize

Show viewport size when the window is resized.

HTML
<body onresize="showSize()">
  <p id="size">Resize the window…</p>

  <script>
    function showSize() {
      document.getElementById("size").textContent =
        window.innerWidth + " × " + window.innerHeight;
    }
    showSize();
  </script>
</body>
Try It Yourself

How It Works

  1. Put onresize on <body> — it maps to the window.
  2. User resizes the browser — the resize event fires.
  3. Handler reads size — innerWidth and innerHeight.
  4. Update the page — show dimensions or adjust layout logic.

Example 2 — window.addEventListener

Preferred script pattern — listen on window directly.

HTML
<p id="log">Waiting for resize…</p>

<script>
  function reportSize() {
    document.getElementById("log").textContent =
      "Window: " + window.innerWidth + " × " + window.innerHeight;
  }

  window.addEventListener("resize", reportSize);
  reportSize();
</script>
Try It Yourself

How It Works

  1. Define reportSize — write width × height into the log.
  2. Register on window — only the window receives resize.
  3. Call once on load — so the first size appears without waiting.
  4. Resize the window — the listener updates the text.

Example 3 — Debounced resize

Wait until resizing pauses before running heavier work.

HTML
<p id="status">Idle</p>

<script>
  var timer = null;

  function onResizeSettled() {
    document.getElementById("status").textContent =
      "Settled at " + window.innerWidth + " × " + window.innerHeight;
  }

  window.addEventListener("resize", function () {
    document.getElementById("status").textContent = "Resizing…";
    clearTimeout(timer);
    timer = setTimeout(onResizeSettled, 200);
  });
</script>
Try It Yourself

How It Works

  1. Each resize shows “Resizing…” and resets a timer.
  2. After 200 ms quiet — treat the size as settled.
  3. Run heavy work once — layout, charts, or network — not on every pixel drag.
  4. Keep the page responsive — debounce is the usual pattern for window resize.

Edge Cases & Pitfalls

Most confusion is listening on a div, or doing heavy work on every resize tick.

window only

Fires only on window

You can set onresize on other elements, but modern browsers deliver resize only to the window. Use ResizeObserver for boxes.

body

<body onresize> maps to Window

In HTML, several body handlers (including onresize) are window event-handler content attributes — not element-level resize.

performance

Fires very often while dragging

Debounce or throttle before expensive layout, canvas, or network work. See MDN’s notes on optimizing window.onresize.

iframe

Embedded demos report the iframe

In Try It Yourself, sizes are for that frame’s viewport. Open the result fullscreen to feel a real window resize.

property

.onresize = replaces listeners

Assigning the IDL property overwrites a previous assignment. Prefer addEventListener for multiple handlers.

Browser Support

onresize is a window event-handler attribute (often written on body in HTML). It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

onresize

Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support the window resize event.

97% Global support (approx.)
Google Chrome 1+
Full support
Mozilla Firefox 1+
Full support
Apple Safari 1.1+
Full support
Microsoft Edge 12+
Full support
Opera 7+
Full support
Samsung Internet Modern · Full
Full support
Internet Explorer 4+
Supported
onresize Widely supported

Bottom line: Safe to use for viewport-size UI. Prefer addEventListener, debounce heavy work, and use ResizeObserver for element-level size tracking.

Key Takeaways

  • Role: run script when the browser window is resized.
  • Target: window only — use body onresize or addEventListener.
  • Read size: innerWidth / innerHeight (or CSS media queries for layout).
  • Elements: track box size with ResizeObserver, not onresize.

One line: onresize means “the window changed size” — listen on window, debounce heavy work, and use ResizeObserver for elements.

Frequently Asked Questions

It is an event-handler attribute for the window resize event. When the document view (browser window) is resized, the resize event fires and the script runs. In HTML, putting onresize on body maps to the Window object.
On the body element in markup (it maps to window), or use window.addEventListener("resize", handler) in JavaScript. Resize events fire only on the window, not on arbitrary elements.
No. Modern browsers fire resize only on window. For a box or component changing size, use the ResizeObserver API instead.
Dragging a window edge can fire many times per second. Debounce or throttle the handler if you do heavy work (layout, network, canvas redraw).
Both work. For learning, body onresize is fine. In production code, prefer window.addEventListener("resize", handler).
No. The resize event and onresize handler are Baseline widely available. Prefer addEventListener in apps, and ResizeObserver for element-level size changes.

Next: onscroll

Run JavaScript when the user scrolls the document or an element.

onscroll tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful