CSS Portal

HTML onresize Event Attribute

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
onresize
HTML Event AttributeThis event attribute is supported by all modern browsers.
Updated 14th October 2023

Description

The onresize HTML event attribute is triggered when the browser window is resized. It can be used to perform actions such as updating the layout of the page, resizing images, or showing or hiding elements.

The onresize attribute can be used on any HTML element, but it is most commonly used on the <body> element. This is because the <body> element represents the entire document, so resizing the browser window will also resize the <body> element.

To use the onresize attribute, simply add it to the HTML element you want to listen for resize events on. The value of the onresize attribute should be a JavaScript function that will be executed when the element is resized.

For example, the following code will execute the myFunction() function when the browser window is resized:

<body onresize="myFunction()">
  ...
</body>

The myFunction() function can then be used to perform any actions that you want to take when the browser window is resized. For example, the following function will update the width of a div element to match the width of the browser window:

function myFunction() {
  var div = document.getElementById("myDiv");
  div.style.width = window.innerWidth + "px";
}

The onresize event attribute is a powerful tool that can be used to create dynamic and responsive web pages. It is especially useful for pages that need to be displayed on different devices with different screen sizes.

Syntax

<element onresize="script">

Values

  • scriptThe name of the script to use when the event has been triggered.

Example

<!DOCTYPE HTML>
<html>
<body onresize="myFunction()">

<p>Try resizing your browser window.</p>

<script>
function myFunction() {
alert("EXAMPLE: You have resized the browser window.");
}
</script>

</body>
</html>
Open in editor

Browser Support

The following information will show you the current browser support for the HTML onresize event attribute. Hover over a browser icon to see the version that first introduced support for this HTML event attribute.

This event attribute is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee