HTML - DOM Style 对象 maxHeight 属性



HTML DOM Style 对象 **maxHeight** 属性设置或返回元素的最大高度。它只影响块级元素或具有固定或绝对定位的元素。

语法

设置 maxHeight 属性
object.style.maxHeight= "none | length | percentage | initial | inherit";
获取 maxHeight 属性
object.style.maxHeight;

属性值

描述
none 这是默认值,不设置最大高度限制。
长度单位 指定元素的最大高度,以长度单位表示。
百分比 指定元素的最大高度,为父元素高度的百分比。
initial 用于将此属性设置为其默认值。
inherit 用于继承其父元素的属性。

返回值

返回一个字符串值,表示元素的最大高度。

HTML DOM Style 对象 'maxHeight' 属性示例

以下示例演示如何设置 div 元素的最大高度。

设置 div 元素的最大高度

以下示例使用 **长度单位** 属性值设置 div 元素的最大高度。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        HTML DOM Style Object maxHeight Property
    </title>
    <style>
        #margin {
            border: 2px solid #04af2f;
            overflow: auto;
            width: 400px;
            height: 200px;
        }
    </style>
</head>
<body>
    <h3>
        Click to set maximum height.
    </h3>
    <button onclick="fun()">
        Set Max Height
    </button>
    <br>
    <div id="margin">
        <p>
            CSS is the acronym for "Cascading 
            Style Sheet". It's a style sheet 
            language used for describing the 
            presentation of a document written 
            in a markup language like HTML. 
        </p>
        <p>
            CSS helps the web developers to 
            control the layout and other visual 
            aspects of the web pages. CSS plays 
            a crucial role in modern web development 
            by providing the tools necessary to 
            create visually appealing, accessible,
            and responsive websites.
        </p>
    </div>
    <script>
        function fun() {
            document.getElementById("margin")
                .style.maxHeight = "100px";
        }
    </script>
</body>
</html>

使用百分比设置最大高度

以下示例使用 **百分比** 值设置子 div 元素的最大高度。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        HTML DOM Style Object maxHeight Property
    </title>
    <style>
        #margin {
            border: 2px solid #04af2f;
            overflow: auto;
            width: 400px;
            height: 200px;
        }
        #height {
            margin: 10px;
            border: 1px solid black;
            overflow: auto;
        }
    </style>
</head>
<body>
    <h3>
        Click to set maximum height.
    </h3>
    <button onclick="fun()">
        Set Max Height
    </button>
    <br>
    <div id="margin">
        <div id="height">
            <p>
                CSS is the acronym for "Cascading
                Style Sheet". It's a style sheet
                language used for describing the
                presentation of a document written
                in a markup language like HTML.
            </p>
            <p>
                CSS helps the web developers to
                control the layout and other visual
                aspects of the web pages. CSS plays
                a crucial role in modern web development
                by providing the tools necessary to
                create visually appealing, accessible,
                and responsive websites.
            </p>
        </div>
    </div>
    <script>
        function fun() {
            document.getElementById("height")
                .style.maxHeight = "40%";
        }
    </script>
</body>
</html>

支持的浏览器

属性 Chrome Edge Firefox Safari Opera
maxHeight 是 18 是 12 是 1 是 1.3 是 7
html_dom_style_object_reference.htm
广告
© . All rights reserved.