Blogger Information
Blog 33
fans 0
comment 0
visits 27663
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Web 前端 - CSS - Grid 网格布局及通用PC端布局实战
Original
1251 people have browsed it

Web 前端 - CSS - Grid 网格布局及通用PC端布局实战

[toc]

一、概述

序号 属性 描述
1 解释 网格布局是一个二维的基于网格的布局系统
2 特点 由水平(行)和垂直(列)线集合创建的一个模式,我们可以根据这个模式排列我们的设计元素
3 场景 和flex配合能够快速布局复杂的页面,使用媒体查询重新排列网格变得非常容易,同时gird布局比其他布局方式所需的代码量少很多

二、网格容器

  • 网格容器:应用 display: grid 的元素。这是所有网格项的直接父级元素
  • 网格项:网格容器的直接子元素,也叫项目
  • 网格线:构成网格结构的分界线。它们既可以是垂直的(“列网格线(column grid lines)”),也可以是水平的(“行网格线(row grid lines)”),并位于行或列的任一侧
  • 网格轨道:两条相邻网格线之间的空间。你可以把它们想象成网格的列或行

1. 属性

序号 属性 描述 格式
1 grid-template-columns 显式定义列及单元格的宽度 grid-template-columns: [line_name] cell_width_1 [line_name] cell_width_2 [line_name] cell_width_3;
2 grid-template-rows 显式定义行及单元格的高度 grid-template-rows: [line_name] cell_height_1 [line_name] cell_height_2 [line_name] cell_height_3;
3 grid-auto-columns 隐式定义列及单元格的宽度,当项目多于单元格时就会创建隐式单元格 grid-auto-columns: cell_height_1 cell_height_2 cell_height_3;
4 grid-auto-rows 隐式定义行及单元格的高度,当项目多于单元格时就会创建隐式单元格 grid-auto-rows: cell_height_1 cell_height_2 cell_height_3;
5 grid-auto-flow 设置项目在单元格中的排列方式
6 justify-content 设置单元格在容器横轴(行)上的对齐方式
7 align-content 设置单元格在容器纵轴(列)上的对齐方式
8 place-content justify-contentalign-content属性的简写
9 justify-items 设置项目在单元格横轴(行)上的对齐方式
10 align-items 设置项目在单元格纵轴(列)上的对齐方式
11 place-items justify-itemsalign-items属性的简写
12 column-gap 设置列间距
13 row-gap 设置行间距
14 gap column-gaprow-gap属性的简写

2. 值

序号 值/单位 描述 适用属性 备注
1 auto 浏览器自动计算 grid-template-columnsgrid-template-rowsgrid-auto-columnsgrid-auto-rows
2 px 固定值,像素计 grid-template-columnsgrid-template-rowsgrid-auto-columnsgrid-auto-rows
3 % 百分比,按百分比分配 grid-template-columnsgrid-template-rowsgrid-auto-columnsgrid-auto-rows
4 fr 比例,按比例分配 grid-template-columnsgrid-template-rowsgrid-auto-columnsgrid-auto-rows
5 repeat 重复、分组、弹性、自动填充设置 grid-template-columnsgrid-template-rows 重复:grid-template-columns: repeat (n, cell_height_1 cell_height_2 cell_height_3),n代表重复的次数,cell_height代表单元格的宽度
6 row 默认值。水平(行)方向,顺序为从左往右 grid-auto-flow
7 column 垂直(列)方向,顺序为从上往下 grid-auto-flow
8 start 默认值。左侧起始线对齐 justify-content
9 end 右侧终止线对齐 justify-content
10 center 居中对齐 justify-content
11 stretch 填充满整个网格容器的宽度 justify-content
12 space-evenly 平均对齐。剩余空间在每个单元格之间平均分配 justify-content
13 space-around 分散对齐。剩余空间在每个单元格两侧平均分配 justify-content
14 space-between 两端对齐。剩余空间在头尾单元格之外的子元素之间平均分配 justify-content
15 start 默认值。顶部起始线对齐 align-content
16 end 底部终止线对齐 align-content
17 center 居中对齐 align-content
18 stretch 填充满整个网格容器的高度 align-content
19 space-evenly 平均对齐。剩余空间在每个单元格之间平均分配 align-content
20 space-around 分散对齐。剩余空间在每个单元格两侧平均分配 align-content
21 space-between 两端对齐。剩余空间在头尾单元格之外的子元素之间平均分配 align-content
22 start 默认值。单元格左侧起始线对齐 justify-items
23 end 单元格右侧终止线对齐 justify-items
24 center 居中对齐 justify-items
25 stretch 填满单元格的宽度 justify-items
26 start 默认值。单元格顶部起始线对齐 align-items
27 end 单元格底部终止线对齐 align-items
28 center 居中对齐 align-items
29 stretch 填满单元格的高度 align-items

