HTML - DOM Style 物件 alignContent 屬性



HTML DOM Style 物件的 **alignContent** 屬性用於在彈性容器的專案未佔用所有可用空間時,在橫軸或縱軸上對齊這些專案。

語法

以下是獲取或設定 alignContent 屬性的語法。

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

屬性值

此屬性接受以下列出的值。

描述
stretch 這是預設的屬性值。它將專案拉伸以適應容器。
center 它將專案定位到容器的中心。
flex-start 它將專案定位到容器的開頭。
flex-end 它將專案定位到容器的結尾。
space-between 它在行之間放置專案並留出空間。
space-around 它在行之前、之間和之後放置專案並留出空間。
initial 用於將此屬性設定為其預設值。
inherit 用於繼承其父元素的屬性。

返回值

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

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

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

將值設定為 stretch 和 center

在此示例中,我們將 alignContent 值設定為 **stretch** 和 **center**。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>HTML DOM Style Object alignContent Property</title>
    <style>
        #align {
            width: 300px;
            height: 300px;
            border: 1px solid #000000;
            display: flex;
            flex-flow: row wrap;
            align-content: space-around;
        }
        #align div {
            width: 300px;
            height: 70px;
        }
    </style>
</head>
<body>
    <div id="align">
        <div style="background-color:rgb(158, 225, 118);"></div>
        <div style="background-color:rgb(119, 205, 234);"></div>
        <div style="background-color:rgb(211, 145, 219);"></div>
    </div>
    <p>Click button to change property.</p>
    <p id="id"></p>
    <button onclick="fun()">stretch</button>
    <button onclick="funtwo()">center</button>
    <script>
        function fun() {
            document.getElementById("align").style.alignContent = "stretch";
        }
        function funtwo() {
            document.getElementById("align").style.alignContent = "center";
        }
    </script>
</body>
</html>

將值設定為 flex-start 和 flex-end

在此示例中,我們將 alignContent 值設定為 **flex-start** 和 **flex-end**。

<!DOCTYPE html>
<html>
<head>
    <title>HTML DOM Style Object alignContent Property</title>
    <style>
        #align {
            width: 300px;
            height: 300px;
            border: 1px solid #000000;
            display: flex;
            flex-flow: row wrap;
            align-content: space-around;
        }
        #align div {
            width: 300px;
            height: 70px;
        }
    </style>
</head>
<body>
    <div id="align">
        <div style="background-color:rgb(158, 225, 118);"></div>
        <div style="background-color:rgb(119, 205, 234);"></div>
        <div style="background-color:rgb(211, 145, 219);"></div>
    </div>
    <p>Click button to change property.</p>
    <p id="id"></p>
    <button onclick="fun()">flex-start</button>
    <button onclick="funtwo()">flex-end</button>
    <script>
        function fun() {
            document.getElementById("align").style.alignContent = "flex-start";
        }
        function funtwo() {
            document.getElementById("align").style.alignContent = "flex-end";
        }
    </script>
</body>
</html>

將值設定為“space-between”和“space-around”

在此示例中,我們將 alignContent 值設定為 **space-between** 和 **space-around**。

<!DOCTYPE html>
<html>
<head>
    <title>HTML DOM Style Object alignContent Property</title>
    <style>
        #align {
            width: 300px;
            height: 400px;
            border: 1px solid #000000;
            display: flex;
            flex-flow: row wrap;
        }
        #align div {
            width: 300px;
            height: 70px;
        }
    </style>
</head>
<body>
    <div id="align">
        <div style="background-color:rgb(158, 225, 118);"></div>
        <div style="background-color:rgb(119, 205, 234);"></div>
        <div style="background-color:rgb(211, 145, 219);"></div>
    </div>
    <p>Click button to change property.</p>
    <p id="id"></p>
    <button onclick="fun()">Space Between</button>
    <button onclick="funtwo()">space-around</button>
    <script>
        function fun() {
            document.getElementById("align").style.alignContent = "space-between";
        }
        function funtwo() {
            document.getElementById("align").style.alignContent = "space-around";
        }
    </script>
</body>
</html>

支援的瀏覽器

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