HTML - DOM Style 物件 alignSelf 屬性



HTML DOM Style 物件**alignSelf**屬性用於設定彈性容器內專案的預設對齊方式。

語法

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

獲取 alignSelf 屬性
object.style.alignSelf = "auto | stretch | center | flex-start | flex-end | baseline | initial | inherit";
設定 alignSelf 屬性
object.style.alignSelf;

屬性值

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

描述
auto 這是預設的屬性值。在此元素繼承其父容器的 align-items 屬性,或者如果它沒有父容器,則將元素設定為“stretch”。
stretch 這是預設的屬性值。它將專案拉伸以適應容器。
center 它將專案定位到容器的中心。
flex-start 它將專案定位在容器的開頭。
flex-end 它將專案定位在容器的結尾。
baseline 它將專案定位在容器的基線。
initial 它用於將此屬性設定為其預設值。
inherit 它用於繼承其父元素的屬性。

返回值

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

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

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

將值設定為 stretch 和 center

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

<!DOCTYPE html>
<html lang="en">
<head>
    <title>HTML DOM Style Object alignSelf Property</title>
    <style>
        #align {
            width: 300px;
            height: 300px;
            border: 1px solid #000000;
            display: flex;
        }

        #point {
            align-self: center;
        }
    </style>
</head>
<body>
    <div id="align">
        <div style="background-color:rgb(158, 225, 118);">Welcome</div>
        <div style="background-color:rgb(119, 205, 234);">to Tutorials</div>
        <div style="background-color:rgb(211, 145, 219);" id="point">Point</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("point").style.alignSelf = "stretch";
        }
        function funtwo() {
            document.getElementById("point").style.alignSelf = "center";
        }
    </script>
</body>
</html>

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

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

<!DOCTYPE html>
<html lang="en">
<head>
    <title>HTML DOM Style Object alignSelf Property</title>
    <style>
        #align {
            width: 300px;
            height: 300px;
            border: 1px solid #000000;
            display: flex;
        }

        #point {
            align-self: center;
        }
    </style>
</head>
<body>
    <div id="align">
        <div style="background-color:rgb(158, 225, 118);">Welcome</div>
        <div style="background-color:rgb(119, 205, 234);">to Tutorials</div>
        <div style="background-color:rgb(211, 145, 219);" id="point">Point</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("point").style.alignSelf = "flex-start";
        }
        function funtwo() {
            document.getElementById("point").style.alignSelf = "flex-end";
        }
    </script>
</body>
</html>

將值設定為 auto 和 baseline

在此示例中,我們將 alignSelf 值設定為**auto**和**baseline**。

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

支援的瀏覽器

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