HTML | DOM Style width Property
The Style width property in HTML DOM is used to set or return the width of an element which can be block-level elements or elements with fixed position.
Syntax:
- It is used to return the width property:
object.style.width
- It is used to set the width property:
object.style.width = "auto|length|%|initial|inherit"
Property Value Description
- auto-This value automatically sets the default width value by the browser.
- length-This value sets the width value in the specified length units.
- %-Percentage value sets the width value in the specified percentage of the parent element’s width.
- initial-This value sets the width property to its browser’s default value.
- inherit-This value sets the width property to the value from its parent element.
Return Value: A String, representing the width of an element.
Example 1:
<!DOCTYPE html> <html> <head> <style> #GfG { width: 100px; height: 100px; background-color: green; color: white; } </style> </head> <body> <p>HTML|DOM Style Width Property:</p> <button onclick="myFunction()">Click here!</button> <div id="GfG"> <h1>GfG</h1> </div> <script> function myFunction() { document.getElementById("GfG") .style.width = "500px"; } </script> </body> </html> |
chevron_right
filter_none
Output:
- Before click on the button:
- After click on the button:
Example 2:
<!DOCTYPE html> <html> <body> <h1>GeeksforGeeks</h1> <h2>HTML|DOM Style Width Property:</h2> <button type="button" id="myBtn" onclick="myFunction()">Click Here!</button> <script> function myFunction() { document.getElementById("myBtn") .style.width = "500px"; } </script> </body> </html> |
chevron_right
filter_none
Output:
- Before click on the button:
- After click on the button:
Supported Browsers: The browser supported by DOM style width property are listed below:
- Google Chrome
- Internet Explorer
- Firefox
- Safari
- Opera
Recommended Posts:
- HTML | DOM HR width Property
- HTML | DOM IFrame width Property
- HTML | DOM Object width Property
- HTML | Screen width Property
- HTML | DOM Video width Property
- HTML | canvas ImageData width Property
- HTML | DOM Input Image width Property
- HTML | DOM Style right Property
- HTML | DOM Style top Property
- HTML | DOM Style animationTimingFunction Property
- HTML | DOM Style perspectiveOrigin Property
- HTML | DOM Style flex Property
- HTML | DOM Style font Property
- HTML | DOM Style maxWidth Property
- HTML | DOM Style minWidth Property
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



