HTML - DOM Style 物件 height 屬性



HTML DOM Style 物件的 **height** 屬性設定或返回元素的高度。它僅影響塊級元素或具有絕對或固定定位的元素。

語法

設定 height 屬性
object.style.height= "auto | length | percentage | initial| inherit";
獲取 height 屬性
object.style.height;

屬性值

描述
auto 這是預設值,瀏覽器會設定高度。
長度 以長度單位指定高度。
百分比 以父元素的百分比指定高度。
initial 用於將此屬性設定為其預設值。
inherit 用於繼承其父元素的屬性。

返回值

它返回一個字串值,表示元素的高度。

HTML DOM Style 物件“height”屬性示例

以下示例設定 div 和影像的高度。

設定 div 元素的高度

以下示例設定 div 元素的高度。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        HTML DOM Style Object fontWeight Property
    </title>
    <style>
        div {
            background-color: #04af2f;
        }
    </style>
</head>
<body>
    <p>Click to set height of div element.</p>
    <button onclick="fun()">Set Height</button>
    <br>
    <br>
    <div id="height">
        Welcome to Tutorials Point.
    </div>    
    <script>
        function fun() {
            document.getElementById("height")
                .style.height= "400px";
        }
    </script>
</body>
</html>

設定影像的高度

以下示例設定影像的高度。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        HTML DOM Style Object fontWeight Property
    </title>
</head>
<body>
    <p>Click to set height of div element.</p>
    <button onclick="fun()">Set Height</button>
    <br>
    <br>
    <img src="/html/images/test.png" id="height">  
    <script>
        function fun() {
            document.getElementById("height")
                .style.height= "50px";
        }
    </script>
</body>
</html>

支援的瀏覽器

屬性 Chrome Edge Firefox Safari Opera
height 是 1 是 12 是 1 是 1 是 7
html_dom_style_object_reference.htm
廣告