HTML - DOM Style 物件 display 屬性



HTML DOM Style 物件的 **display** 屬性設定或返回元素的顯示型別。它類似於 visibility 屬性,但區別在於,使用 display:none 會隱藏整個元素,而使用 visibility:hidden 則僅使元素不可見,元素仍然位於其原始位置。

語法

設定 display 屬性
object.style.display= value;
獲取 display 屬性
object.style.display;

屬性值

描述
inline 這是預設值,它將元素顯示為內聯元素。
block 它將元素顯示為塊級元素。
compact 它根據上下文將元素顯示為塊級或內聯元素。
flex 它將元素顯示為塊級彈性盒。
inline-block 它將元素顯示為內聯框內的塊框。
inline-flex 它將元素顯示為內聯級彈性盒。
inline-table 它將元素顯示為內聯表格。
list-item 它將元素顯示為列表。
marker 它與 :before 和 :after CSS 偽元素一起使用。它將框前或框後的內容設定為標記。
none 它不顯示任何元素。
run-in 它根據上下文將元素顯示為塊級或內聯元素。
table 它將元素顯示為塊級表格,在表格前後各有一個換行符。
table-caption 它將元素顯示為表格標題。
table-cell 它將元素顯示為表格單元格。
table-column 它將元素顯示為單元格列。
table-column-group 它將元素顯示為一個或多個列的組。
table-footer-group 它將元素顯示為表格腳註行。
table-header-group 它將元素顯示為表格表頭行。
table-row 它將元素顯示為表格行。
table-row-group 它將元素顯示為一個或多個行的組。
initial 它用於將此屬性設定為其預設值。
inherit 它用於繼承其父元素的屬性。

返回值

它返回一個字串值,表示元素的顯示型別。

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

以下示例說明了 display 屬性的不同屬性值。

設定 Display 屬性

在此示例中,我們已將 display 屬性值設定為“inline”、“block”和“none”。

<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        #display {
            height: 400px;
            width: 400px;
            background-color: blanchedalmond;
        }
    </style>
    <title>
        HTML DOM Style Object display Property
    </title>
</head>
<body>
    <p>Click to change display style</p>
    <button onclick="fun()">Inline</button>
    <button onclick="funTwo()">Block</button>
    <button onclick="funThree()">None</button>
    <br>
    <div id="display">
        Welcome to TutorialsPoint..
    </div>
    <script>
        function fun() {
            document.getElementById("display")
                .style.display = "inline";
        }
        function funTwo() {
            document.getElementById("display")
                .style.display = "block";
        }
        function funThree() {
            document.getElementById("display")
                .style.display = "none";
        }
    </script>
</body>
</html>

獲取 Display 屬性

在此示例中,我們使用了兩個 display 屬性值,它們分別是“table-caption”和“none”,並且我們還獲取了所用屬性值的名稱。

<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        #display {
            height: 400px;
            width: 400px;
            background-color: blanchedalmond;
        }
    </style>
    <title>
        HTML DOM Style Object display Property
    </title>
</head>
<body>
    <p>Click to change display style</p>
    <button onclick="funThree()">Table Caption</button>
    <button onclick="funTwo()">None</button>
    <br>
    <div id="display">
        Welcome to <span id="cap">TutorialsPoint</span>
    </div>
    <p id="output"></p>
    <script>
        function funThree() {
            let x=document.getElementById("cap")
                .style.display = "table-caption";
            document.getElementById("output").innerHTML=x
        }
        function funTwo() {
            let x=document.getElementById("display")
                .style.display = "none";
            document.getElementById("output").innerHTML=x
        }        
    </script>
</body>
</html>

支援的瀏覽器

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