zl程序教程

您现在的位置是:首页 >  其他

当前栏目

grid项目属性之grid-area&justify-self/align-self

amp属性项目 Grid self Area Align
2023-09-11 14:19:18 时间

简介

  1. 使用grid-area一般搭配grid-template-areas一起使用,详情点击,并搜索在网格布局中自定义摆放元素
  2. justify-self/align-self一般用于项目在容器内的排放位置

例子

grid-area

  • 这里指介绍作为简写的grid-area,非简写形式的使用我们可以看简介中的文章
  • 作为先写形式,他是grid-row-startgrid-row-endgrid-column-startgrid-column-end的合并简写,语法如下
grid-area:<grid-row-start> / <grid-column-start> / <grid-row-end> / <grid-column-end>

例子

<body>
    <style>
        * {
            list-style: none;
            margin: 0;
            padding: 0;
        }

        ul {
            width: 600px;
            height: 600px;
            border: 3px solid rebeccapurple;
            font-weight: bold;
            color: white;
            text-align: center;
            line-height: 100px;
            font-size: 40px
        }

        li:nth-child(even) {
            background-color: red;
        }

        li:nth-child(odd) {
            background-color: #000000;
        }

        ul{
            display: grid;
            grid-template-rows: repeat(3,1fr);
            grid-template-columns: repeat(3,1fr);
            gap: 20px 10px;
        }
        li:nth-child(1) {
            grid-area: 1/1/3/3;
        }

    </style>
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <li>5</li>
        <li>6</li>
    </ul>
</body>

效果如下

在这里插入图片描述
根据grid-area属性值,我们就可以看到项目序号为1的盒子占据了4个空间

justify-self/align-self

  • 他们的属性值可取如下start | end | center | stretch
  • justify-self用于调整水平方向上面的布局,如果用上了当前的属性,那么水平方向上的
  • align-self用于调整垂直方向上面的布局
  • 项目只要用上了这个属性,那么项目的大小就是有内容撑开,或者自定义的大小
  • 这两个有一个单独的缩写属性就是

start

ul{
    display: grid;
    grid-template-rows: repeat(3,1fr);
    grid-template-columns: repeat(3,1fr);
    gap: 20px 10px;
}

li:nth-child(1) {
    justify-self: start;
    align-self: start;
}

在这里插入图片描述

end

ul{
    display: grid;
    grid-template-rows: repeat(3,1fr);
    grid-template-columns: repeat(3,1fr);
    gap: 20px 10px;
}

li:nth-child(1) {
    justify-self: end;
    align-self: end;
}

在这里插入图片描述

center

ul{
    display: grid;
    grid-template-rows: repeat(3,1fr);
    grid-template-columns: repeat(3,1fr);
    gap: 20px 10px;
}

li:nth-child(1) {
    justify-self: center;
    align-self: center;
}

在这里插入图片描述

stretch

ul{
    display: grid;
    grid-template-rows: repeat(3,1fr);
    grid-template-columns: repeat(3,1fr);
    gap: 20px 10px;
}

li:nth-child(1) {
    /* 水平方向拉伸 */
    justify-self: stretch; 
    /* 垂直方向拉伸 */
    align-self: center;
}

在这里插入图片描述

placeself

这个属于justify-self和align-self的缩写,他的摆放是先水平,然后再垂直

li:nth-child(1) {
    /* 水平方向拉伸 */
    justify-self: stretch; 
    /* 垂直方向拉伸 */
    align-self: center;
}

可以写成

li:nth-child(1) {
    place-self: stretch stretch;
}