Blogger Information
Blog 16
fans 0
comment 0
visits 5720
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Grid常用的容器与项目属性学习与应用
Sinan学习博客
Original
410 people have browsed it

Grid常用的容器与项目属性学习与应用

1. 显式网格

  • display: grid; 将容器设置为网格容器
  • grid-template-columns;设置网格的列数
  • grid-template-rows;设置网格的行数
  1. <div class="container"></div>
  2. <style>
  3. .container {
  4. width: 300px;
  5. height: 150px;
  6. display: grid;
  7. /* grid-template-columns: repeat(3, 100px);
  8. grid-template-rows: repeat(3, 50px); */
  9. grid-template-columns: repeat(3, 1fr);
  10. grid-template-rows: repeat(3, 1fr);
  11. }
  12. </style>

1.1 给网格添加项目(子项目)

这里设置了9个网格,子项目默认会按顺序从左到右,从上到下排列。

  1. <div class="container">
  2. <div>1</div>
  3. <div>2</div>
  4. <div>3</div>
  5. <div>4</div>
  6. <div>5</div>
  7. <div>6</div>
  8. <div>7</div>
  9. <div>8</div>
  10. <div>9</div>
  11. </div>
  12. <style>
  13. .container {
  14. width: 300px;
  15. height: 150px;
  16. display: grid;
  17. /* grid-template-columns: repeat(3, 100px);
  18. grid-template-rows: repeat(3, 50px); */
  19. grid-template-columns: repeat(3, 1fr);
  20. grid-template-rows: repeat(3, 1fr);
  21. place-items: center;
  22. }
  23. .container > div {
  24. font-size: 20px;
  25. font-weight: bold;
  26. }
  27. </style>

1.2 移动网格里的项目

  • 1个项目至少占据1个单元格,1个单元格默认占据1行1列
  • grid-column: 2/3;设置项目的列号
  • grid-row: 2/3;设置项目的行号
  • 如果项目只跨越1个单元格,可以简写,grid-column:2; grid-row:2;
  1. <div class="container">
  2. <div class="item">1</div>
  3. </div>
  4. <style>
  5. .container {
  6. width: 300px;
  7. height: 150px;
  8. display: grid;
  9. grid-template-columns: repeat(3, 1fr);
  10. grid-template-rows: repeat(3, 1fr);
  11. }
  12. .container > .item:first-child {
  13. font-size: 20px;
  14. font-weight: bold;
  15. background-color: orange;
  16. /*将网格1行1列的项目,移动到网格2行2列*/
  17. grid-column: 2/3;
  18. grid-row: 2/3;
  19. }
  20. </style>

1.2 跨越多个单元格

  • 可以指定行号:

    • grid-row: 2 / 4;
    • grid-column: 2 / 4;
  • 可以指定跨越单元格的步长(个数)

    • grid-row: 2 / span 2;
    • grid-column: 2 / span 2;
  • 项目跨越多个单元格形成1个网格区域,可以用grid-area设置

    • grid-area:行开始 / 列开始 / 行结束 / 列结束
  1. <div class="container">
  2. <div class="item">1</div>
  3. </div>
  4. <style>
  5. .container {
  6. width: 300px;
  7. height: 150px;
  8. display: grid;
  9. grid-template-columns: repeat(3, 1fr);
  10. grid-template-rows: repeat(3, 1fr);
  11. }
  12. .container > .item:first-child {
  13. font-size: 20px;
  14. font-weight: bold;
  15. background-color: orange;
  16. padding: 10px;
  17. /* 可以指定行号: */
  18. /* grid-row: 2 / 4; */
  19. /* grid-column: 2 / 4; */
  20. /* 可以指定跨域单元格的步长(个数) */
  21. /* grid-row: 2 / span 2; */
  22. /* grid-column: 2 / span 2; */
  23. /* 项目跨越多个单元格形成1个网格区域,可以用grid-area设置 */
  24. /* grid-area:行开始 / 列开始 / 行结束 / 列结束 */
  25. grid-area: 2 / 2 / span 2 / span 2;
  26. }
  27. </style>

2. 排列规则

项目在网格中的排列规则:

  • gird-auto-flow:row; 默认排列方式,从左到右
  • gird-auto-flow:column; 从上到下


