HTML DOM Style maxWidth Property
Definition and Usage
The maxWidth property sets or returns the maximum width of an element.
The maxWidth property only works on block-level elements or elements with absolute/fixed positioning.
Note: The width of an element can never be larger than the value specified by the maxWidth property.
Syntax
Set the maxWidth property:
Object.style.maxWidth="none|length|%|inherit"
Return the maxWidth property:
Object.style.maxWidth
| Value | Description |
|---|---|
| none | Default. The width of the element is not limited. |
| length | Defines the maximum width in px, cm, etc. |
| % | Defines the maximum width as a percentage of the parent element. |
| inherit | The value of the maxWidth property is inherited from the parent element. |
Browser Support
All major browsers support the maxWidth property.
Note: IE7 and earlier versions do not support the "inherit" value. IE8 only supports "inherit" if a !DOCTYPE is specified. IE9 supports "inherit".
Example
Set the maximum width of an element:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Tutorial(example.com)</title>
<script>
function displayResult(){
document.getElementById("p1").style.maxWidth="150px";
}
</script>
</head>
<body>
<p style="background:red;" id="p1">This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text.</p>
<input type="button" onclick="displayResult()" value="Set maxWidth">
</body>
</html>
YouTip