HTML - DOM样式对象alignItems属性



HTML DOM 样式对象**alignItems**属性用于设置灵活容器内项目的默认对齐方式。

语法

以下是获取或设置 alignItems 属性的语法。

设置 alignItems 属性
object.style.alignItems = "stretch | center | flex-start | flex-end | baseline | initial | inherit";
获取 alignItems 属性
object.style.alignItems;

属性值

此属性接受以下值。

描述
stretch 这是默认属性值。它会拉伸项目以适应容器。
center 它将项目定位在容器的中心。
flex-start 它将项目定位在容器的开头。
flex-end 它将项目定位在容器的结尾。
baseline 它将项目定位在容器的基线。
initial 它用于将此属性设置为其默认值。
inherit 它用于继承其父元素的属性。

返回值

它返回一个字符串值,表示元素的 align-items 属性。

HTML DOM 样式对象“alignItems”属性示例

以下示例说明了该属性的不同值。

将值设置为 stretch 和 center

在此示例中,我们将 alignItems 值设置为**stretch**和**center**。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>HTML DOM Style Object alignItems Property</title>
    <style>
        div#align {
            width: 300px;
            height: 300px;
            border: 1px solid #000000;
            display: flex;
        }
    </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);">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("align").style.alignItems = "stretch";
        }
        function funtwo() {
            document.getElementById("align").style.alignItems = "center";
        }
    </script>
</body>
</html>

将值设置为 flex-start 和 flex-end

在此示例中,我们将 alignItems 值设置为**flex-start**和**flex-end**。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>HTML DOM Style Object alignItems Property</title>
    <style>
        div#align {
            width: 300px;
            height: 300px;
            border: 1px solid #000000;
            display: flex;
        }
    </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);">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("align").style.alignItems = "flex-start";
        }
        function funtwo() {
            document.getElementById("align").style.alignItems = "flex-end";
        }
    </script>
</body>
</html>

将值设置为 baseline

在此示例中,我们将 alignItems 值设置为**baseline**。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>HTML DOM Style Object alignItems Property</title>
    <style>
        div#align {
            width: 300px;
            height: 300px;
            border: 1px solid #000000;
            display: flex;
        }
    </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);">Point</div>
    </div>
    <p>Click button to change property.</p>
    <p id="id"></p>
    <button onclick="fun()">baseline</button>
    <script>
        function fun() {
            document.getElementById("align").style.alignItems = "baseline";
        }
    </script>
</body>
</html>

支持的浏览器

属性 Chrome Edge Firefox Safari Opera
alignItems 是 29 是 12 是 20 是 9 是 12.1
html_dom_style_object_reference.htm
广告
© . All rights reserved.