HTML | DOM Style maxWidth Property
The maxWidth property set/return the maximum width of an element.
The maxWidth property affects only on block-level elements, absolute or fixed position elements.
Syntax:
-
It returns the maxWidth property.
object.style.maxWidth
-
It sets the maxWidth Property.
object.style.maxWidth = "none|length|%|initial|inherit"
Values:
| Value | Description |
|---|---|
| none | Default value without any limit on width of the element |
| length | Define width’s maximum value in length unit |
| % | Define width’s maximum value in % of the parent element |
| initial | Set property to its default value |
| inherit | Inherit from its parent element |
Return value:It return the maximum width of element.
Example-1: Set width in length unit.
<!DOCTYPE html> <html> <head> <title>DOM Style maxWidth Property </title> </head> <style> #Geek1 { color: white; width: 300px; background: green; } </style> <body> <center> <h1 id="Geek1"> GeeksForGeeks </h1> <h2>DOM Style maxWidth Property </h2> <br> <button type="button" onclick="mygeeks()"> Click to change </button> <script> function mygeeks() { // set width using length unit. document.getElementById( "Geek1").style.maxWidth = "220px"; } </script> </center> </body> </html> |
chevron_right
filter_none
Output:
- Before click on button:
- After click on button:
Example-2: Set width in ‘%’.
<!DOCTYPE html> <html> <head> <title>DOM Style maxWidth Property </title> </head> <style> #Geek1 { color: white; width: 50%; background: green; } #Geek_Center { background: yellow; width: 400px; margin-left: 150px; } </style> <body> <center id="Geek_Center"> <h3 id="Geek1"> GeeksForGeeks </h3> <h2>DOM Style maxWidth Property </h2> <br> <button type="button" onclick="mygeeks()"> Click to change </button> <script> function mygeeks() { // Set width using % . document.getElementById( "Geek1").style.maxWidth = "35%"; } </script> </center> </body> </html> |
chevron_right
filter_none
Output:
- Before click on button:
- After click on button:
Supported Browsers:
- Google Chrome
- Internet Explorer
- Mozilla firefox
- Safari
- Opera
Recommended Posts:
- HTML | DOM Style right Property
- HTML | DOM Style top Property
- HTML | DOM Style padding Property
- HTML | DOM Style outlineStyle Property
- HTML | DOM Style visibility Property
- HTML | DOM Style columnRule Property
- HTML | DOM Style columnCount Property
- HTML | DOM Style order Property
- HTML | DOM Style fontStyle Property
- HTML | DOM Style borderTopStyle Property
- HTML | DOM Style minWidth Property
- HTML DOM | Style pageBreakAfter Property
- HTML | DOM Style flexDirection Property
- HTML | DOM Style objectFit Property
- HTML DOM | Style paddingRight 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.



