HTML Topics
- HTML Intro
- HTML Basic
- HTML Editors
- HTML CSS
- HTML Tags
- HTML Deprecated Tags
- HTML Events
- HTML Event Attributes
- HTML Global Attributes
- HTML Attributes
- abbr
- accept
- accept-charset
- accesskey
- action
- align
- alt
- archive
- as
- async
- autocapitalize
- autocomplete
- autofocus
- autoplay
- axis
- bgcolor
- border
- cellpadding
- cellspacing
- char
- charoff
- charset
- checked
- cite
- class
- classid
- codebase
- codetype
- color
- cols
- colspan
- compact
- content
- contenteditable
- controls
- coords
- crossorigin
- data
- data-*
- datetime
- declare
- default
- defer
- dir
- dirname
- disabled
- download
- draggable
- enctype
- enterkeyhint
- for
- form
- formaction
- formenctype
- formmethod
- formnovalidate
- formtarget
- frame
- frameborder
- frameset
- headers
- height
- hidden
- high
- href
- hreflang
- hspace
- http-equiv
- id
- inert
- inputmode
- ismap
- kind
- label
- lang
- list
- loop
- low
- max
- maxlength
- media
- method
- min
- multiple
- muted
- name
- novalidate
- onabort
- onafterprint
- onbeforeprint
- onbeforeunload
- onblur
- oncanplay
- oncanplaythrough
- onchange
- onclick
- oncontextmenu
- oncopy
- oncuechange
- oncut
- ondblclick
- ondrag
- ondragend
- ondragenter
- ondragleave
- ondragover
- ondragstart
- ondrop
- ondurationchange
- onemptied
- onended
- onerror
- onfocus
- onhashchange
- oninput
- oninvalid
- onkeydown
- onkeypress
- onkeyup
- onload
- onloadeddata
- onloadedmetadata
- onloadstart
- onmousedown
- onmousemove
- onmouseout
- onmouseover
- onmouseup
- onmousewheel
- onoffline
- ononline
- onpagehide
- onpageshow
- onpaste
- onpause
- onplay
- onplaying
- onpopstate
- onprogress
- onratechange
- onreset
- onresize
- onscroll
- onsearch
- onseeked
- onseeking
- onselect
- onstalled
- onstorage
- onsubmit
- onsuspend
- ontimeupdate
- ontoggle
- onunload
- onvolumechange
- onwaiting
- onwheel
- open
- optimum
- pattern
- placeholder
- popover
- popovertarget
- popovertargetaction
- poster
- preload
- readonly
- rel
- required
- reversed
- rows
- rowspan
- sandbox
- scope
- selected
- shape
- size
- sizes
- span
- spellcheck
- src
- srcdoc
- srclang
- srcset
- start
- step
- style
- tabindex
- target
- title
- translate
- type
- usemap
- value
- width
- wrap
- HTML Comments
- HTML Entity
- HTML Head
- HTML Form
- HTML IndexedDB
- HTML Drag & Drop
- HTML Geolocation
- HTML Canvas
- HTML Status Code
- HTML Language Code
- HTML Country Code
- HTML Charset
- MIME Types
HTML onresize Attribute
Photo Credit to CodeToFun
🙋 Introduction
The onresize
attribute in HTML is a powerful tool that allows developers to specify JavaScript code that should be executed when the browser window is resized.
This attribute is commonly used to trigger dynamic changes or updates in the web page layout based on the dimensions of the viewport.
🎯 Purpose of onresize
The primary purpose of the onresize
attribute is to enable developers to respond to changes in the size of the browser window.
This can be crucial for creating responsive and adaptive web designs that adjust to different screen sizes and orientations.
💎 Values
The onresize
attribute accepts a JavaScript code or function that will be executed when the resize event occurs.
It can be set to call an existing JavaScript function or contain inline JavaScript code. For example:
<body onresize="myFunction()">
🧠 How it Works
In this example, the myFunction() JavaScript function will be called whenever the browser window is resized.
📄 Example
Let's look at a simple example of how to use the onresize
attribute in an HTML document:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>onresize Example</title>
<style>
body {
text-align: center;
}
</style>
<script>
// JavaScript function to be executed on resize
function handleResize() {
alert("Window has been resized!");
}
</script>
</head>
<body onresize="handleResize()">
<h1>HTML onresize Attribute Example</h1>
<p>Resize the browser window to see the alert.</p>
</body>
</html>
🧠 How it Works
In this example, the handleResize() function is triggered whenever the browser window is resized, displaying an alert to notify the user.
🔄 Dynamic Values with JavaScript
You can also dynamically set the onresize
attribute using JavaScript.
This can be useful when you want to customize the resize behavior based on certain conditions or user interactions. Here's a brief example:
<script>
// Dynamically set onresize behavior
window.onresize = function() {
console.log("Window has been resized dynamically!");
};
</script>
🧠 How it Works
In this script, the onresize
attribute is set to an anonymous function that logs a message to the console whenever the window is resized. This dynamic approach allows for greater flexibility in handling resize events.
🏆 Best Practices
- Use the
onresize
attribute judiciously, considering the specific requirements of your web page. - Be mindful of the performance implications of executing JavaScript on resize, especially if the code involves complex operations.
- Test your web page across different devices and screen sizes to ensure a seamless user experience.
🎉 Conclusion
The onresize
attribute is a valuable feature for creating responsive and dynamic web pages that adapt to varying viewport sizes.
By leveraging this attribute, developers can enhance the overall user experience and ensure their web content remains visually appealing across different devices.
👨💻 Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (HTML onresize Attribute), please comment here. I will help you immediately.