3. 隐式网格

  • 当超出显式网格数量的项目,会创建在隐式网格中,隐式网格会自动生成。
  • 超出网格的项目,自动显示在隐式网格,高度默认式内容高度。
  • 用grid-auto-rows属性能使容器内的项目高度一致。
  1. <div class="container">
  2. <div class="item">item1</div>
  3. <div class="item">item2</div>
  4. <div class="item">item3</div>
  5. <div class="item">item4</div>
  6. <div class="item">item5</div>
  7. <div class="item">item6</div>
  8. <div class="item">item7</div>
  9. <div class="item">item8</div>
  10. <div class="item">item9</div>
  11. <!-- 超出网格的项目 -->
  12. <div class="item">item10</div>
  13. <div class="item">item11</div>
  14. <div class="item">item12</div>
  15. </div>
  16. <style>
  17. .container {
  18. width: 300px;
  19. height: 150px;
  20. display: grid;
  21. grid-template-columns: repeat(3, 1fr);
  22. grid-template-rows: repeat(3, 1fr);
  23. grid-auto-flow: row;
  24. /* 超出网格的项目,自动显示在隐式网格,高度默认式内容高度。 */
  25. /* 用grid-auto-rows属性能使容器内的项目高度一致。 */
  26. grid-auto-rows: 1fr;
  27. }
  28. .container > .item {
  29. font-size: 16px;
  30. font-weight: bold;
  31. background-color: orange;
  32. }

4. 项目在容器中的对齐

  1. <div class="container">
  2. <div class="item">item1</div>
  3. <div class="item">item2</div>
  4. <div class="item">item3</div>
  5. <div class="item">item4</div>
  6. <div class="item">item5</div>
  7. <div class="item">item6</div>
  8. <div class="item">item7</div>
  9. <div class="item">item8</div>
  10. <div class="item">item9</div>
  11. </div>
  12. <style>
  13. .container {
  14. width: 450px;
  15. height: 250px;
  16. display: grid;
  17. grid-template-columns: repeat(3, 100px);
  18. grid-template-rows: repeat(3, 50px);
  19. grid-auto-flow: row;
  20. /* 超出网格的项目,自动显示在隐式网格,高度默认式内容高度。 */
  21. /* 用grid-auto-rows属性能使容器内的项目高度一致。 */
  22. grid-auto-rows: 1fr;
  23. background-color: aqua;
  24. border: 1px solid #000;
  25. /* place-content: center start; */
  26. /* place-content: space-around space-between; */
  27. /* place-content: space-between; */
  28. place-content: space-evenly;
  29. }
  30. .container > .item {
  31. font-size: 16px;
  32. font-weight: bold;
  33. background-color: orange;
  34. }
  35. </style>






4. 项目在单元格中的对齐

  • place-items: center center;
  • place-items: center;
  • place-items: end;
  • place-items: srart end;
  • place-items: center end;
  • place-items: staft;
  • place-items: center start;

5. 设置网格间距 gap

  1. <div class="container">
  2. <div class="item">item1</div>
  3. <div class="item">item2</div>
  4. <div class="item">item3</div>
  5. <div class="item">item4</div>
  6. <div class="item">item5</div>
  7. <div class="item">item6</div>
  8. <div class="item">item7</div>
  9. <div class="item">item8</div>
  10. <div class="item">item9</div>
  11. </div>
  12. <style>
  13. .container {
  14. width: 450px;
  15. height: 450px;
  16. display: grid;
  17. grid-template-columns: repeat(3, 1fr);
  18. grid-template-rows: repeat(3, 1fr);
  19. grid-auto-flow: row;
  20. grid-auto-rows: 1fr;
  21. background-color: aqua;
  22. border: 1px solid #000;
  23. /*设置行间距20px,列间距10px*/
  24. gap: 20px 10px;
  25. }
  26. .container > .item {
  27. font-size: 16px;
  28. font-weight: bold;
  29. background-color: orange;
  30. }
  31. </style>

