Blogger Information
Blog 15
fans 2
comment 0
visits 35300
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
grid布局 (容器建立、网格、区域、设置单元格数量大小)
w™下載一個妳
Original
2385 people have browsed it

1.创建容器代码:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>创建grid容器</title>
  7. <style>
  8. .container {
  9. /* 容器大小 */
  10. width: 400px;
  11. height: 400px;
  12. background-color: rgb(85, 124, 196);
  13. /* 创建grid容器 */
  14. display: grid;
  15. /* 设置项目在网格中的填充方案,默认行优先 */
  16. grid-auto-flow: row;
  17. /* 设置项目在网格中的填充方案,列优先 */
  18. /* grid-auto-flow: column; */
  19. /* 显示地划分行与列,三列二行 */
  20. grid-template-columns: 100px 100px 100px;
  21. grid-template-rows: 100px 100px;
  22. /* 对于放置不下的项目会隐式生成单元格 */
  23. grid-auto-rows: auto;
  24. grid-auto-rows: 160px;
  25. }
  26. .item {
  27. background-color: yellowgreen;
  28. font-size: 1.8rem;
  29. text-align: center;
  30. }
  31. </style>
  32. </head>
  33. <body>
  34. <div class="container">
  35. <div class="item item1">1</div>
  36. <div class="item item2">2</div>
  37. <div class="item item3">3</div>
  38. <div class="item item4">4</div>
  39. <div class="item item5">5</div>
  40. <div class="item item6">6</div>
  41. <div class="item item7">7</div>
  42. </div>
  43. </body>
  44. </html>

1.1创建容器演练图:


2.设置单元格的数量与大小代码:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>设置单元格的数量与大小</title>
  7. <style>
  8. .container {
  9. /* 容器大小 */
  10. width: 400px;
  11. height: 400px;
  12. background-color: rgb(85, 124, 196);
  13. /* 创建grid容器 */
  14. display: grid;
  15. /* 固定值 */
  16. grid-template-columns: 100px 100px 100px;
  17. grid-template-rows: 100px 100px 100px;
  18. /* 百分比 */
  19. grid-template-columns: 30% 20% auto;
  20. grid-template-rows: 100px 100px 100px;
  21. /* 按比例 */
  22. grid-template-columns: 1fr 1fr 1fr;
  23. grid-template-rows: 1fr 1fr auto;
  24. /* 重复设置 */
  25. grid-template-columns: repeat(3, 100px);
  26. grid-template-rows: repeat(3, 100px);
  27. /* 按分组来设置 */
  28. grid-template-columns: repeat(2, 50px 100px);
  29. grid-template-rows: repeat(2, 50px 100px);
  30. /* 弹性设置 */
  31. grid-template-columns: repeat(2, minmax(50px, 100px));
  32. grid-template-rows: repeat(3, minmax(150px, 1fr));
  33. /* 自动填充 */
  34. grid-template-columns: repeat(auto-fill, 100px);
  35. grid-template-rows: repeat(auto-fill, 100px);
  36. }
  37. .item {
  38. background-color: yellowgreen;
  39. font-size: 1.8rem;
  40. }
  41. </style>
  42. </head>
  43. <body>
  44. <div class="container">
  45. <div class="item item1">1</div>
  46. <div class="item item2">2</div>
  47. <div class="item item3">3</div>
  48. <div class="item item4">4</div>
  49. <div class="item item5">5</div>
  50. <div class="item item6">6</div>
  51. <div class="item item7">7</div>
  52. </div>
  53. </body>
  54. </html>

2.1设置单元格的数量与大小演练图:


3.使用默认的网格线来划分单元格代码:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>使用默认的网格线来划分单元格</title>
  7. <style>
  8. .container {
  9. /* 容器大小 */
  10. width: 400px;
  11. height: 400px;
  12. background-color: rgb(85, 124, 196);
  13. grid-template-columns: repeat(4, 1fr);
  14. grid-template-rows: repeat(4, 1fr);
  15. /* 创建grid容器 */
  16. display: grid;
  17. }
  18. .item {
  19. font-size: 1.8rem;
  20. }
  21. .item.item1 {
  22. background-color: yellowgreen;
  23. /* 开始行数1 */
  24. grid-row-start: 1;
  25. /* 结束行数3 */
  26. grid-row-end: 3;
  27. /* 开始列数1 */
  28. grid-column-start: 1;
  29. /* 结束列数3 */
  30. grid-column-end: 3;
  31. /* 反方向开始 */
  32. /* 开始行数-1 */
  33. /* grid-row-start: -1; */
  34. /* 结束行数-3 */
  35. /* grid-row-end: -3; */
  36. /* 开始列数1 */
  37. /* grid-column-start: -1; */
  38. /* 结束列数3 */
  39. /* grid-column-end: -3; */
  40. /* 设置中间区域 */
  41. /* grid-row-start: 2;
  42. grid-row-end: 4;
  43. grid-column-start: 2;
  44. grid-column-end: 4; */
  45. /* 完全覆盖所有空间 */
  46. /* grid-row-start: 1;
  47. grid-row-end: -1;
  48. grid-column-start: 1;
  49. grid-column-end: -1; */
  50. }
  51. /* 简写1 */
  52. .item.item2 {
  53. background-color: violet;
  54. grid-row: 1/3;
  55. grid-column: 3/5;
  56. }
  57. /* 使用偏移量来简化,将第三个移到左下角 */
  58. .item.item3 {
  59. background-color: turquoise;
  60. grid-row: 3 / span 2;
  61. grid-column: 1 / span 2;
  62. }
  63. /* 简写最后一个项目占据剩余空间 */
  64. .item.item4 {
  65. background-color: tomato;
  66. grid-row-end: span 2;
  67. grid-column-end: span 2;
  68. }
  69. </style>
  70. </head>
  71. <body>
  72. <div class="container">
  73. <div class="item item1">1</div>
  74. <div class="item item2">2</div>
  75. <div class="item item3">3</div>
  76. <div class="item item4">4</div>
  77. </div>
  78. </body>
  79. </html>

