Latest web development tutorials

HTML DOM Style maxWidth property

Style Object Reference Style Objects

Definition and Usage

maxWidth property sets or returns the maximum width of the element.

maxWidth properties play a role only in the block-level element or elements of absolute / fixed position.

Note: The width of an element can never be greater than the value specified maxWidth property.

grammar

Setting maxWidth properties:

Object.style.maxWidth="none|length|%|inherit"

Back maxWidth properties:

Object.style.maxWidth

描述
none 默认。元素的宽度没有限制。
length 使用 px、cm 等单位定义最大宽度。
% 定义最大宽度为父元素的百分比。
inherit maxWidth 属性的值从父元素继承。


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support maxWidth property.

Note: IE7 and earlier versions do not support the "inherit" value.IE8 only provides! DOCTYPE supported "inherit". IE9 support "inherit".


Examples

Examples

The maximum width of the element:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>本教程(w3big.com)</title>
<script>
function displayResult(){
    document.getElementById("p1").style.maxWidth="150px";
}
</script>
</head>
<body>

<p style="background:red;" id="p1">这是一些文本。这是一些文本。这是一些文本。这是一些文本。这是一些文本。这是一些文本。这是一些文本。</p>
<input type="button" onclick="displayResult()" value="设置元素最大宽度">

</body>
</html>

try it"


Style Object Reference Style Objects