网格布局中 justify-items 和 align-litems

justify-itemsalign-litems 是从整体上调整 网格项(grid items)中的内容网格区域(grid area) 中的位置的。
也可以单独调整 某个网格项(grid items)中的内容,在这个网格项上使用 justify-selfalign-self 属性。

justify-items

网格容器( grid container) 的属性,设置 网格项(grid items)中的内容(the content within grid items) 沿着行轴线(水平方向)上的对齐方式,适用于容器内的所有网格项。

如果要单独调整 某个网格项中的内容 沿着行轴线(水平方向)上的对齐方式,则可以在 这个网格项 上使用 justify-self 属性。

值:

  • start: 将 网格项中的内容 对齐到其网格区域(grid area)行轴线的起始边缘(Justifies content of grid items with the starting edge of the grid area along the row-axis)
    在这里插入图片描述
  • end: 将 网格项中的内容 对齐到其网格区域(grid area)行轴线的结束边缘(Justifies content of grid items with the ending edge of the grid area along the row-axis)
    在这里插入图片描述
  • center: 将网格项中的内容对齐到其网格区域(grid area)行轴线的中心位置(Justifies content of grid items in the center of the grid area along the row-axis)
    在这里插入图片描述
  • stretch: 默认值,将 网格项中的内容 撑满其网格区域(grid area)(This is the default value. Fills up the width of the grid area.)
    在这里插入图片描述

align-items

网格容器( grid container) 的属性,设置 网格项(grid items)中的内容(the content within grid items) 沿着列轴线(垂直方向)上的对齐方式,适用于容器内的所有网格项。

如果要单独调整 某个网格项中的内容 沿着列轴线(垂直方向)上的对齐方式,则可以在 这个网格项 上使用 align-self 属性。

值:

  • start: 将 网格项中的内容 对齐到其网格区域(grid area)列轴线的起始边缘(Aligns content of grid items with the starting edge of the grid area along the row-axis)
    在这里插入图片描述
  • end: 将 网格项中的内容 对齐到其网格区域(grid area)列轴线的结束边缘(Aligns content of grid items with the ending edge of the grid area along the row-axis)
    在这里插入图片描述
  • center: 将网格项中的内容对齐到其网格区域(grid area)列轴线的中心位置(Aligns content of grid items in the center of the grid area along the row-axis)
    在这里插入图片描述
  • stretch: 默认值,将 网格项中的内容 撑满其网格区域(grid area)(This is the default value. Fills up the width of the grid area.)
    在这里插入图片描述
    举个栗子:

html主要代码:

    <div class="wrapper">
        <div class="box a">
            <p>This is box A. </p>
        </div>

        <div class="box b">
            <p>This is box B.</p>
        </div>

        <div class="box c">
            <p>This is box C.</p>
        </div>

        <div class="box d">
            <p>This is box D.</p>
        </div>
        
        <div class="box e">
            <p>Each of the boxes on the left has a grid area of 2 columns and 2 rows </p>
            <p>The justify-items property is used to justify the content inside each grid-area.</p>
            <p>Other values of justify-items are:</p>
            <ul>
                <li>stretch</li>
                <li>start</li>
                <li>end</li>
                <li>center</li>
            </ul>
        </div>
    </div>

css主要代码:

        .wrapper {
            display: grid;
            /* 网格项中内容的对齐方式 */
            justify-items: start;
            grid-gap: 10px;
            grid-template-columns: repeat(6, 150px);
            grid-template-rows: repeat(4, 150px);
        }

        .a {
            grid-column: 1 / 3;
            grid-row: 1 / 3;
            background: rgba(232, 140, 217, 0.5);
        }

        .b {
            grid-column: 3 / 5;
            grid-row: 1 / 3;
            background:rgba(65, 193, 12, 0.5);
        }

        .c {
            grid-column: 1 / 3;
            grid-row: 3 / 5;
            background:rgba(151, 183, 241, 0.5);
        }

        .d {
            grid-column: 3 / 5;
            grid-row: 3 /5;
            background:rgba(232, 187, 140, 0.5);
        }

        .e {
            grid-column: 5 / 7;
            grid-row: 1 / 5;
            /* 网格项也可以用 align-self 属性单独调整自己列轴线(垂直方向)上的对齐方式 */
            align-self: start;
            background:#5ecfda3d;
        }

最终效果:
在这里插入图片描述
完整代码也贴出来:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>grid demo</title>
    <style>
        * {
            padding: 0;
            margin: 0;
            font-size: 20px;
            color: #000;
        }

        ul{
            margin-left: 40px;
        }

        .wrapper {
            background: no-repeat url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/12005/grid.png);
        }

        .box {
            border: 1px solid #444;
        }

        .wrapper {
            display: grid;
            /* 网格项中内容的对齐方式 */
            justify-items: start;
            grid-gap: 10px;
            grid-template-columns: repeat(6, 150px);
            grid-template-rows: repeat(4, 150px);
        }

        .a {
            grid-column: 1 / 3;
            grid-row: 1 / 3;
            background: rgba(232, 140, 217, 0.5);
        }

        .b {
            grid-column: 3 / 5;
            grid-row: 1 / 3;
            background:rgba(65, 193, 12, 0.5);
        }

        .c {
            grid-column: 1 / 3;
            grid-row: 3 / 5;
            background:rgba(151, 183, 241, 0.5);
        }

        .d {
            grid-column: 3 / 5;
            grid-row: 3 /5;
            background:rgba(232, 187, 140, 0.5);
        }

        .e {
            grid-column: 5 / 7;
            grid-row: 1 / 5;
            /* 网格项也可以用 align-self 属性单独调整自己列轴线(垂直方向)上的对齐方式 */
            align-self: start;
            background:#5ecfda3d;
        }
    </style>
</head>
<body>
    <div class="wrapper">
        <div class="box a">
            <p>This is box A. </p>
        </div>

        <div class="box b">
            <p>This is box B.</p>
        </div>

        <div class="box c">
            <p>This is box C.</p>
        </div>

        <div class="box d">
            <p>This is box D.</p>
        </div>
        
        <div class="box e">
            <p>Each of the boxes on the left has a grid area of 2 columns and 2 rows </p>
            <p>The justify-items property is used to justify the content inside each grid-area.</p>
            <p>Other values of justify-items are:</p>
            <ul>
                <li>stretch</li>
                <li>start</li>
                <li>end</li>
                <li>center</li>
            </ul>
        </div>
    </div>
</body>
</html>
  • 4
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值