3.1使用默认的网格线来划分单元格演练图:


4使用命名网格线来划分单元格代码:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>使用命名网格线来划分单元格</title>
  7. <style>
  8. .container {
  9. /* 容器大小 */
  10. width: 400px;
  11. height: 400px;
  12. background-color: wheat;
  13. /* 创建grid容器 */
  14. display: grid;
  15. grid-template-columns: [c1-start] 100px [c1-end c2-start] 100px [c2-end c3-start] 100px [c3-end];
  16. grid-template-rows: [r1-start] 100px [r1-end r2-start] 100px [r2-end r3-start] 100px [r3-end];
  17. }
  18. .item {
  19. font-size: 2rem;
  20. }
  21. .item.item1 {
  22. background-color: lightgreen;
  23. /* 默认就是跨越一行/一列,所以可以省略 */
  24. grid-row-start: r2-start;
  25. grid-row-start: r1-end;
  26. grid-column-start: c3-start;
  27. }
  28. /* 简写 */
  29. .item.item2 {
  30. background-color: lightpink;
  31. /* grid-row: r1-start / r2-start;
  32. grid-column: c1-start / c3-end; */
  33. grid-column-end: span 3;
  34. }
  35. /* 使用偏移量来简化, 将第三个移动到左下角 */
  36. .item.item3 {
  37. background-color: yellow;
  38. grid-row-end: span 2;
  39. grid-column-end: span 2;
  40. }
  41. .item.item4 {
  42. background-color: lightgrey;
  43. }
  44. </style>
  45. </head>
  46. <body>
  47. <div class="container">
  48. <div class="item item1">1</div>
  49. <div class="item item2">2</div>
  50. <div class="item item3">3</div>
  51. <div class="item item4">4</div>
  52. </div>
  53. </body>
  54. </html>

4.1使用命名网格线来划分单元格演练图:


5默认网格区域代码:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>默认网格区域</title>
  7. <style>
  8. .container {
  9. /* 容器大小 */
  10. width: 400px;
  11. height: 400px;
  12. background-color: wheat;
  13. /* 创建grid容器 */
  14. display: grid;
  15. grid-template-columns: repeat(4, 1fr);
  16. grid-template-rows: repeat(4, 1fr);
  17. }
  18. .item {
  19. font-size: 2rem;
  20. }
  21. .item.item1 {
  22. background-color: lightgreen;
  23. /* grid-area: 1 / 1 / 2 / 5; */
  24. /* 用偏移量进行简化 */
  25. /* grid-area: 1 / 1 / span 1 / span 4; */
  26. /* 是从当前位置开始的填充 */
  27. grid-area: span 1 / span 4;
  28. }
  29. /* 简写 */
  30. .item.item2 {
  31. background-color: lightpink;
  32. /* grid-area: 2 / 1 / 4 / 2; */
  33. /* grid-area: span 2 / span 1; */
  34. /* 默认就是偏移一行/一列 */
  35. grid-area: span 2;
  36. }
  37. /* 使用偏移量来简化, 将第三个移动到左下角 */
  38. .item.item3 {
  39. background-color: yellow;
  40. }
  41. .item.item4 {
  42. background-color: lightgrey;
  43. /* grid-area: row-start / col-start / row-end / col-end; */
  44. }
  45. .item.item5 {
  46. background-color: violet;
  47. }
  48. </style>
  49. </head>
  50. <body>
  51. <div class="container">
  52. <div class="item item1">1</div>
  53. <div class="item item2">2</div>
  54. <div class="item item3">3</div>
  55. <div class="item item4">4</div>
  56. <div class="item item5">5</div>
  57. </div>
  58. </body>
  59. </html>

5.1默认网格区域演练图:


