Window: resize event

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

The resize event fires when the document view (window) has been resized.

This event is not cancelable and does not bubble.

In some earlier browsers it was possible to register resize event handlers on any HTML element. It is still possible to set onresize attributes or use addEventListener() to set a handler on any element. However, resize events are only fired on the window object (i.e., returned by document.defaultView). Only handlers registered on the window object will receive resize events.

While the resize event fires only for the window nowadays, you can get resize notifications for other elements using the ResizeObserver API.

If the resize event is triggered too many times for your application, see Optimizing window.onresize to control the time after which the event fires.

Syntax

Use the event name in methods like addEventListener(), or set an event handler property.

js
addEventListener("resize", (event) => { })

onresize = (event) => { }

Event type

A generic Event.

Examples

Window size logger

The following example reports the window size each time it is resized.

HTML

html
<p>Resize the browser window to fire the <code>resize</code> event.</p>
<p>Window height: <span id="height"></span></p>
<p>Window width: <span id="width"></span></p>

JavaScript

js
const heightOutput = document.querySelector("#height");
const widthOutput = document.querySelector("#width");

function reportWindowSize() {
  heightOutput.textContent = window.innerHeight;
  widthOutput.textContent = window.innerWidth;
}

window.onresize = reportWindowSize;

Result

Note: The example output here is in an <iframe>, so the reported width and height values are for the <iframe>, not the window that this page is in. In particular, it will be hard to adjust the window size so as to see a difference in the reported height.

The effect is easier to see if you view the example in its own window.

addEventListener equivalent

You could set up the event handler using the addEventListener() method:

js
window.addEventListener("resize", reportWindowSize);

Specifications

Specification
CSSOM View Module
# eventdef-window-resize

Browser compatibility

desktop mobile server
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
resize event
Chrome – Full support
Chrome 1 (Release date: 2008-12-11)
footnote
footnote Chrome does not fire a resize event on page load.
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote
footnote Before Edge 79, Edge fired a resize event on page load. This is no longer the case.
Firefox – Full support
Firefox 1 (Release date: 2004-11-09)
footnote
footnote Before Firefox 68, Firefox fired a resize event on page load. This is no longer the case.
Opera – Full support
Opera 7 (Release date: 2003-01-28)
footnote
footnote Opera does not fire a resize event on page load.
Safari – Full support
Safari 1.1 (Release date: 2003-10-24)
footnote Full support
Chrome Android – Full support
Chrome Android 18 (Release date: 2012-06-27)
footnote
footnote Chrome Android does not fire a resize event on page load.
Firefox for Android – Full support
Firefox for Android 4 (Release date: 2011-03-29)
footnote
footnote Before Firefox for Android 68, Firefox for Android fired a resize event on page load. This is no longer the case.
Opera Android – Full support
Opera Android 10.1 (Release date: 2010-11-09)
footnote
footnote Opera does not fire a resize event on page load.
Safari on iOS – Full support
Safari on iOS 1 (Release date: 2007-06-29)
footnote Full support
Samsung Internet – Full support
Samsung Internet 1 (Release date: 2013-04-27)
footnote
footnote Samsung Internet does not fire a resize event on page load.
WebView Android – Full support
WebView Android 1 (Release date: 2008-09-23)
footnote
footnote WebView does not fire a resize event on page load.
WebView on iOS – Full support
WebView on iOS 1 (Release date: 2007-06-29)
footnote Full support
Deno – No support
Deno
footnote No support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
See implementation notes.