Style maxWidthProperty

Style 对象参考手册Style Object

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:An element's width can never be greater 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 element's width has no limit.
length Defines the maximum width using units such as 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

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the maxWidth property.

Note:IE7 and earlier versions do not support the "inherit" value. IE8 supports "inherit" only if a !DOCTYPE is specified. IE9 supports "inherit".


Example

Example

Set the maximum width of an element:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Example(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 the maximum width of the element">

</body>
</html>

Try it Yourself »


Style 对象参考手册Style Object Other Extensions