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
廣告