三、网格单元格

  • 网格单元格:两个相邻的行和两个相邻的列网格线之间的空间,这是构成网格系统的一个基本单元

1. 属性

序号 属性 描述 格式
1 grid-column-start 引用列网格线划分单元格跨度,单元格开始的列网格线 `grid-column-start: n span n line_name auto`
2 grid-column-end 引用列网格线划分单元格跨度,单元格结束的列网格线 `grid-column-end: n span n line_name auto`
3 grid-row-start 引用行网格线划分单元格跨度,单元格开始的行网格线 `grid-row-start: n span n line_name auto`
4 grid-row-end 引用行网格线划分单元格跨度,单元格结束的行网格线 `grid-row-end: n span n line_name auto`
5 grid-column grid-column-startgrid-column-end属性的简写 `grid-column: satrt-line / end-line satrt-line / span n`
6 grid-row grid-row-startgrid-row-end属性的简写 `grid-row: satrt-line / end-line satrt-line / span n`
7 justify-self 设置某项目在单元格横轴(行)上的对齐方式
8 align-self 设置某项目在单元格纵轴(列)上的对齐方式
9 place-self justify-itemsalign-items属性的简写

2. 值

序号 描述 适用属性
1 start 默认值。单元格左侧起始线对齐 justify-self
2 end 单元格右侧终止线对齐 justify-self
3 center 居中对齐 justify-self
4 stretch 填满单元格的宽度 justify-self
5 start 默认值。单元格顶部起始线对齐 align-self
6 end 单元格底部终止线对齐 align-self
7 center 居中对齐 align-self
8 stretch 填满单元格的高度 align-self

四、网格区域

  • 网格区域:由网格单元格组成的矩形块,一个网格区域可以由任意数量的网格单元格组成

属性

序号 属性 描述 格式 适用
1 grid-template-areas 通过引用grid-area属性指定的网格区域名称来定义网格模板 `grid-template-areas: grid-area_name . none` 容器
2 grid-area 为项目提供一个名称,以便可以被使用网格容器 grid-template-areas 属性创建的模板进行引用,或者直接划分单元格跨度 `grid-area: item_name grid-row-start / grid-column-start / grid-row-end / grid-column-end` 项目

五、代码实例

1. 使用固定值、百分比、比例、auto、repeat重复、分组、弹性和自动填充设置显式地划分行和列

