Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:grid有毒, 用了就回不去了
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<style>
body {
padding: 10px;
}
.container {
display: grid;
/* 这下面的100px可以用百分比或者1fr来表示,也是同样的效果 */
grid-template-columns: 100px 100px 100px;
grid-template-rows: 100px 100px 100px;
}
.box {
background-color: #444;
color: #fff;
font-size: 150%;
padding: 20px;
}
.container :nth-of-type(2n) {
background-color: red;
}
</style>
<body>
<div class="container">
<div class="box box1">1</div>
<div class="box box2">2</div>
<div class="box box3">3</div>
<div class="box box4">4</div>
<div class="box box5">5</div>
<div class="box box6">6</div>
<div class="box box7">7</div>
<div class="box box8">8</div>
<div class="box box9">9</div>
<!-- <div class="box box10">10</div> -->
</div>
</body>
</html>
grid-column-start
、列线终止线grid-column-end
和行线起始线grid-row-start
、行线终止线grid-row-end
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<style>
body {
padding: 10px;
}
.container {
display: grid;
grid-template-columns: 100px 100px 100px;
grid-template-rows: 100px 100px 100px;
}
.box {
background-color: #444;
color: #fff;
font-size: 150%;
padding: 20px;
}
.container :nth-of-type(2n) {
background-color: red;
}
/* 可以更具网格线的编号来定位网格的位置 */
.box1 {
grid-column-start: 1;
grid-column-end: 2;
grid-row-start: 1;
grid-row-end: 2;
background-color: lawngreen;
}
.box9 {
grid-column-start: 3;
grid-column-end: 4;
grid-row-start: -2;
grid-row-end: -1;
background-color: rgb(202, 122, 255);
}
</style>
<body>
<div class="container">
<div class="box box1">1</div>
<div class="box box2">2</div>
<div class="box box3">3</div>
<div class="box box4">4</div>
<div class="box box5">5</div>
<div class="box box6">6</div>
<div class="box box7">7</div>
<div class="box box8">8</div>
<div class="box box9">9</div>
<!-- <div class="box box10">10</div> -->
</div>
</body>
</html>
定位网格的位置,使网格交换位置
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<style>
body {
padding: 10px;
}
.container {
display: grid;
grid-template-columns: 100px 100px 100px;
grid-template-rows: 100px 100px 100px;
}
.box {
background-color: #444;
color: #fff;
font-size: 150%;
padding: 20px;
}
.container :nth-of-type(2n) {
background-color: red;
}
/* 可以更具网格线的编号来定位网格的位置 */
.box9 {
grid-column-start: 1;
grid-column-end: 2;
grid-row-start: 1;
grid-row-end: 2;
background-color: lawngreen;
}
.box1 {
grid-column-start: 3;
grid-column-end: 4;
grid-row-start: -2;
grid-row-end: -1;
background-color: rgb(202, 122, 255);
}
</style>
<body>
<div class="container">
<div class="box box1">1</div>
<div class="box box2">2</div>
<div class="box box3">3</div>
<div class="box box4">4</div>
<div class="box box5">5</div>
<div class="box box6">6</div>
<div class="box box7">7</div>
<div class="box box8">8</div>
<div class="box box9">9</div>
<!-- <div class="box box10">10</div> -->
</div>
</body>
</html>
grid-column
和grid-row的start
与end
值合并在一起,两者之间用/
来分隔
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<style>
body {
padding: 10px;
}
.container {
display: grid;
grid-template-columns: 100px 100px 100px;
grid-template-rows: 100px 100px 100px;
}
.box {
background-color: #444;
color: #fff;
font-size: 150%;
padding: 20px;
}
.container :nth-of-type(2n) {
background-color: red;
}
/* 可以更具网格线的编号来定位网格的位置 */
/* 定义网格位置的简写方式 */
.box1 {
grid-column: 1/2;
grid-row: 1/2;
background-color: lawngreen;
}
.box9 {
grid-column: 3/4;
grid-row: -2/-1;
background-color: rgb(202, 122, 255);
}
</style>
<body>
<div class="container">
<div class="box box1">1</div>
<div class="box box2">2</div>
<div class="box box3">3</div>
<div class="box box4">4</div>
<div class="box box5">5</div>
<div class="box box6">6</div>
<div class="box box7">7</div>
<div class="box box8">8</div>
<div class="box box9">9</div>
<!-- <div class="box box10">10</div> -->
</div>
</body>
</html>
grid-area
,网格区域他是由四条网格线组成的一个空间,简单点说,一个网格单元格也是一个网格区域(因为他也是有四条网格线组成的一个空间),多个单元格合并在一起也是一个网格区域。这样一来,要实现上例的效果,还可以使用网格区域grid-area来完成。row-start/column-start/row-end/column-end
现在我们要获取到box5
这个有两种写法grid-area: 2/2/3/3``grid-area: 2/2/-2/-2
,意思就是正负数都可以获取这个区域
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<style>
body {
padding: 10px;
}
.container {
display: grid;
grid-template-columns: 100px 100px 100px;
grid-template-rows: 100px 100px 100px;
}
.box {
background-color: #444;
color: #fff;
font-size: 150%;
padding: 20px;
}
.container :nth-of-type(2n) {
background-color: red;
}
/* 可以更具网格线的编号来定位网格的位置 */
/* 定义网格位置的简写方式 */
.box1 {
grid-column: 1/2;
grid-row: 1/2;
background-color: lawngreen;
}
.box9 {
grid-column: 3/4;
grid-row: -2/-1;
background-color: rgb(202, 122, 255);
}
/* 使用网格区域我们来获取一下box5 */
.box5 {
/* 这个获取区域可以写两种均可以获取到box5号这个 */
grid-area: 2/2/3/3;
/* grid-area: 2/2/-2/-2; */
background-color: mediumturquoise;
}
</style>
<body>
<div class="container">
<div class="box box1">1</div>
<div class="box box2">2</div>
<div class="box box3">3</div>
<div class="box box4">4</div>
<div class="box box5">5</div>
<div class="box box6">6</div>
<div class="box box7">7</div>
<div class="box box8">8</div>
<div class="box box9">9</div>
<!-- <div class="box box10">10</div> -->
</div>
</body>
</html>
gap
来表示grid-column-gap
, grid-row-gap
在grid
布局中创建grid-gap: 10px 5px
同样可以简写为这样的格式,但这前面个为横后面个数为纵
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<style>
body {
padding: 10px;
}
.container {
display: grid;
/* 这下面的100px可以用百分比或者1fr来表示,也是同样的效果 */
grid-template-columns: 100px 100px 100px;
grid-template-rows: 100px 100px 100px;
/* 表示的是网格的间距 */
grid-column-gap: 10px; /*这个是表示纵方向*/
grid-row-gap: 10px; /*这个是表示横方向*/
grid-gap: 10px 5px; /*同样可以简写为这样的格式,但这前面个为横后面个数为纵*/
}
.box {
background-color: #444;
color: #fff;
font-size: 150%;
padding: 20px;
}
.container :nth-of-type(2n) {
background-color: red;
}
/* 可以更具网格线的编号来定位网格的位置 */
/* 定义网格位置的简写方式 */
.box1 {
grid-column: 1/2;
grid-row: 1/2;
background-color: lawngreen;
}
.box9 {
grid-column: 3/4;
grid-row: -2/-1;
background-color: rgb(202, 122, 255);
}
/* 使用网格区域我们来获取一下box5 */
.box5 {
/* 这个获取区域可以写两种均可以获取到box5号这个 */
grid-area: 2/2/3/3;
/* grid-area: 2/2/-2/-2; */
background-color: mediumturquoise;
}
</style>
<body>
<div class="container">
<div class="box box1">1</div>
<div class="box box2">2</div>
<div class="box box3">3</div>
<div class="box box4">4</div>
<div class="box box5">5</div>
<div class="box box6">6</div>
<div class="box box7">7</div>
<div class="box box8">8</div>
<div class="box box9">9</div>
<!-- <div class="box box10">10</div> -->
</div>
</body>
</html>
grid
中可以使用的函数repeat()
、minmax()
、fit-content()
grid
中repeat()
函数repeat
函数可以以一种更简洁的方式去表示大量而且重复行的表达式比如上面 grid-template-columns: 100px 100px 100px
我们可以写成 repeat(3, 100px)
、还可以写成repeat(3, 1fr)
、还可以写成repeat(3, 百分比的形式)
它的第一个参数是重复的次数
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<style>
body {
padding: 10px;
}
.container {
width: 300px;
height: 300px;
display: grid;
/* 这下面的100px可以用百分比或者1fr来表示,也是同样的效果 */
/* grid-template-columns: 100px 100px 100px;
grid-template-rows: 100px 100px 100px; */
grid-template-rows: repeat(3, 100px);
grid-template-columns: repeat(3, 100px);
/* 表示的是网格的间距 */
grid-column-gap: 10px; /*这个是表示纵方向*/
grid-row-gap: 10px; /*这个是表示横方向*/
grid-gap: 10px 5px; /*同样可以简写为这样的格式,但这前面个为横后面个数为纵*/
}
.box {
background-color: #444;
color: #fff;
font-size: 150%;
padding: 20px;
}
.container :nth-of-type(2n) {
background-color: red;
}
/* 可以更具网格线的编号来定位网格的位置 */
/* 定义网格位置的简写方式 */
.box1 {
grid-column: 1/2;
grid-row: 1/2;
background-color: lawngreen;
}
.box9 {
grid-column: 3/4;
grid-row: -2/-1;
background-color: rgb(202, 122, 255);
}
/* 使用网格区域我们来获取一下box5 */
.box5 {
/* 这个获取区域可以写两种均可以获取到box5号这个 */
grid-area: 2/2/3/3;
grid-area: 2/2/-2/-2;
background-color: mediumturquoise;
}
</style>
<body>
<div class="container">
<div class="box box1">1</div>
<div class="box box2">2</div>
<div class="box box3">3</div>
<div class="box box4">4</div>
<div class="box box5">5</div>
<div class="box box6">6</div>
<div class="box box7">7</div>
<div class="box box8">8</div>
<div class="box box9">9</div>
<!-- <div class="box box10">10</div> -->
</div>
</body>
</html>
minmax()
函数fit-content()
函数min(maximum size, max(minimum size, argument))
参数可以是长度值和百分比。justify-items: center | end | center | stretch(默认); //水平方向对齐
左对齐|右对齐|居中|填满表格
align-items: start | end | center | stretch;//垂直方向对齐
顶部对齐|底部对齐|居中|填满表格
重要的一点是这些都必须在网格中才能体现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<style>
body {
padding: 10px;
}
.container {
display: grid;
/* 这下面的100px可以用百分比或者1fr来表示,也是同样的效果 */
/* grid-template-columns: 100px 100px 100px;
grid-template-rows: 100px 100px 100px; */
grid-template-rows: repeat(3, 100px);
grid-template-columns: repeat(3, 100px);
/* 表示的是网格的间距 */
grid-column-gap: 10px; /*这个是表示纵方向*/
grid-row-gap: 10px; /*这个是表示横方向*/
grid-gap: 10px 5px; /*同样可以简写为这样的格式,但这前面个为横后面个数为纵*/
}
.box {
background-color: #444;
color: #fff;
font-size: 150%;
padding: 20px;
}
/* .container :nth-of-type(2n) {
background-color: red;
} */
/* 可以更具网格线的编号来定位网格的位置 */
/* 定义网格位置的简写方式 */
.box1 {
grid-column: 1/2;
grid-row: 1/2;
background-color: lawngreen;
}
.box9 {
grid-column: 3/4;
grid-row: -2/-1;
background-color: rgb(202, 122, 255);
}
/* 使用网格区域我们来获取一下box5 */
.box5 {
/* 这个获取区域可以写两种均可以获取到box5号这个 */
grid-area: 2/2/3/3;
grid-area: 2/2/-2/-2;
background-color: mediumturquoise;
}
/* 这个是让网格中的元素对齐方式 */
.box4 {
display: grid;
justify-items: center;
align-items: center;
background-color: olivedrab;
}
</style>
<body>
<div class="container">
<div class="box box1">1</div>
<div class="box box2">2</div>
<div class="box box3">3</div>
<div class="box box4">4</div>
<div class="box box5">5</div>
<div class="box box6">6</div>
<div class="box box7">7</div>
<div class="box box8">8</div>
<div class="box box9">9</div>
<!-- <div class="box box10">10</div> -->
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>圣杯布局</title>
</head>
<style type="text/css">
.header {
grid-area: header;
background-color: olivedrab;
}
.footer {
grid-area: footer;
background-color: orange;
}
.main {
grid-area: main;
background-color: orangered;
}
.left {
grid-area: left;
background-color: orchid;
}
.right {
grid-area: right;
background-color: powderblue;
}
.container {
display: grid;
grid-template-areas:
"header header header"
"left main right"
"footer footer footer";
grid-template-columns: 150px 1fr 150px;
grid-template-rows:
100px
1fr
30px;
min-height: 100vh;
}
@media screen and (max-width: 600px) {
.container {
grid-template-areas:
"header"
"left"
"main"
"right"
"footer";
grid-template-columns: 100%;
grid-template-rows:
100px
50px
1fr
50px
30px;
}
}
</style>
<body>
<body class="container">
<header class="header">header</header>
<main class="main">main</main>
<aside class="left">left</aside>
<aside class="right">right</aside>
<footer class="footer">footer</footer>
</body>
</body>
</html>