title: Grid布局
categories: 前端
1. 强大的Grid布局
感谢MDNN和Codepen,授人鱼不如授人渔。
MDN
MDN
Codepen
2. 简单的认识
网格布局是一种二维网格布局系统(flex布局为一维布局系统),把一个元素作为网格容器只要让dispaly: grid
即可;网格容器是由一些水平和垂直的线构成的,每四条线构成一个轨道,也就是一个格子。格子与格子的距离叫做沟槽,个人认为也叫做margin
,沟槽有两种,水平方向格子与格子的距离(row-gap
),垂直方向格子与格子的距离(cloumn-gap
)。
2.1 grid-template-columns
grid-template-columns
定义了一个grid网格容器有多少列,所以应用在容器上,具体使用方式如下。
grid-template-rows属性
2.1.1 使用具体像素值定义列宽
body {
width: 90%;
max-width: 900px;
margin: 2em auto;
font: 0.9em/1.2 Arial, Helvetica, sans-serif;
}
.container {
/* 开启网格布局*/
dispaly: grid;
/* 定义3列,每一列的大小为200px, 多余的会自动换行,且无论网格如何缩小都有3列*/
grid-template-columns: 200px 200px 200px;
}
.container > div {
border-radius: 5px;
padding: 10px;
background-color: rgb(207, 232, 220);
border: 2px solid rgb(79, 185, 227);
}
<h1>Simple grid example</h1>
<div class="container">
<div>One</div>
<div>Two</div>
<div>Three</div>
<div>Four</div>
<div>Five</div>
<div>Six</div>
<div>Seven</div>
</div>
2.1.2 以fr为单位定义列宽
grid-template-columns: 1fr 1fr 1fr 1fr;
grid-template-columns: 2fr 2fr 1fr 1fr;
2.1.3 像素值与fr单位混合使用
.container {
display: grid;
/*第一列和最后一列宽度为200px, 中间列宽度为网格容器宽度-400px*/
grid-tempalte-columns: 200px 1fr 200px
}
- 使用repeat函数
.container {
display: grid;
/*重复3列,每一列大小一样*/
grid-template-coluns: repeat(3, 1fr)
/*重复3组,每一组中有2列,宽度为1比2*/
grid-template-coluns: repeat(3, 1fr 2fr)
}
2.1.4 使用repeat函数自适应列
.container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
/*最小宽度为100px,随着内容增大*/
grid-template-columns: repeat(auto-fit, minmax(100px, auto));
background-color: red;
}
3. grid-gap
指定格子之间的距离
.container {
/* 开启 */
display: grid;
grid-template-columns: 1fr 1fr 1fr;
background-color: red;
grid-gap: 20px; /*等于grid-row-gap:20px grid-column-gap:20px*/
}
4. minmax
定义轨道的最小值和最大值
.container {
display: grid;
/*最小宽度取最小的那个格子,最大宽度不超过200px*/
grid-template-columns: minmax(max-content, 300px) minmax(min-content, 200px) 150px
background-color: red;
grid-gap: 20px;
}
5. grid-column和grid-row
使用网格线放置元素, 指定竖线的起始未知和结束位置和横线的始末位置。也可以给网格线命名,然后直接使用网格线的名字定位元素。
grid-template-areas
grid-column
grid-row
<div class="wrapper">
<div class="box1">One</div>
<div class="box2">Two</div>
<div class="box3">Three</div>
<div class="box4">Four</div>
<div class="box5">Five</div>
</div>
.wrapper {
display: grid;
width: 90%;
ax-width: 900px;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: 50px;
}
.box1 {
grid-column-start: 1; /*列从第1条线开始第4线结束,可以看出是垮了整个轨道*/
grid-column-end: 4;
grid-row-start: 1; /*行从第1条线开始第3线结束*/
grid-row-end: 3;
/*
等价于 (grid-column: 1/4 和 grid-row: 1/3)
或者 (grid-column: 1/-1 和 grid-row: 1/3)
或者 grid-column: 1/ span 3 span表示跨越的轨道数
*/
/*
* 或者等价于 grid-area: 1 / 1 / 4 / 3
* row-start/column-statrt/row-end/column-end
*/
}
.box2 {
grid-column-start: 1;
/*
* 当没有指明结束线时默认延申一个轨道,即end = start + 1
* 即gird-column-end: 2
*/
grid-row-start: 3;
grid-row-end: 5;
/*
* 反向跨越
* grid-row-start: span 2
* grid-row-end: 5
*/
}
div {
background-color: rgb(207, 232, 220);
border: 2px solid rgb(79, 185, 227);
}
6. 模板区域
<div class="wrapper">
<div class="header">Header</div>
<div class="sidebar">Sidebar</div>
<div class="content">Content</div>
<div class="footer">Footer</div>
</div>
.wrapper {
display: grid;
/*
* 指定9列,与grid-template-areas相对应, 简单来说一行对应9个单词
*/
grid-template-columns: repeat(9, 1fr); /*9列*/
grid-auto-rows: minmax(100px, auto);
/*
* 网格容器使用grid-template-areas
*/
grid-template-areas:
"hd hd hd hd hd hd hd hd hd"
"sd sd sd main main main main main main"
"ft ft ft ft ft ft ft ft ft";
}
.header {
grid-area: hd;
}
.footer {
grid-area: ft;
}
.content {
grid-area: main;
}
.sidebar {
grid-area: sd;
}
6.1 流出空白区域
.wrapper {
display: grid;
grid-template-columns: repeat(9, 1fr);
grid-auto-rows: minmax(100px, auto);
/*
* 流出空白的区域:使用点代替
*/
grid-template-areas:
"hd hd hd hd hd hd hd hd hd"
"sd sd sd main main main main main main"
". . . ft ft ft ft ft ft";
}
6.2 使用媒体查询改变布局
/*
* 当媒体宽度大于500px时的布局样式
*
*/
@media (min-width: 500px) {
.wrapper {
grid-template-columns: repeat(9, 1fr);
grid-template-areas:
"hd hd hd hd hd hd hd hd hd"
"sd sd sd main main main main main main"
"sd sd sd ft ft ft ft ft ft";
}
}
/*
* 当媒体的宽度大于700时的布局样式
*/
@media (min-width: 700px) {
.wrapper {
grid-template-areas:
"hd hd hd hd hd hd hd hd hd"
"sd sd main main main main main ft ft";
}
}
6.3 网格简写规则
grid-template
可以同时设置三个属性: grid-template-rows
grid-template-columns
grid-template-areas
.wrapper {
display: grid;
grid-template:
/*grid-template-areas*/
"hd hd hd hd hd hd hd hd hd" minmax(100px, auto) /*grid-template-columns*/
"sd sd sd main main main main main main" minmax(100px, auto) /*三行表示 grid-template-rows*/
"ft ft ft ft ft ft ft ft ft" minmax(100px, auto)
/ 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr ; /*轨道清单*/
}
grid
表示以下属性的简写
7. 自动定位
7.1 grid-auto-rows
<div class="wrapper">
<div>One</div>
<div>Two</div>
<div>Three</div>
<div>Four</div>
<div>Five</div>
<div>Six</div>
<div>Seven</div>
<div>Eight</div>
</div>
.wrapper {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
/*自动增长,每行的宽度是100px*/
grid-auto-rows: 100px;
grid-auto-rows: 100px;
}
div {
background-color: rgb(207, 232, 220);
border: 2px solid rgb(79, 185, 227);
}
7.2 结合minmax
<div class="wrapper">
<div>One</div>
<div>Two</div>
<div>Three</div>
<div>Four
<br>This cell
<br>Has extra
<br>content.
<br>Max is auto
<br>so the row expands.
</div>
<div>Five</div>
</div>
.wrapper {
display: grid;
width: 500px;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
/* 每一行最小100px, 延申的时候自动*/
grid-auto-rows: minmax(100px, auto)
}
div {
background-color: rgb(207, 232, 220);
border: 2px solid rgb(79, 185, 227);
}
7.3 明确行大小
<div class="wrapper">
<div>One</div>
<div>Two</div>
<div>Three</div>
<div>Four</div>
<div>Five</div>
<div>Six</div>
<div>Seven</div>
<div>Eight</div>
<div>Nine</div>
<div>Ten</div>
</div>
.wrapper {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
grid-auto-rows: 50px 100px;
width: 600px;
margin: 0 auto
}
div {
background-color: rgb(207, 232, 220);
border: 2px solid rgb(79, 185, 227);
}
7.4 列方向优先占位
<div class="wrapper">
<div>One</div>
<div>Two</div>
<div>Three</div>
<div>Four</div>
<div>Five</div>
<div>Six</div>
<div>Seven</div>
<div>Eight</div>
</div>
.wrapper {
display: grid;
gap: 10px;
/*3行,每一行100px*/
grid-template-rows: repeat(3, 100px);
/*让排列方向改变为垂直方向*/
grid-auto-flow: column;
/*列按照这样的方式自动增长*/
grid-auto-columns: 300px 100px;
margin: 0 auto;
width: 720px
}
div {
background-color: rgb(207, 232, 220);
border: 2px solid rgb(79, 185, 227);
}
7.5 含有定位属性的项目
<div class="wrapper">
<div>One</div>
<div>Two</div>
<div>Three</div>
<div>Four</div>
<div>Five</div>
<div>Six</div>
<div>Seven</div>
<div>Eight</div>
<div>Nine</div>
<div>Ten</div>
<div>Eleven</div>
<div>Twelve</div>
</div>
.wrapper {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-auto-rows: 100px;
gap: 10px;
}
.wrapper div:nth-child(2) {
/* 定位第2个元素*/
grid-column: 3;
grid-row: 2 / 4;
}
.wrapper div:nth-child(5) {
/*定位第5个元素*/
grid-column: 1 / 3;
grid-row: 1 / 3;
}
/*剩下的元素自动占满其他剩余的空间*/
7.5 排列不下
<div class="wrapper">
<div>One</div>
<div>Two</div>
<div>Three</div>
<div>Four</div>
<div>Five</div>
<div>Six</div>
<div>Seven</div>
<div>Eight</div>
<div>Nine</div>
<div>Ten</div>
<div>Eleven</div>
<div>Twelve</div>
</div>
.wrapper {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-auto-rows: 50px;
gap: 10px;
width: 900px;
margin: 0 auto
}
.wrapper div:nth-child(4n+1) {
grid-column-end: span 2;
grid-row-end: span 2;
background-color: #ffa94d;
}
.wrapper div:nth-child(2) {
grid-column: 3;
grid-row: 2 / 4;
}
.wrapper div:nth-child(5) {
grid-column: 1 / 3;
grid-row: 1 / 3;
}
div {
background-color: rgb(207, 232, 220);
border: 2px solid rgb(79, 185, 227);
}
7.6 grid-auto-flow: dense
加上这个属性上面的图就会变成下面的图。
8. 对其方向
块轴
行轴
8.1 align-items和algin-self
<div class="wrapper">
<div class="item1">Item 1</div>
<div class="item2">Item 2</div>
<div class="item3">Item 3</div>
<div class="item4">Item 4</div>
</div>
.wrapper {
display: grid;
grid-template-columns: repeat(8, 1fr);
gap: 10px;
grid-auto-rows: 100px;
grid-template-areas:
"a a a a b b b b"
"a a a a b b b b"
"c c c c d d d d"
"c c c c d d d d";
align-items: strentch;
}
.item1 {
grid-area: a;
/*默认值*/
algin-self: strentch;
}
.item2 {
grid-area: b;
algin-self: start
}
.item3 {
grid-area: c;
align-self: end
}
.item4 {
grid-area: d;
align-self: center
}
8.2 justify-items和justify-self
<div class="wrapper">
<div class="item1">Item 1</div>
<div class="item2">Item 2</div>
<div class="item3">Item 3</div>
<div class="item4">Item 4</div>
</div>
.wrapper {
display: grid;
width: 400px;
grid-template-columns: repeat(8, 1fr);
gap: 10px;
grid-auto-rows: 80px;
grid-template-areas:
"a a a a b b b b"
"a a a a b b b b"
"c c c c d d d d"
"c c c c d d d d";
background-color: #ffa94d;
margin: 0 auto;
}
.item1 {
grid-area: a;
}
.item2 {
grid-area: b;
justify-self: start;
}
.item3 {
grid-area: c;
justify-self: end;
}
.item4 {
grid-area: d;
justify-self: center;
}
div {
background-color: rgb(207, 232, 220);
border: 2px solid rgb(79, 185, 227);
}
8.3 对齐网格到块轴
8.3.1 align-content
align-content - CSS:层叠样式表 | MDN (mozilla.org)
<div class="wrapper">
<div class="item1">Item 1</div>
<div class="item2">Item 2</div>
<div class="item3">Item 3</div>
<div class="item4">Item 4</div>
</div>
.wrapper {
display: grid;
grid-template-columns: repeat(3, 100px);
grid-template-rows: repeat(3, 100px);
height: 500px;
width: 500px;
gap: 10px;
grid-template-areas:
"a a b"
"a a b"
"c d d";
background-color: #ffa94d;
//space-around表示边上的margin-top等于元素之间的margin-top的一半
align-content: space-around; /*space-between, center*/
margin: 0 auto
}
.item1 {
grid-area: a;
align-self
}
.item2 {
grid-area: b;
}
.item3 {
grid-area: c;
}
.item4 {
grid-area: d;
}
div {
background-color: rgb(207, 232, 220);
border: 2px solid rgb(79, 185, 227);
}
8.4 对齐网格到行轴
<div class="wrapper">
<div class="item1">Item 1</div>
<div class="item2">Item 2</div>
<div class="item3">Item 3</div>
<div class="item4">Item 4</div>
</div>
.wrapper {
display: grid;
grid-template-columns: repeat(3, 100px);
grid-template-rows: repeat(3, 100px);
height: 500px;
width: 500px;
gap: 10px;
grid-template-areas:
"a a b"
"a a b"
"c d d";
background-color: #ffa94d;
justify-content: space-around;/*end, center, start...*/
margin: 0 auto
}
.item1 {
grid-area: a;
align-self
}
.item2 {
grid-area: b;
}
.item3 {
grid-area: c;
}
.item4 {
grid-area: d;
}
div {
background-color: rgb(207, 232, 220);
border: 2px solid rgb(79, 185, 227);
}
8.5 自动对齐边距
margin: 0 auto
这是之前的剧中, 在网格中对item元素使用margin-left: auto
, 在分配内容之后剩余的距离都算为它的值。
9. 书写模式
也就是文字方向,中文一般是从左向右写,或者从上完下写,没有从右往左和从下到上,但是别的国家不一定是这种规则。
writing-mode - CSS:层叠样式表 | MDN (mozilla.org)
<div class="wrapper">
<div class="item1">你好</div>
<div class="item2">xjplt</div>
<div class="item3">Item 3</div>
<div class="item4">Item 4</div>
<div class="item5">Item 5</div>
</div>
.wrapper {
display: grid;
grid-template-columns: repeat(3, 100px);
grid-template-rows: repeat(2, 100px);
grid-gap: 10px;
writing-mode: vertical-lr;
margin: 0 auto
}
div {
background-color: rgb(207, 232, 220);
border: 2px solid rgb(79, 185, 227);
}