6. 仿PHP.CN模块

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>案例</title>
  8. <style>
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. box-sizing: border-box;
  13. }
  14. html {
  15. font-size: 1rem;
  16. }
  17. body {
  18. background-color: #f5f5f5;
  19. color: #666;
  20. }
  21. .container {
  22. /* background-color: #eee; */
  23. /* border: 1px solid #000; */
  24. padding: 0 10px;
  25. }
  26. a {
  27. text-decoration: none;
  28. color: #666;
  29. }
  30. /* 移动端优先,由小屏到大屏 */
  31. @media (max-width: 375px) {
  32. /* 当屏幕宽度小于375px时,使用这里面的CSS规则 */
  33. .container {
  34. margin: 20px auto;
  35. width: 375px;
  36. }
  37. .container > .courseList {
  38. grid-template-columns: repeat(2, 1fr);
  39. grid-template-rows: repeat(5, 1fr);
  40. }
  41. }
  42. @media (min-width: 375px) and (max-width: 640px) {
  43. /* 当屏幕宽度介于375px--640px之间时,使用这里的css规则 */
  44. .container {
  45. margin: 50px auto;
  46. max-width: 640px;
  47. }
  48. .container > .courseList {
  49. grid-template-columns: repeat(3, 1fr);
  50. grid-template-rows: repeat(4, 1fr);
  51. }
  52. }
  53. @media (min-width: 640px) {
  54. /* 当屏幕大于640px时,使用这里面的CSS规则 */
  55. .container {
  56. margin: 50px auto;
  57. max-width: 1200px;
  58. }
  59. .container > .courseList {
  60. grid-template-columns: repeat(5, 1fr);
  61. grid-template-rows: repeat(2, 1fr);
  62. }
  63. }
  64. .container > .newCourse {
  65. height: 80px;
  66. display: flex;
  67. place-content: space-between;
  68. place-items: center;
  69. }
  70. .container > .courseList {
  71. /* height: 400px; */
  72. /* background: #ccc; */
  73. display: grid;
  74. gap: 30px 20px;
  75. }
  76. figure {
  77. border-radius: 8px;
  78. overflow: hidden;
  79. background-color: #fff;
  80. }
  81. figure > img {
  82. width: 100%;
  83. }
  84. figure > figcaption {
  85. font-size: 0.875rem;
  86. line-height: 1.5rem;
  87. padding: 10px;
  88. }
  89. figure > figcaption > span {
  90. background-color: bisque;
  91. color: #f90;
  92. padding: 2px 5px;
  93. font-size: small;
  94. margin-right: 10px;
  95. }
  96. figure > p {
  97. display: flex;
  98. place-content: space-between;
  99. font-size: smaller;
  100. padding: 10px;
  101. }
  102. </style>
  103. </head>
  104. <body></body>
  105. <div class="container">
  106. <div class="newCourse">
  107. <h3 class="left">最新课程</h3>
  108. <p class="right"><a href="#">更多 ></a></p>
  109. </div>
  110. <div class="courseList">
  111. <figure>
  112. <img src="img/63bbd15353895588.jpg" />
  113. <figcaption><span>初级</span><a href="#">零基础前端课程【Vue进阶学习及实战应用】</a></figcaption>
  114. <p><span>1124次学习</span><span>收藏</span></p>
  115. </figure>
  116. <figure>
  117. <img src="img/63be61f9c82a1759.jpg" />
  118. <figcaption><span>初级</span>Vue3.0从0到搭建通用后台管理系统项目实战</figcaption>
  119. <p><span>1124次学习</span><span>收藏</span></p>
  120. </figure>
  121. <figure>
  122. <img src="img/63bfcb2974f91471.jpg" />
  123. <figcaption><span>初级</span>前端项目-尚优选【HTML/CSS/JS技术综合实</figcaption>
  124. <p><span>1124次学习</span><span>收藏</span></p>
  125. </figure>
  126. <figure>
  127. <img src="img/63c251a5378af408.jpg" />
  128. <figcaption><span>初级</span>极致CMS零基础建站教学视频</figcaption>
  129. <p><span>1124次学习</span><span>收藏</span></p>
  130. </figure>
  131. <figure>
  132. <img src="img/63c4f5a167a4b379.jpg" />
  133. <figcaption><span>初级</span>WordPress零基础新手搭建个人博客及企业网站</figcaption>
  134. <p><span>1124次学习</span><span>收藏</span></p>
  135. </figure>
  136. <figure>
  137. <img src="img/63c64f278df45107.jpg" />
  138. <figcaption><span>初级</span><a href="#">帝国CMS企业仿站教程</a></figcaption>
  139. <p><span>1124次学习</span><span>收藏</span></p>
  140. </figure>
  141. <figure>
  142. <img src="img/63d8e62db5e5c880.png" />
  143. <figcaption><span>初级</span>PHP+MySQL網站程式入門課(廣東話/粵語) 梁浩賢</figcaption>
  144. <p><span>1124次学习</span><span>收藏</span></p>
  145. </figure>
  146. <figure>
  147. <img src="img/63db62f3d0b65880.jpg" />
  148. <figcaption><span>初级</span>Apipost从入门到精通</figcaption>
  149. <p><span>1124次学习</span><span>收藏</span></p>
  150. </figure>
  151. <figure>
  152. <img src="img/63db6ec52e1a3840.jpg" />
  153. <figcaption><span>初级</span>PHP入门速学(台湾同胞版)</figcaption>
  154. <p><span>1124次学习</span><span>收藏</span></p>
  155. </figure>
  156. <figure>
  157. <img src="img/63e202276944f543.jpg" />
  158. <figcaption><span>初级</span>Laravel 8 课程精讲(台湾同胞版)</figcaption>
  159. <p><span>1124次学习</span><span>收藏</span></p>
  160. </figure>
  161. </div>
  162. </div>
  163. </html>

PC

ipad

手机

Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:完成的非常好,总结也认真齐全,继续加油
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