1️⃣ 容器属性
2️⃣ 设置网格容器
3️⃣ display 设置网格容器
.box {// 块元素容器display: grid;// 行元素容器display: inline-grid;}
2️⃣ 设置行列
3️⃣ grid-template-columns 设置列
// 固定值
// 设置三列每列固定高度 100px
grid-template-columns: 100px 100px 100px;
// 百分比
// 设置三列每列为容器高度的 33.3%
grid-template-columns: 33.3% 33.3% 33.3%;
// fr 关键字,fr 表示比例关系
// 设置三列并将容器高度等分十份,三列高度分别为 3份 4份 3份( 等分数量根据 fr 的数量而定 )
grid-template-columns: 3fr 4fr 3fr;
// fr 可以与绝对长度的单位结合使用,这时会非常方便,第一列100px 第二三列各占用剩余空间的一半
grid-template-columns: 100px 5fr 5fr;
// auto 关键字,auto 表示由浏览器自己决定长度
// 设置三列第一列和第三列的高度为固定值 100px,第二列的高度为容器剩下的可用空间高度( auto 为自适应高度 )
grid-template-columns: 100px auto 100px;
// repeat()函数,repeat 接收两个参数,第一个是重复的次数,第二个参数是所要重复的值。
// 设置三列,每列为 1fr
grid-template-columns: repeat(3, 1fr);
// 设置六列分别为 1fr 2fr 1fr 2fr 1fr 2fr
grid-template-columns: repeat(3, 1fr 2fr);
// auto-fill 关键字,auto-fill 表示每列 100px,容器能生成几列就生成几列,直到容器不能放置更多的列
grid-template-columns: repeat(auto-fill, 100px);
// minmax() 函数,minmax 函数产生一个长度范围,表示长度就在这个范围之中。它接受两个参数,分别为最小值和最大值。
grid-template-columns: 100px 100px minmax(100px, 1fr);
// 网格线的名称
// 代码指定网格布局为3行 x 3列,因此有4根垂直网格线和4根水平网格线。方括号里面依次是这八根线的名字
grid-template-columns: [c1] 1fr [c2] 1fr [c3] 1fr [c4];
grid-template-rows: [r1] 1fr [r2] 1fr [r3] 1fr [r4];
3️⃣ grid-template-rows 设置行
设置行的方法与列方法相同
2️⃣ 设置行列间距
根据最新标准,三个属性名的 grid-前缀已经删除,grid-column-gap 和 grid-row-gap 写成 column-gap 和 row-gap,grid-gap 写成 gap。
3️⃣ row-gap 行间距
// 设置行间距为 100px
row-gap: 100px;
// 设置行间距为容器高度的 10%
row-gap: 10%;
3️⃣ column-gap 列间距
列间距设置与行间距方法相同
3️⃣ gap 行列间距简写
// gap: <row-gap> <column-gap>;
// 设置行间距为 10px 列间距为 20px
gap: 10px 20px;
// 设置行间距和列间距均为 20px ( 省略第二个值默认等于第一个值 )
gap: 20px;
2️⃣ 设置网格横排还是竖排
3️⃣ grid-auto-flow 网格的排列规则
划分网格以后,容器的子元素会按照顺序,自动放置在每一个网格。默认的放置顺序是”先行后列”,即先填满第一行,再开始放入第二行。 这个顺序由 grid-auto-flow 属性决定,默认值是 row,即”先行后列”。也可以将它设成 column,变成”先列后行”。
grid-auto-flow: column;

grid-auto-flow属性除了设置成row和column,还可以设成row dense和column dense。这两个值主要用于,某些项目指定位置以后,剩下的项目怎么自动放置。
grid-auto-flow: row dense; 设为row dense,表示”先行后列”,并且尽可能紧密填满,尽量不出现空格。
grid-auto-flow: column dense; 如果将设置改为column dense,表示”先列后行”,并且尽量填满空格。
2️⃣ 单元格内容的对齐方式 - 控制所有单个单元格
3️⃣ justify-items 水平方向对齐
// justify-items: start | end | center | stretch;
// start:对齐单元格的起始边缘。
justify-items: start;
// end:对齐单元格的结束边缘。
justify-items: end;
// center:单元格内部居中。
justify-items: center;
// stretch:拉伸,占满单元格的整个宽度(默认值)。
justify-items: stretch;
3️⃣ align-items 垂直方向对齐
// align-items: start | end | center | stretch;
// 垂直对齐方式与水平对齐方式的使用方法相同
3️⃣ place-items 水平垂直对齐的简写
// place-items: <align-items> <justify-items>;
// 单元格内容垂直水平居中
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(3, 1fr);
place-items: center center;
2️⃣ 整个内容区域在容器里的对齐方式 - 控制整列或整行
3️⃣ justify-content 水平位置
// justify-content: start | end | center | stretch | space-around | space-between | space-evenly;
// start - 对齐容器的起始边框
justify-content: start;
// end - 对齐容器的结束边框
justify-content: end;
// center - 容器内部居中
justify-content: center;
// stretch - 项目大小没有指定时,拉伸占据整个网格容器 ( 没指定大小,每一列等分容器,如果指定了大小,效果等同于 space-evenly )
justify-content: stretch;
// space-around - 每个项目两侧的间隔相等。所以,项目之间的间隔比项目与容器边框的间隔大一倍( 每一列的左右 margin 相等 )
justify-content: space-around;
// space-between - 项目与项目的间隔相等,项目与容器边框之间没有间隔( 列居中靠边排列 )
justify-content: space-between;
// space-evenly - 项目与项目的间隔相等,项目与容器边框之间也是同样长度的间隔 ( 项目与项目,项目与容器边缘的间隔均相等 )
justify-content: space-evenly;
3️⃣ align-content 垂直位置
// align-content: start | end | center | stretch | space-around | space-between | space-evenly;
// 使用方法与 justify-content 相同
3️⃣ place-content 水平垂直位置的简写
// place-content: <align-content> <justify-content>
grid-template-columns: repeat(3, 100px);
grid-template-rows: repeat(3, 100px);
place-content: center space-evenly;
2️⃣ 为网格布局指定区域
3️⃣ grid-template-areas 为网格命名
网格布局允许指定”区域”(area),一个区域由单个或多个单元格组成。grid-template-areas 属性用于定义区域。 区域名可以任意起名
// 代码先划分出9个单元格,然后将其定名为a到i的九个区域,分别对应这九个单元格。
grid-template-areas: 'a b c'
'd e f'
'g h i';
// 多个单元格合并成一个区域的写法如下。
grid-template-areas: 'a a a'
'b b b'
'c c c';
// 如果某些区域不需要利用,则使用"点"(.)表示。
grid-template-areas: 'a . c'
'd . f'
'g . i';
// 注意,区域的命名会影响到网格线。每个区域的起始网格线,会自动命名为区域名-start,终止网格线自动命名为区域名-end。
// 比如,区域名为header,则起始位置的水平网格线和垂直网格线叫做header-start,终止位置的水平网格线和垂直网格线叫做header-end。
2️⃣ 自动创建的多余网格的列宽和行高
3️⃣ grid-auto-columns 自动创建的列宽
3️⃣ grid-auto-rows 自动创建的行高
grid-auto-columns属性和grid-auto-rows属性用来设置,浏览器自动创建的多余网格的列宽和行高。它们的写法与 grid-template-columns和grid-template-rows完全相同。如果不指定这两个属性,浏览器完全根据单元格内容的大小,决定新增网格的列宽和行高。
grid-template-columns: 100px 100px 100px;
grid-template-rows: 100px 100px 100px;
grid-auto-rows: 50px; // 3 行之外自动创建的行高为 50px
grid-auto-columns: 50px; // 3 列之外自动创建的列为 50px
1️⃣ 项目属性
2️⃣ 定义项目的四个边框所在网格线
项目的位置是可以指定的,具体方法就是指定项目的四个边框,分别定位在哪根网格线
3️⃣ grid-column-start 左边框所在的垂直网格线
3️⃣ grid-column-end 右边框所在的垂直网格线
3️⃣ grid-row-start 上边框所在的水平网格线
3️⃣ grid-row-end 下边框所在的水平网格线
grid-template-areas: 'a b c'
'd e f'
'g h i';
// 使用网格线数字
div:nth-child(1) {
grid-column-start: 2;
grid-column-end: 4;
grid-row-start: 2;
grid-row-end: 3;
background-color: #eccc68;
}
// 使用网格线名字
div:nth-child(1) {
grid-column-start: e-start;
grid-column-end: f-end;
grid-row-start: e-start;
grid-row-end: e-end;
background-color: #eccc68;
}
// 简写形式
div:nth-child(1) {
grid-column: e-start/f-end;
grid-row: e-start/e-end;
background-color: #eccc68;
}
2️⃣ 指定项目放在哪一个区域
3️⃣ grid-area 属性指定项目放在哪一个区域。
grid-template-areas: 'a b c'
'd e e'
'g h i';
div:nth-child(1) {
grid-area: e;
background-color: #eccc68;
}
2️⃣ 设置单元格内容的对齐方式 - 控制单个单元格
3️⃣ justify-self 水平位置
justify-self属性设置单元格内容的水平位置(左中右),跟justify-items属性的用法完全一致,但只作用于单个项目。
justify-self: start | end | center | stretch;
// start:对齐单元格的起始边缘
justify-self: start;
// end:对齐单元格的结束边缘
justify-self: end;
// center:单元格内部居中
justify-self: center;
// stretch:拉伸,占满单元格的整个宽度(默认值)
justify-self: stretch;
3️⃣ align-self 垂直位置
align-self属性设置单元格内容的垂直位置(上中下),跟align-items属性的用法完全一致,也是只作用于单个项目。
// align-self: start | end | center | stretch;
// align-self 的使用方法和 justify-self 相同
3️⃣ place-self 水平垂直的简写
place-self 属性是 align-self 属性和 justify-self 属性的合并简写形式。
place-self: <align-self> <justify-self>;
div:nth-child(5) {
place-self: center center;
background-color: #ffa502;
}
1️⃣ 测试代码
// html
<body>
<div class="box">
<div><p>1</p></div>
<div><p>2</p></div>
<div><p>3</p></div>
<div><p>4</p></div>
<div><p>5</p></div>
<div><p>6</p></div>
<div><p>7</p></div>
<div><p>8</p></div>
<div><p>9</p></div>
</div>
</body>
// less
.box {
margin: 50px;
width: 400px;
height: 400px;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(3, 1fr);
border: 1px solid black;
>div {
width: 100px;
height: 100px;
p {
width: 20px;
height: 20px;
margin: 0;
color: #fff;
text-align: center;
line-height: 20px;
background-color: #000;
}
}
div:nth-child(1) {
background-color: #eccc68;
}
div:nth-child(2) {
background-color: #ff7f50;
}
div:nth-child(3) {
background-color: #ff6b81;
}
div:nth-child(4) {
background-color: #f12ef8;
}
div:nth-child(5) {
background-color: #ffa502;
}
div:nth-child(6) {
background-color: #ff6348;
}
div:nth-child(7) {
background-color: #ff4757;
}
div:nth-child(8) {
background-color: #7bed9f;
}
div:nth-child(9) {
background-color: #70a1ff;
}
}
