HTML - DOM Style 物件 justifyContent 屬性



HTML DOM Style 物件 **justifyContent** 屬性設定或返回彈性專案在主軸或水平方向上的對齊方式,當它們沒有使用所有可用空間時。

語法

設定 justifyContent 屬性
object.style.justifyContent= "flex-start | flex-end | center | space-between | space-around | initial | inherit";
獲取 justifyContent 屬性
object.style.justifyContent;

屬性值

描述
flex-start 這是預設值,它將彈性專案對齊到容器的開頭。
flex-end 它將彈性專案對齊到容器的末尾。
center 它將彈性專案對齊到容器的中心。
space-between 它在行之間放置彈性專案。
space-around 它在行之前、之間和之後放置彈性專案。
initial 用於將此屬性設定為其預設值。
inherit 用於繼承其父元素的屬性。

返回值

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

HTML DOM Style 物件“justifyContent”屬性的示例

以下示例說明了應用於 div 元素的 justifyContent 屬性,該元素具有五個不同顏色的專案。

將彈性專案設定為“flex-start”和“flex-end”

在此示例中,我們將彈性容器的專案對齊到 flex-start、flex-end 和 center。

<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        #flex {
            width: 700px;
            height: 300px;
            border: 1px solid black;
            display: flex;
        }
        #flex div {
            height: 80px;
            width: 80px;
        }
    </style>
    <title>
        HTML DOM Style Object justifyContent Property
    </title>
</head>
<body>
    <p>
        Click to try different justifyContent property.
    </p>
    <button onclick="funTwo()">Flex-End</button>
    <button onclick="fun()">Flex-Start</button>
    <button onclick="funThree()">Center</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 funTwo() {
            document.getElementById("flex")
                .style.justifyContent = "flex-end";
        }
        function fun() {
            document.getElementById("flex")
                .style.justifyContent = "flex-start";
        }
        function funThree() {
            document.getElementById("flex")
                .style.justifyContent = "center";
        }
    </script>
</body>
</html>

將彈性專案設定為“space-between”和“space-around”

在此示例中,我們將彈性容器的專案對齊到 space-between 和 space-around。

<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        #flex {
            width: 700px;
            height: 300px;
            border: 1px solid black;
            display: flex;
        }
        #flex div {
            height: 80px;
            width: 80px;
        }
    </style>
    <title>
        HTML DOM Style Object justifyContent Property
    </title>
</head>
<body>
    <p>
        Click to try different justifyContent property.
    </p>
    <button onclick="funTwo()">Space-Between</button>
    <button onclick="fun()">Space-Around</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 funTwo() {
            document.getElementById("flex")
                .style.justifyContent = "space-between";
        }
        function fun() {
            document.getElementById("flex")
                .style.justifyContent = "space-around";
        }
    </script>
</body>
</html>

支援的瀏覽器

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