HTML - DOM樣式物件flexDirection屬性



HTML DOM 樣式物件 **flexDirection** 屬性設定或返回彈性元素的放置方向。

語法

設定 flexDirection 屬性
object.style.flexDirection= "row | row-reverse | column | column-reverse | initial | inherit";
獲取 flexDirection 屬性
object.style.flexDirection;

屬性值

描述
row 這是預設值,它將彈性專案水平顯示為一行。
row-reverse 與 row 相同,但順序相反。
column 它將彈性專案垂直顯示為一列。
column-reverse 與 column 相同,但順序相反。
initial 用於將此屬性設定為其預設值。
inherit 用於繼承其父元素的屬性。

返回值

它返回一個字串值,表示元素的 flex direction 屬性。

HTML DOM 樣式物件“flexDirection”屬性示例

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

將 Flex Direction 設定為“row-reverse”

以下示例將 flex direction 設定為 row-reverse,預設設定為 row。

<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        #flex {
            width: 500px;
            height: 500px;
            border: 1px solid black;
            display: flex;
        }
        #flex div {
            height: 80px;
            width: 80px;
        }
    </style>
    <title>
        HTML DOM Style Object flexDirection Property
    </title>
</head>
<body>
    <p>Click to apply flexDirection property.</p>
    <button onclick="fun()">Row-Reverse</button>
    <br>
    <br>
    <div id="flex">
        <div style="background-color: #04af2f;">1</div>
        <div style="background-color: aqua;">2</div>
        <div style="background-color: yellow;">3</div>
        <div style="background-color: whitesmoke">4</div>
        <div style="background-color: black;">5</div>
    </div>
    <script>
        function fun() {
            document.getElementById("flex")
                .style.flexDirection = "row-reverse"
        }
    </script>
</body>
</html>

將 Flex Direction 設定為“column”和“column-reverse”

在此示例中,我們將 flex direction 設定為 column 和 column-reverse。

<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        #flex {
            width: 500px;
            height: 500px;
            border: 1px solid black;
            display: flex;
        }
        #flex div {
            height: 80px;
            width: 80px;
        }
    </style>
    <title>
        HTML DOM Style Object flexDirection Property
    </title>
</head>
<body>
    <p>Click to apply flexDirection property.</p>
    <button onclick="fun()">Column</button>
    <button onclick="funTwo()">Column-Reverse</button>
    <br>
    <br>
    <div id="flex">
        <div style="background-color: #04af2f;">1</div>
        <div style="background-color: aqua;">2</div>
        <div style="background-color: yellow;">3</div>
        <div style="background-color: whitesmoke">4</div>
        <div style="background-color: black;">5</div>
    </div>
    <script>
        function fun() {
            document.getElementById("flex")
                .style.flexDirection = "column"
        }
        function funTwo() {
            document.getElementById("flex")
                .style.flexDirection = "column-reverse"
        }
    </script>
</body>
</html>

支援的瀏覽器

屬性 Chrome Edge Firefox Safari Opera
flexDirection 是 29 是 12 是 20 是 9 是 12.1
html_dom_style_object_reference.htm
廣告