CSS 网格布局(Grid)

Header

Menu

Main

Right

Footer

代码如下:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .item1 { grid-area: header; }
  6. .item2 { grid-area: menu; }
  7. .item3 { grid-area: main; }
  8. .item4 { grid-area: right; }
  9. .item5 { grid-area: footer; }
  10. .grid-container {
  11. display: grid;
  12. grid-template-areas:
  13. 'header header header header header header'
  14. 'menu main main main right right'
  15. 'menu footer footer footer footer footer';
  16. grid-gap: 10px;
  17. background-color: #2196F3;
  18. padding: 10px;
  19. }
  20. .grid-container > div {
  21. background-color: rgba(255, 255, 255, 0.8);
  22. text-align: center;
  23. padding: 20px 0;
  24. font-size: 30px;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <h2>Grid 布局</h2>
  30. <p>此网格布局包含六列和三行:</p>
  31. <div class="grid-container">
  32. <div class="item1">Header</div>
  33. <div class="item2">Menu</div>
  34. <div class="item3">Main</div>
  35. <div class="item4">Right</div>
  36. <div class="item5">Footer</div>
  37. </div>
  38. </body>
  39. </html>

网格布局

CSS 网格布局模块(CSS Grid Layout Module)提供了带有行和列的基于网格的布局系统,它使网页设计变得更加容易,而无需使用浮动和定位。


浏览器支持

所有现代浏览器均支持网格属性。

57.016.052.01044

网格元素

网格布局由一个父元素以及一个或多个子元素组成:

  1. <div class="grid-container">
  2. <div class="grid-item">1</div>
  3. <div class="grid-item">2</div>
  4. <div class="grid-item">3</div>
  5. <div class="grid-item">4</div>
  6. <div class="grid-item">5</div>
  7. <div class="grid-item">6</div>
  8. <div class="grid-item">7</div>
  9. <div class="grid-item">8</div>
  10. <div class="grid-item">9</div>
  11. </div>

1

2

3

4

5

6

7

8

9


Display 属性

当 HTML 元素的 display 属性设置为 gridinline-grid 时,它就会成为网格容器。

实例1
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .grid-container {
  6. display: grid;
  7. grid-template-columns: auto auto auto;
  8. background-color: #2196F3;
  9. padding: 10px;
  10. }
  11. .grid-item {
  12. background-color: rgba(255, 255, 255, 0.8);
  13. border: 1px solid rgba(0, 0, 0, 0.8);
  14. padding: 20px;
  15. font-size: 30px;
  16. text-align: center;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <h2>display 属性:</h2>
  22. <div class="grid-container">
  23. <div class="grid-item">1</div>
  24. <div class="grid-item">2</div>
  25. <div class="grid-item">3</div>
  26. <div class="grid-item">4</div>
  27. <div class="grid-item">5</div>
  28. <div class="grid-item">6</div>
  29. <div class="grid-item">7</div>
  30. <div class="grid-item">8</div>
  31. <div class="grid-item">9</div>
  32. </div>
  33. <p>请将 <em>display</em> 属性设置为 <em>grid</em>,以生成块级的网格容器。</p>
  34. </body>
  35. </html>
实例2
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .grid-container {
  6. display: inline-grid;
  7. grid-template-columns: auto auto auto;
  8. background-color: #2196F3;
  9. padding: 10px;
  10. }
  11. .grid-item {
  12. background-color: rgba(255, 255, 255, 0.8);
  13. border: 1px solid rgba(0, 0, 0, 0.8);
  14. padding: 20px;
  15. font-size: 30px;
  16. text-align: center;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <h2>display 属性:</h2>
  22. <div class="grid-container">
  23. <div class="grid-item">1</div>
  24. <div class="grid-item">2</div>
  25. <div class="grid-item">3</div>
  26. <div class="grid-item">4</div>
  27. <div class="grid-item">5</div>
  28. <div class="grid-item">6</div>
  29. <div class="grid-item">7</div>
  30. <div class="grid-item">8</div>
  31. <div class="grid-item">9</div>
  32. </div>
  33. <p>请将 <em>display</em> 属性设置为 <em>inline-grid</em>,以生成行内的网格容器。</p>
  34. </body>
  35. </html>

网格容器的所有直接子元素将自动成为网格项目。


网格列(Grid Columns)

网格项的垂直线被称为列。


网隔行(Grid Rows)

网格项的水平线被称为行。


网格间隙(Grid Gaps)

每列/行之间的间隔称为间隙。

您可以通过使用以下属性之一来调整间隙大小:

  • grid-column-gap
  • grid-row-gap
  • grid-gap
实例1

grid-column-gap 属性设置列之间的间隙:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .grid-container {
  6. display: grid;
  7. grid-column-gap: 50px;
  8. grid-template-columns: auto auto auto;
  9. background-color: #2196F3;
  10. padding: 10px;
  11. }
  12. .grid-item {
  13. background-color: rgba(255, 255, 255, 0.8);
  14. border: 1px solid rgba(0, 0, 0, 0.8);
  15. padding: 20px;
  16. font-size: 30px;
  17. text-align: center;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <h2>grid-column-gap 属性:</h2>
  23. <div class="grid-container">
  24. <div class="grid-item">1</div>
  25. <div class="grid-item">2</div>
  26. <div class="grid-item">3</div>
  27. <div class="grid-item">4</div>
  28. <div class="grid-item">5</div>
  29. <div class="grid-item">6</div>
  30. <div class="grid-item">7</div>
  31. <div class="grid-item">8</div>
  32. <div class="grid-item">9</div>
  33. </div>
  34. <p>请使用 <em>grid-column-gap</em> 属性调整列之间的空间。</p>
  35. </body>
  36. </html>
实例2

grid-row-gap 属性设置行之间的间隙:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .grid-container {
  6. display: grid;
  7. grid-row-gap: 50px;
  8. grid-template-columns: auto auto auto;
  9. background-color: #2196F3;
  10. padding: 10px;
  11. }
  12. .grid-item {
  13. background-color: rgba(255, 255, 255, 0.8);
  14. border: 1px solid rgba(0, 0, 0, 0.8);
  15. padding: 20px;
  16. font-size: 30px;
  17. text-align: center;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <h2>grid-row-gap 属性:</h2>
  23. <div class="grid-container">
  24. <div class="grid-item">1</div>
  25. <div class="grid-item">2</div>
  26. <div class="grid-item">3</div>
  27. <div class="grid-item">4</div>
  28. <div class="grid-item">5</div>
  29. <div class="grid-item">6</div>
  30. <div class="grid-item">7</div>
  31. <div class="grid-item">8</div>
  32. <div class="grid-item">9</div>
  33. </div>
  34. <p>请使用 <em>grid-row-gap</em> 属性来调整行间距。</p>
  35. </body>
  36. </html>
实例3

grid-gap 属性是 grid-row-gap 和 grid-column-gap 属性的简写属性:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .grid-container {
  6. display: grid;
  7. grid-gap: 50px 100px;
  8. grid-template-columns: auto auto auto;
  9. background-color: #2196F3;
  10. padding: 10px;
  11. }
  12. .grid-item {
  13. background-color: rgba(255, 255, 255, 0.8);
  14. border: 1px solid rgba(0, 0, 0, 0.8);
  15. padding: 20px;
  16. font-size: 30px;
  17. text-align: center;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <h2>grid-gap 属性:</h2>
  23. <div class="grid-container">
  24. <div class="grid-item">1</div>
  25. <div class="grid-item">2</div>
  26. <div class="grid-item">3</div>
  27. <div class="grid-item">4</div>
  28. <div class="grid-item">5</div>
  29. <div class="grid-item">6</div>
  30. <div class="grid-item">7</div>
  31. <div class="grid-item">8</div>
  32. <div class="grid-item">9</div>
  33. </div>
  34. <p>请使用 <em>grid-gap</em> 属性来调整列<em></em>行之间的空间。</p>
  35. </body>
  36. </html>
实例4

grid-gap 属性还可用于将行间隙和列间隙设置为一个值:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .grid-container {
  6. display: grid;
  7. grid-gap: 50px;
  8. grid-template-columns: auto auto auto;
  9. background-color: #2196F3;
  10. padding: 10px;
  11. }
  12. .grid-item {
  13. background-color: rgba(255, 255, 255, 0.8);
  14. border: 1px solid rgba(0, 0, 0, 0.8);
  15. padding: 20px;
  16. font-size: 30px;
  17. text-align: center;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <h2>grid-gap 属性:</h2>
  23. <div class="grid-container">
  24. <div class="grid-item">1</div>
  25. <div class="grid-item">2</div>
  26. <div class="grid-item">3</div>
  27. <div class="grid-item">4</div>
  28. <div class="grid-item">5</div>
  29. <div class="grid-item">6</div>
  30. <div class="grid-item">7</div>
  31. <div class="grid-item">8</div>
  32. <div class="grid-item">9</div>
  33. </div>
  34. <p>请使用 <em>grid-gap</em> 属性来调整列<em></em>行之间的空间。</p>
  35. </body>
  36. </html>

网格行(Grid Lines)

列之间的线称为列线(column lines)。行之间的线称为行线(row lines)。

当把网格项目放在网格容器中时,请引用行号:

实例1

把网格项目放在列线 1,并在列线 3 结束它:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .grid-container {
  6. display: grid;
  7. grid-template-columns: auto auto auto;
  8. grid-gap: 10px;
  9. background-color: #2196F3;
  10. padding: 10px;
  11. }
  12. .grid-container > div {
  13. background-color: rgba(255, 255, 255, 0.8);
  14. text-align: center;
  15. padding: 20px 0;
  16. font-size: 30px;
  17. }
  18. .item1 {
  19. grid-column-start: 1;
  20. grid-column-end: 3;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <h2>网格行</h2>
  26. <div class="grid-container">
  27. <div class="item1">1</div>
  28. <div class="item2">2</div>
  29. <div class="item3">3</div>
  30. <div class="item4">4</div>
  31. <div class="item5">5</div>
  32. <div class="item6">6</div>
  33. <div class="item7">7</div>
  34. <div class="item8">8</div>
  35. </div>
  36. <p>您可以在放置网格项时引用行号。</p>
  37. </body>
  38. </html>
实例2

把网格项目放在行线 1,并在行线 3 结束它:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .grid-container {
  6. display: grid;
  7. grid-template-columns: auto auto auto;
  8. grid-gap: 10px;
  9. background-color: #2196F3;
  10. padding: 10px;
  11. }
  12. .grid-container > div {
  13. background-color: rgba(255, 255, 255, 0.8);
  14. text-align: center;
  15. padding: 20px 0;
  16. font-size: 30px;
  17. }
  18. .item1 {
  19. grid-row-start: 1;
  20. grid-row-end: 3;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <h2>网格行</h2>
  26. <div class="grid-container">
  27. <div class="item1">1</div>
  28. <div class="item2">2</div>
  29. <div class="item3">3</div>
  30. <div class="item4">4</div>
  31. <div class="item5">5</div>
  32. <div class="item6">6</div>
  33. <div class="item7">7</div>
  34. <div class="item8">8</div>
  35. </div>
  36. <p>您可以在放置网格项时引用行号。</p>
  37. </body>
  38. </html>

所有 CSS Grid 属性

属性 描述
column-gap 指定列之间的间距
gap row-gap 与 column-gap 的简写属性
grid grid-template-rows,grid-template-columns, grid-template-areas, grid-auto-rows, grid-auto-columns, 和 grid-auto-flow 的简写属性
grid-area 为网格项目指定一个名称, 或者是 grid-row-start, grid-column-start, grid-row-end, 与 grid-column-end 的简写属性
grid-auto-columns 指定默认列大小
grid-auto-flow 指定如何在网格中插入自动放置的项
grid-auto-rows 指定默认行大小
grid-column 指定 grid-column-start 与 grid-column-end 的简写属性
grid-column-end 指定网格项的结束位置
grid-column-gap 指定列之间间隙的大小
grid-column-start 指定网格项从何处开始
grid-gap A shorthand property for the grid-row-gap and grid-column-gap properties
grid-row 指定 grid-row-start 与 grid-row-end 的简写属性
grid-row-end 指定网格项的结束位置
grid-row-gap 指定行间距的大小
grid-row-start 指定网格项从何处开始
grid-template 指定 grid-template-rows, grid-template-columns 与 grid-areas 的简写属性
grid-template-areas 指定如何使用命名网格项显示列和行
grid-template-columns 指定列的大小,以及网格布局中的列数
grid-template-rows 指定网格布局中行的大小
row-gap 指定网格行之间的间距