1.1 使用固定值显式地划分行和列

  1. <!DOCTYPE html>
  2. <html lang="zh_hans">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. .box {
  9. width: 400px;
  10. height: 400px;
  11. background-color: lightblue;
  12. display: grid;
  13. grid-template-columns: 100px 100px 100px;
  14. grid-template-rows: 100px 100px 100px;
  15. }
  16. span {
  17. background-color: lightcoral;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div class="box">
  23. <span>1</span>
  24. <span>2</span>
  25. <span>3</span>
  26. <span>4</span>
  27. <span>5</span>
  28. <span>6</span>
  29. <span>7</span>
  30. <span>8</span>
  31. <span>9</span>
  32. </div>
  33. </body>
  34. </html>

1.2 使用百分比显式地划分行和列

  1. <!DOCTYPE html>
  2. <html lang="zh_hans">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. .box {
  9. width: 400px;
  10. height: 400px;
  11. background-color: lightblue;
  12. display: grid;
  13. grid-template-columns: 25% 25% 25%;
  14. grid-template-rows: 25% 25% 25%;
  15. }
  16. span {
  17. background-color: lightcoral;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div class="box">
  23. <span>1</span>
  24. <span>2</span>
  25. <span>3</span>
  26. <span>4</span>
  27. <span>5</span>
  28. <span>6</span>
  29. <span>7</span>
  30. <span>8</span>
  31. <span>9</span>
  32. </div>
  33. </body>
  34. </html>

1.3 使用比例显式地划分行和列

  1. <!DOCTYPE html>
  2. <html lang="zh_hans">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. .box {
  9. width: 400px;
  10. height: 400px;
  11. background-color: lightblue;
  12. display: grid;
  13. grid-template-columns: 0.25fr 0.25fr 0.25fr;
  14. grid-template-rows: 0.25fr 0.25fr 0.25fr;
  15. }
  16. span {
  17. background-color: lightcoral;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div class="box">
  23. <span>1</span>
  24. <span>2</span>
  25. <span>3</span>
  26. <span>4</span>
  27. <span>5</span>
  28. <span>6</span>
  29. <span>7</span>
  30. <span>8</span>
  31. <span>9</span>
  32. </div>
  33. </body>
  34. </html>

1.4 使用auto显式地划分行和列

  1. <!DOCTYPE html>
  2. <html lang="zh_hans">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. .box {
  9. width: 400px;
  10. height: 400px;
  11. background-color: lightblue;
  12. display: grid;
  13. grid-template-columns: 0.25fr auto 0.25fr;
  14. grid-template-rows: 0.25fr auto 0.25fr;
  15. }
  16. span {
  17. background-color: lightcoral;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div class="box">
  23. <span>1</span>
  24. <span>2</span>
  25. <span>3</span>
  26. <span>4</span>
  27. <span>5</span>
  28. <span>6</span>
  29. <span>7</span>
  30. <span>8</span>
  31. <span>9</span>
  32. </div>
  33. </body>
  34. </html>

1.5 使用repeat重复显式地划分行和列

  1. <!DOCTYPE html>
  2. <html lang="zh_hans">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. .box {
  9. width: 400px;
  10. height: 400px;
  11. background-color: lightblue;
  12. display: grid;
  13. grid-template-columns: repeat(3, 0.25fr);
  14. grid-template-rows: repeat(3, 0.25fr);
  15. }
  16. span {
  17. background-color: lightcoral;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div class="box">
  23. <span>1</span>
  24. <span>2</span>
  25. <span>3</span>
  26. <span>4</span>
  27. <span>5</span>
  28. <span>6</span>
  29. <span>7</span>
  30. <span>8</span>
  31. <span>9</span>
  32. </div>
  33. </body>
  34. </html>

1.6 使用repeat分组显式地划分行和列

  1. <!DOCTYPE html>
  2. <html lang="zh_hans">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>Document</title>
  7. <style>
  8. .box {
  9. width: 400px;
  10. height: 400px;
  11. background-color: lightblue;
  12. display: grid;
  13. grid-template-columns: repeat(3, 0.5fr 0.3fr);
  14. grid-template-rows: repeat(3, 0.5fr 0.3fr);
  15. }
  16. span {
  17. background-color: lightcoral;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div class="box">
  23. <span>1</span>
  24. <span>2</span>
  25. <span>3</span>
  26. <span>4</span>
  27. <span>5</span>
  28. <span>6</span>
  29. <span>7</span>
  30. <span>8</span>
  31. <span>9</span>
  32. </div>
  33. </body>
  34. </html>

1.7 使用repeat弹性显式地划分行和列

  1. <!DOCTYPE html>
  2. <html lang="zh_hans">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>Document</title>
  7. <style>
  8. .box {
  9. width: 400px;
  10. height: 400px;
  11. background-color: lightblue;
  12. display: grid;
  13. grid-template-columns: repeat(3, minmax(100px, 200px));
  14. grid-template-rows: repeat(3, minmax(100px, 200px));
  15. }
  16. span {
  17. background-color: lightcoral;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div class="box">
  23. <span>1</span>
  24. <span>2</span>
  25. <span>3</span>
  26. <span>4</span>
  27. <span>5</span>
  28. <span>6</span>
  29. <span>7</span>
  30. <span>8</span>
  31. <span>9</span>
  32. </div>
  33. </body>
  34. </html>

1.8 使用repeat自动填充显式地划分行和列

  1. <!DOCTYPE html>
  2. <html lang="zh_hans">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>Document</title>
  7. <style>
  8. .box {
  9. width: 400px;
  10. height: 400px;
  11. background-color: lightblue;
  12. display: grid;
  13. grid-template-columns: repeat(auto-fill, 100px);
  14. grid-template-rows: repeat(auto-fill, 100px);
  15. }
  16. span {
  17. background-color: lightcoral;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div class="box">
  23. <span>1</span>
  24. <span>2</span>
  25. <span>3</span>
  26. <span>4</span>
  27. <span>5</span>
  28. <span>6</span>
  29. <span>7</span>
  30. <span>8</span>
  31. <span>9</span>
  32. </div>
  33. </body>
  34. </html>

2. 设置单元格在容器上的对齐方式、项目在单元格上的对齐方式、某项目在单元格中的对齐方式、项目在单元格中的排列方式及行和列间距

2.1 设置单元格在容器上的对齐方式

  1. <!DOCTYPE html>
  2. <html lang="zh_hans">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. .box {
  9. width: 400px;
  10. height: 400px;
  11. background-color: lightblue;
  12. display: grid;
  13. grid-template-columns: repeat(3, 0.25fr);
  14. grid-template-rows: repeat(3, 0.25fr);
  15. place-content: center center;
  16. }
  17. span {
  18. background-color: lightcoral;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div class="box">
  24. <span>1</span>
  25. <span>2</span>
  26. <span>3</span>
  27. <span>4</span>
  28. <span>5</span>
  29. <span>6</span>
  30. <span>7</span>
  31. <span>8</span>
  32. <span>9</span>
  33. </div>
  34. </body>
  35. </html>

2.2 设置项目在单元格上的对齐方式

  1. <!DOCTYPE html>
  2. <html lang="zh_hans">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. .box {
  9. width: 400px;
  10. height: 400px;
  11. background-color: lightblue;
  12. display: grid;
  13. grid-template-columns: repeat(3, 0.25fr);
  14. grid-template-rows: repeat(3, 0.25fr);
  15. place-items: center center;
  16. }
  17. span {
  18. background-color: lightcoral;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div class="box">
  24. <span>1</span>
  25. <span>2</span>
  26. <span>3</span>
  27. <span>4</span>
  28. <span>5</span>
  29. <span>6</span>
  30. <span>7</span>
  31. <span>8</span>
  32. <span>9</span>
  33. </div>
  34. </body>
  35. </html>

2.3 设置某项目在单元格中的对齐方式

  1. <!DOCTYPE html>
  2. <html lang="zh_hans">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. .box {
  9. width: 400px;
  10. height: 400px;
  11. background-color: lightblue;
  12. display: grid;
  13. grid-template-columns: repeat(3, 0.25fr);
  14. grid-template-rows: repeat(3, 0.25fr);
  15. }
  16. span {
  17. background-color: lightcoral;
  18. }
  19. span:nth-of-type(5) {
  20. place-self: center center;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <div class="box">
  26. <span>1</span>
  27. <span>2</span>
  28. <span>3</span>
  29. <span>4</span>
  30. <span>5</span>
  31. <span>6</span>
  32. <span>7</span>
  33. <span>8</span>
  34. <span>9</span>
  35. </div>
  36. </body>
  37. </html>

2.4 设置项目在单元格中的排列方式

  1. <!DOCTYPE html>
  2. <html lang="zh_hans">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. .box {
  9. width: 400px;
  10. height: 400px;
  11. background-color: lightblue;
  12. display: grid;
  13. grid-template-columns: repeat(3, 0.25fr);
  14. grid-template-rows: repeat(3, 0.25fr);
  15. grid-auto-flow: column;
  16. }
  17. span {
  18. background-color: lightcoral;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div class="box">
  24. <span>1</span>
  25. <span>2</span>
  26. <span>3</span>
  27. <span>4</span>
  28. <span>5</span>
  29. <span>6</span>
  30. <span>7</span>
  31. <span>8</span>
  32. <span>9</span>
  33. </div>
  34. </body>
  35. </html>

2.5 设置行和列间距

  1. <!DOCTYPE html>
  2. <html lang="zh_hans">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. .box {
  9. width: 400px;
  10. height: 400px;
  11. background-color: lightblue;
  12. display: grid;
  13. grid-template-columns: repeat(3, 0.25fr);
  14. grid-template-rows: repeat(3, 0.25fr);
  15. gap: 10px 5px;
  16. }
  17. span {
  18. background-color: lightcoral;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div class="box">
  24. <span>1</span>
  25. <span>2</span>
  26. <span>3</span>
  27. <span>4</span>
  28. <span>5</span>
  29. <span>6</span>
  30. <span>7</span>
  31. <span>8</span>
  32. <span>9</span>
  33. </div>
  34. </body>
  35. </html>

3. 使用网格线、使用偏移量和使用命名网格线划分单元格跨度

3.1 使用网格线划分单元格跨度

  1. <!DOCTYPE html>
  2. <html lang="zh_hans">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>Document</title>
  7. <style>
  8. .box {
  9. width: 400px;
  10. height: 400px;
  11. background-color: lightblue;
  12. display: grid;
  13. grid-template-columns: repeat(3, 100px);
  14. grid-template-rows: repeat(3, 100px);
  15. }
  16. span {
  17. background-color: lightcoral;
  18. }
  19. span:nth-of-type(1) {
  20. background-color: lavender;
  21. grid-column-start: 2;
  22. grid-column-end: 3;
  23. grid-row-start: 1;
  24. grid-row-end: 3;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <div class="box">
  30. <span>1</span>
  31. <span>2</span>
  32. <span>3</span>
  33. <span>4</span>
  34. <span>5</span>
  35. <span>6</span>
  36. <span>7</span>
  37. <span>8</span>
  38. <span>9</span>
  39. </div>
  40. </body>
  41. </html>

3.2 使用偏移量划分单元格跨度

  1. <!DOCTYPE html>
  2. <html lang="zh_hans">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>Document</title>
  7. <style>
  8. .box {
  9. width: 400px;
  10. height: 400px;
  11. background-color: lightblue;
  12. display: grid;
  13. grid-template-columns: repeat(3, 100px);
  14. grid-template-rows: repeat(3, 100px);
  15. }
  16. span {
  17. background-color: lightcoral;
  18. }
  19. span:nth-of-type(1) {
  20. background-color: lavender;
  21. grid-column: 2 / span 1;
  22. grid-row: 1 / span 2;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <div class="box">
  28. <span>1</span>
  29. <span>2</span>
  30. <span>3</span>
  31. <span>4</span>
  32. <span>5</span>
  33. <span>6</span>
  34. <span>7</span>
  35. <span>8</span>
  36. <span>9</span>
  37. </div>
  38. </body>
  39. </html>

3.3 使用命名网格线划分单元格跨度

  1. <!DOCTYPE html>
  2. <html lang="zh_hans">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>Document</title>
  7. <style>
  8. .box {
  9. width: 400px;
  10. height: 400px;
  11. background-color: lightblue;
  12. display: grid;
  13. grid-template-columns:
  14. [c1-start] 100px [c1-end c2-start] 100px [c2-end c3-start] 100px
  15. [c3-end];
  16. grid-template-rows:
  17. [r1-start] 100px [r1-end r2-start] 100px [r2-end r3-start] 100px
  18. [r3-end];
  19. }
  20. span {
  21. background-color: lightcoral;
  22. }
  23. span:nth-of-type(1) {
  24. background-color: lavender;
  25. grid-column: c2-start / c2-end;
  26. grid-row: r1-start / r3-start;
  27. }
  28. </style>
  29. </head>
  30. <body>
  31. <div class="box">
  32. <span>1</span>
  33. <span>2</span>
  34. <span>3</span>
  35. <span>4</span>
  36. <span>5</span>
  37. <span>6</span>
  38. <span>7</span>
  39. <span>8</span>
  40. <span>9</span>
  41. </div>
  42. </body>
  43. </html>

4. 命名网格区域、设置网格区域及通用PC端布局实战

  1. <!DOCTYPE html>
  2. <html lang="zh_hans">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>Document</title>
  7. <style>
  8. .box {
  9. width: 300px;
  10. height: 300px;
  11. background-color: lightblue;
  12. display: grid;
  13. grid-template-columns: 60px 1fr 60px;
  14. grid-template-rows: 30px 1fr 30px;
  15. /*设置网格区域*/
  16. grid-template-areas:
  17. "header header header"
  18. "lside main rside"
  19. "footer footer footer"
  20. ;
  21. }
  22. .header {
  23. background-color: lightpink;
  24. /*命名网格区域*/
  25. grid-area: header;
  26. }
  27. .lside {
  28. background-color: lightsteelblue;
  29. /*命名网格区域*/
  30. grid-area: lside;
  31. }
  32. .main {
  33. background-color: lightsalmon;
  34. /*命名网格区域*/
  35. grid-area: main;
  36. }
  37. .rside {
  38. background-color: khaki;
  39. /*命名网格区域*/
  40. grid-area: rside;
  41. }
  42. .footer {
  43. background-color: lightgrey;
  44. /*命名网格区域*/
  45. grid-area: footer;
  46. }
  47. </style>
  48. </head>
  49. <body>
  50. <div class="box">
  51. <span class="header">1</span>
  52. <span class="lside">2</span>
  53. <span class="main">3</span>
  54. <span class="rside">4</span>
  55. <span class="footer">5</span>
  56. </div>
  57. </body>
  58. </html>

六、课程总结

  • 今天学习了 CSS 的 Grid 网格布局,通过上课认真听讲和认真完成老师布置的作业,使得我对 CSS 的理解和运用更加深入和熟悉。最主要的知识点是明白了网格布局是由水平(行)和垂直(列)线集合对元素进行编排的原理,以及了解并熟悉了 Grid 布局常见属性如 grid-template-columns、grid-template-rows、grid-template-areas 等的用法。
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:grid布局不能用在ie上, 还好, ie在国内不到5%份额了, 消失指日可待, 微软的Edge和chrome内核也一样的, 放心使用吧
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!