6.命名网格区域代码:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>命名网格区域</title>
  7. <style>
  8. .container {
  9. /* 容器大小 */
  10. width: 400px;
  11. height: 400px;
  12. background-color: wheat;
  13. /* 创建grid容器 */
  14. display: grid;
  15. grid-template-columns: 80px 1fr 80px;
  16. grid-template-rows: 40px 1fr 40px;
  17. /* 设置命名网格区域, 相同名称的命名区域会合并 */
  18. grid-template-areas:
  19. "hello hello hello"
  20. "left main right"
  21. "footer footer footer";
  22. }
  23. .item {
  24. font-size: 2rem;
  25. }
  26. .item.item1 {
  27. background-color: lightgreen;
  28. grid-area: hello;
  29. }
  30. /* 简写 */
  31. .item.item2 {
  32. background-color: lightpink;
  33. grid-area: left;
  34. }
  35. /* 使用偏移量来简化, 将第三个移动到左下角 */
  36. .item.item3 {
  37. background-color: yellow;
  38. grid-area: main;
  39. }
  40. .item.item4 {
  41. background-color: lightgrey;
  42. grid-area: right;
  43. }
  44. .item.item5 {
  45. background-color: violet;
  46. grid-area: footer;
  47. }
  48. </style>
  49. </head>
  50. <body>
  51. <div class="container">
  52. <div class="item item1">1</div>
  53. <div class="item item2">2</div>
  54. <div class="item item3">3</div>
  55. <div class="item item4">4</div>
  56. <div class="item item5">5</div>
  57. </div>
  58. </body>
  59. </html>

6.1命名网格区域演练图:


7.网格区域线的默认名称代码:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>网格区域线的默认名称</title>
  7. <style>
  8. .container {
  9. /* 容器大小 */
  10. width: 400px;
  11. height: 400px;
  12. background-color: wheat;
  13. /* 创建grid容器 */
  14. display: grid;
  15. grid-template-columns: 80px 1fr;
  16. grid-template-rows: 40px 1fr 40px;
  17. /* 设置命名网格区域, 相同名称的命名区域会合并 */
  18. grid-template-areas:
  19. "header header"
  20. ". . "
  21. "footer footer";
  22. }
  23. .item {
  24. font-size: 2rem;
  25. }
  26. .item.item1 {
  27. background-color: lightgreen;
  28. grid-area: header-start / header-start / header-end / header-end;
  29. }
  30. /* 简写 */
  31. .item.item2 {
  32. background-color: lightpink;
  33. /* 多余 */
  34. /* grid-area: left; */
  35. }
  36. /* 使用偏移量来简化, 将第三个移动到左下角 */
  37. .item.item3 {
  38. background-color: yellow;
  39. /* grid-area: main; */
  40. }
  41. .item.item4 {
  42. background-color: lightgrey;
  43. grid-area: footer-start / footer-start / footer-end / footer-end;
  44. }
  45. </style>
  46. </head>
  47. <body>
  48. <div class="container">
  49. <div class="item item1">1</div>
  50. <div class="item item2">2</div>
  51. <div class="item item3">3</div>
  52. <div class="item item4">4</div>
  53. </div>
  54. </body>
  55. </html>

7.1网格区域线的默认名称演练图:


8.1设置某个项目在单元格中的对齐方式代码:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>设置某个项目在单元格中的对齐方式</title>
  7. <style>
  8. .container {
  9. /* 容器大小 */
  10. width: 400px;
  11. height: 400px;
  12. background-color: wheat;
  13. /* 创建grid容器 */
  14. display: grid;
  15. grid-template-columns: repeat(3, 1fr);
  16. grid-template-rows: repeat(3, 1fr);
  17. justify-items: stretch;
  18. align-items: stretch;
  19. justify-items: start;
  20. align-items: center;
  21. justify-items: center;
  22. align-items: center;
  23. /* place-items: 垂直 水平; */
  24. place-items: start end;
  25. place-items: center center;
  26. place-items: center;
  27. }
  28. .item {
  29. width: 50px;
  30. height: 50px;
  31. background-color: violet;
  32. font-size: 2rem;
  33. }
  34. .item.item5 {
  35. justify-self: end;
  36. align-self: end;
  37. place-self: center end;
  38. }
  39. </style>
  40. </head>
  41. <body>
  42. <div class="container">
  43. <div class="item item1">1</div>
  44. <div class="item item2">2</div>
  45. <div class="item item3">3</div>
  46. <div class="item item4">4</div>
  47. <div class="item item5">5</div>
  48. <div class="item item6">6</div>
  49. <div class="item item7">7</div>
  50. <div class="item item8">8</div>
  51. <div class="item item9">9</div>
  52. </div>
  53. </body>
  54. </html>

8.1设置某个项目在单元格中的对齐方式演练图:


9.学习总结

这节课的知识点很多,要花很多时间去消化。

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:网格布局的属性非常多, 其实咱们课上只讲了大约一半左右 , 还有许多, 要看手册的, mdn上有
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