Blogger Information
Blog 16
fans 0
comment 0
visits 6824
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
隐式网格, 对齐方式与行列间隙的设置方式与使用场景、前提
Stonegarlic
Original
395 people have browsed it

1027作业

grid布局显示网格与项目属性

  1. <body>
  2. <div class="container">
  3. <div class="item">item1</div>
  4. </div>
  5. <style>
  6. .container{
  7. width: 300px;
  8. height:100px;
  9. display: grid;
  10. /* 显示网格 */
  11. /* 3列3行 宽度为100的50 */
  12. /* grid-template-columns: 100px 100px 100px;
  13. grid-template-rows: 50px 50px 50px; */
  14. /* 3列3行 宽度为100的50 */
  15. /* grid-template-columns: repeat(3, 100px);
  16. grid-template-rows: repeat(3, 50px); */
  17. /* 显示网格 repeat:重复 fr:为比例 */
  18. grid-template-columns: repeat(3,1fr);
  19. grid-template-rows: repeat(3,1fr);
  20. }
  21. .container > .item{
  22. background-color: bisque;
  23. /* 起始行 / 结束行*/
  24. grid-row: 3/4;
  25. /* 起始列/结束列 */
  26. grid-column:1/2;
  27. grid-row: 1 /span 1;
  28. grid-column: 1 / span 1;
  29. /* grid-area: 行开始/列开始/行结束/列结束; */
  30. grid-area: 3/1/span 1/span 3;
  31. }
  32. </style>

实例效果

grid布局隐式网格与与排列规则

  1. <title>grid隐式网格与与排列规则</title>
  2. </head>
  3. <body>
  4. <div class="container">
  5. <div class="item">item-1</div>
  6. <div class="item">item-2</div>
  7. <div class="item">item-3</div>
  8. <div class="item">item-4</div>
  9. <div class="item">item-5</div>
  10. <div class="item">item-6</div>
  11. <div class="item">item-7</div>
  12. <div class="item">item-8</div>
  13. <div class="item">item-9</div>
  14. <!-- 在加2个项目 -->
  15. <div class="item other">item-10</div>
  16. <div class="item other">item-11</div>
  17. </div>
  18. <style>
  19. .container{
  20. width: 400px;
  21. height: 150px;
  22. display: grid;
  23. /* 显示网格绘制 */
  24. grid-template-rows: repeat(3,1fr);
  25. grid-template-columns: repeat(3,1fr);
  26. /* 1.排列规则 默认按照行排列*/
  27. grid-auto-flow: row;
  28. /* 列 排列 */
  29. /* grid-auto-flow: column; */
  30. /* 2.隐式网格 多余的项目,出现在隐式网格中*/
  31. grid-auto-rows: 1fr;
  32. /* 列优先排列 */
  33. grid-auto-flow: column;
  34. /* 设置隐式网格的列宽 */
  35. grid-auto-columns: 100px;
  36. }
  37. .container > .item{
  38. background-color: antiquewhite;
  39. }
  40. .container > .item.other{
  41. background-color: orange;
  42. }
  43. /* 排列规则:grid-auto-flow:row/column 行优先与列有优先
  44. 隐式网格:grid-auto-row/colum 设置隐式网格的行与列
  45. */
  46. </style>
  47. </body>

实例效果

grid布局项目在容器中对齐与项目在单元格中对齐

  1. <title>grid项目在容器中对齐与项目在单元格中对齐</title>
  2. </head>
  3. <body>
  4. <div class="container">
  5. <div class="item">item-1</div>
  6. <div class="item">item-2</div>
  7. <div class="item">item-3</div>
  8. <div class="item">item-4</div>
  9. <div class="item">item-5</div>
  10. <div class="item">item-6</div>
  11. <div class="item">item-7</div>
  12. <div class="item">item-8</div>
  13. <div class="item">item-9</div>
  14. </div>
  15. <style>
  16. /**
  17. * * 1. 对齐前提: 必须存在"剩余空间"
  18. * * 2. 对齐方案: "剩余空间"在"项目"之间的分配方式
  19. * * 3. 剩余空间: Flex(主轴, 交叉轴), Grid(容器, 单元格)
  20. * * Grid: 剩余空间存在于"容器" 或 "单元格"
  21. * * 容器中: place-content, place-items
  22. * * 项目中: place-self
  23. */
  24. .container{
  25. background-color: aqua;
  26. border: 1px solid;
  27. /* 创建网格剩余空间 */
  28. width: 600px;
  29. height: 600px;
  30. display: grid;
  31. grid-template-columns: repeat(3,100px);
  32. grid-template-rows: repeat(3,100px);
  33. /* 1.项目在容器中对齐 */
  34. /* place-content: 垂直方向 水平方向; */
  35. /* 默认垂直居上 水平居左 */
  36. /* place-content: start start; */
  37. /* 垂直居中 水平居中 */
  38. /* place-content: center center; */
  39. /* 垂直居中 水平居右 */
  40. place-content: center end;
  41. /* 重复值可以省略 */
  42. place-content: center;
  43. place-content: space-between space-around;
  44. place-content: space-between ;
  45. place-content: space-around;
  46. /* ---------------------------- */
  47. /*2.项目在单元格中对齐 */
  48. /* 项目在单元格中要有剩余空间才能实现对其, */
  49. /* place-items: 垂直方向 水平方向; */
  50. /* 默认垂直居上 水平居左 */
  51. /* place-items: start; */
  52. /* 垂直居中 水平居中 */
  53. place-items: center;
  54. /* 垂直居中 水平居右 */
  55. /* place-items: center end; */
  56. /* place-items: space-between space-between; */
  57. }
  58. .container > .item{
  59. background-color: antiquewhite;
  60. width: 80px;
  61. height: 80px;
  62. place-items: space-between space-between;
  63. }
  64. /* 设置某个项目在单元格中单独设置 */
  65. .container > .item:nth-child(5){
  66. background-color:aquamarine;
  67. /* 选中单独设置某个单元格 */
  68. place-self: end;
  69. }
  70. </style>
  71. </body>

实例效果

grid布局中的行列间隙 gap

  1. title>grid布局中的行列间隙</title>
  2. </head>
  3. <body>
  4. <div class="container">
  5. <div class="item">item-1</div>
  6. <div class="item">item-2</div>
  7. <div class="item">item-3</div>
  8. <div class="item">item-4</div>
  9. <div class="item">item-5</div>
  10. <div class="item">item-6</div>
  11. <div class="item">item-7</div>
  12. <div class="item">item-8</div>
  13. <div class="item">item-9</div>
  14. </div>
  15. <style>
  16. .container{
  17. background-color: antiquewhite;
  18. display: grid;
  19. width: 400px;
  20. height: 400px;
  21. border: 2px solid;
  22. grid-template-columns: repeat(3,100px);
  23. grid-template-rows: repeat(3,100px);
  24. /* 行列间隙 */
  25. /* gap:垂直方向,水平方向 */
  26. gap:10px;
  27. }
  28. .container > .item{
  29. background-color: aqua;
  30. /* margin: 10px; */
  31. }
  32. </style>
  33. </body>

案例效果

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