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> |
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> |
Output:
- Before click on button:
- After click on button:
Supported Browsers: The browser supported by HTML | DOM Style maxWidth Property are listed below:
- Google Chrome
- Internet Explorer
- Mozilla firefox
- Opera
- Safari
Recommended Posts:
- HTML | DOM Style right Property
- HTML | DOM Style top Property
- HTML | DOM Style transform Property
- HTML | DOM Style visibility Property
- HTML | DOM Style columnRuleColor Property
- HTML | DOM Style animationName Property
- HTML | DOM Style animationDelay Property
- HTML | DOM Style paddingLeft Property
- HTML | DOM Style minWidth Property
- HTML | DOM Style backgroundColor Property
- HTML | DOM Style transitionDelay Property
- HTML | DOM Style tableLayout Property
- HTML | DOM Style backfaceVisibility Property
- HTML | DOM Style flexBasis Property
- HTML | DOM Style columnRuleStyle 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.



