Blogger Information
Blog 26
fans 1
comment 1
visits 19462
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
《grid属性常用属性示例》20201224
杨凡的博客
Original
673 people have browsed it

grid布局

列宽、行宽、间隙设置

  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. /* 网格容器 */
  9. /* 网格容器中的每个项目默认都是块级元素 */
  10. .container{
  11. /* 什么支持网格布局元素 */
  12. display: grid;
  13. border: 1px solid #000;
  14. padding: 0.5em;
  15. /* 设置轨道的列宽 */
  16. /* 设置3列2行的布局空间 */
  17. /* 设置列方向的宽度默认auto; */
  18. grid-template-columns: 10em 10em 10em;
  19. /* 设置轨道的行高默认值auto */
  20. grid-template-rows: 5em 5em;
  21. /* 设置轨道的间隙 水平方向间隙 垂直方向间隙*/
  22. gap: 1em 1em;
  23. }
  24. /* 网格项目:网格容器中的子元素 */
  25. .container .item{
  26. background-color: lightcyan;
  27. border: 1px solid #000;
  28. padding: 0.5em;
  29. }
  30. </style>
  31. </head>
  32. <body>
  33. <div class="container">
  34. <span class="item">item1</span>
  35. <span class="item">item2</span>
  36. <span class="item">item3</span>
  37. <span class="item">item4</span>
  38. <span class="item">item5</span>
  39. <span class="item">item6</span>
  40. </div>
  41. </body>
  42. </html>

总结:

  1. display: grid;设置网格布局容器,网格容器中的每个项目默认都是块级元素
  2. grid-template-columns: 10em 10em 10em;设置三列布局,宽度为10em
  3. grid-template-rows: 5em 5em;设置为2行高度为5em
  4. gap: 1em 1em;水平方向间隙 垂直方向间隙 间隙为1em

fr单位使用

  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. display: grid;
  10. border: 1px solid #000;
  11. padding: 0.5em;
  12. grid-template-columns: 10em 10em 10em;
  13. /* 设置轨道宽度时可以使用一个新的单位:fr,类似于flex的伸缩银子 */
  14. /* grid-template-columns: auto auto auto; */
  15. /* fr与auto区别 */
  16. /* 百分比由于列间距的存在会超出范围 */
  17. /* 百分比与fr共存时计算方式:总宽度减去百分比剩余的全部分给fr */
  18. grid-template-columns: 1fr 2fr 1fr;
  19. grid-template-rows: 5em 5em;
  20. /* 设置轨道的间隙 水平方向间隙 垂直方向间隙*/
  21. gap: 1em 1em;
  22. }
  23. .container .item{
  24. background-color: lightcyan;
  25. border: 1px solid #000;
  26. padding: 0.5em;
  27. }
  28. </style>
  29. </head>
  30. <body>
  31. <div class="container">
  32. <span class="item">item1</span>
  33. <span class="item">item2</span>
  34. <span class="item">item3</span>
  35. <span class="item">item4</span>
  36. <span class="item">item5</span>
  37. <span class="item">item6</span>
  38. </div>
  39. </body>
  40. </html>

代码总结:

  1. fr类似于百分比、auto的计算方式,更加灵活的缩放比例
  2. 存在间隙时使用百分比项目会超出范围,fr则不会
  3. 百分比与fr共存时计算方式:总宽度减去百分比剩余的全部分给fr
  4. fr尽量不要与auto,px,em等混合使用

repeat(),minmax()函数的使用

repeat()用法

  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. display: grid;
  10. border: 1px solid #000;
  11. padding: 0.5em;
  12. grid-template-columns: 10em 10em 10em;
  13. /* repeat()第二个参数可以是多个值 */
  14. grid-template-columns: repeat(3,10em);
  15. grid-template-rows: 3em 3em;
  16. /* 设置轨道的间隙 水平方向间隙 垂直方向间隙*/
  17. gap: 1em 1em;
  18. }
  19. .container .item{
  20. background-color: lightcyan;
  21. border: 1px solid #000;
  22. padding: 0.5em;
  23. }
  24. .container1{
  25. display: grid;
  26. border: 1px solid #000;
  27. padding: 0.5em;
  28. grid-template-columns: repeat(3,10em 2em);
  29. grid-template-rows: 3em 3em;
  30. gap: 1em 1em;
  31. }
  32. .container1 .item{
  33. background-color: lightcyan;
  34. border: 1px solid #000;
  35. padding: 0.5em;
  36. }
  37. .container2{
  38. display: grid;
  39. border: 1px solid #000;
  40. padding: 0.5em;
  41. grid-template-columns: repeat(3,10em 2em 1em);
  42. grid-template-rows: 3em 3em;
  43. gap: 1em 1em;
  44. }
  45. .container2 .item{
  46. background-color: lightcyan;
  47. border: 1px solid #000;
  48. padding: 0.5em;
  49. }
  50. .container3{
  51. display: grid;
  52. border: 1px solid #000;
  53. padding: 0.5em;
  54. grid-template-columns: repeat(3,10em) 2em;
  55. grid-template-rows: 3em 3em;
  56. gap: 1em 1em;
  57. }
  58. .container3 .item{
  59. background-color: lightcyan;
  60. border: 1px solid #000;
  61. padding: 0.5em;
  62. }
  63. </style>
  64. </head>
  65. <body>
  66. <div class="container">
  67. <span class="item">item1</span>
  68. <span class="item">item2</span>
  69. <span class="item">item3</span>
  70. <span class="item">item4</span>
  71. <span class="item">item5</span>
  72. <span class="item">item6</span>
  73. </div>
  74. <br>
  75. <div class="container1">
  76. <span class="item">item1</span>
  77. <span class="item">item2</span>
  78. <span class="item">item3</span>
  79. <span class="item">item4</span>
  80. <span class="item">item5</span>
  81. <span class="item">item6</span>
  82. </div>
  83. <br>
  84. <div class="container2">
  85. <span class="item">item1</span>
  86. <span class="item">item2</span>
  87. <span class="item">item3</span>
  88. <span class="item">item4</span>
  89. <span class="item">item5</span>
  90. <span class="item">item6</span>
  91. </div>
  92. <br>
  93. <div class="container3">
  94. <span class="item">item1</span>
  95. <span class="item">item2</span>
  96. <span class="item">item3</span>
  97. <span class="item">item4</span>
  98. <span class="item">item5</span>
  99. <span class="item">item6</span>
  100. </div>
  101. </body>
  102. </html>

代码总结:repeat()有多种写法

  1. grid-template-columns: repeat(3,10em);grid-template-columns: 10em 10em 10em;效果一样
  2. grid-template-columns: repeat(3,10em 2em);grid-template-columns: 10em 2em 10em 2em 10em 2em;效果一样
  3. grid-template-columns: repeat(3,10em 2em 1em);grid-template-columns: 10em 2em 1em 10em 2em 1em;效果一样
  4. grid-template-columns: repeat(3,10em) 2em;grid-template-columns: 10em 10em 10em 2em 10em 10em;效果一样
  5. repeat()可以理解为是简写或者组合写法的函数

minmax()用法

  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. display: grid;
  10. border: 1px solid #000;
  11. padding: 0.5em;
  12. grid-template-columns: 1fr minmax(10em,2fr) 1fr;
  13. grid-template-rows: 3em 3em;
  14. /* 设置轨道的间隙 水平方向间隙 垂直方向间隙*/
  15. gap: 1em 1em;
  16. }
  17. .container .item{
  18. background-color: lightcyan;
  19. border: 1px solid #000;
  20. padding: 0.5em;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <div class="container">
  26. <span class="item">item1</span>
  27. <span class="item">item2</span>
  28. <span class="item">item3</span>
  29. <span class="item">item4</span>
  30. <span class="item">item5</span>
  31. <span class="item">item6</span>
  32. </div>
  33. </body>
  34. </html>

代码总结:

  1. grid-template-columns: 1fr minmax(10em,2fr) 1fr;设置和中间区块最小值10em,其他值是两边区块的两倍
  2. 通过minmax()的应用,可以用它作为媒体查询来使用
  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. display: grid;
  10. border: 1px solid #000;
  11. padding: 0.5em;
  12. grid-template-columns: repeat(3,1fr);
  13. grid-template-rows: 5em 5em;
  14. /* 设置轨道的间隙 水平方向间隙 垂直方向间隙*/
  15. gap: 1em 1em;
  16. grid-auto-rows: 5em;
  17. }
  18. .container .item{
  19. background-color: lightcyan;
  20. border: 1px solid #000;
  21. padding: 0.5em;
  22. }
  23. .container>.item:nth-of-type(5){
  24. background-color: red;
  25. /* grid-area设置任何一个项目所在的网格单元的区域 */
  26. /* grid-area:行起始编号/列起始编号/行结束编码/列结束编号 */
  27. grid-area: 1/1/2/2;
  28. /* 简写 如果跨越超过1行以上或者1列以上就不能简写 */
  29. grid-area: 1/1;
  30. }
  31. </style>
  32. </head>
  33. <body>
  34. <div class="container">
  35. <span class="item">item1</span>
  36. <span class="item">item2</span>
  37. <span class="item">item3</span>
  38. <span class="item">item4</span>
  39. <span class="item">item5</span>
  40. <span class="item">item6</span>
  41. <span class="item">item7</span>
  42. <span class="item">item8</span>
  43. <span class="item">item9</span>
  44. </div>
  45. </body>
  46. </html>

文章总结:

指定一个容器使用网格布局

  • display: grid: 网格容器默认都是”块级元素”
  • display: inline-grid: 可以设置为”行内元素”

用于设置每一个单元格大小与显示方式

  • grid-template-columns: 定义每一列的列宽
  • grid-template-rows:: 定义每一行的行高

用于设置行与列排列方式

  • grid-auto-flow:row;: 行优先排列,设置隐式轨道行高grid-auto-rows
  • grid-auto-flow:column;: 列优先排列,设置隐式轨道列宽grid-auto-columns

用于设置行与列之间的间隙(行间距/列间距)

  • grid-columns-gap: 定义每一列的列宽
  • grid-row-gap:: 定义每一行的行高

用于自定义容器的显示位置

  • grid-area: 定义每一列的列宽,设置任何一个项目所在的网格单元的区域,grid-area:行起始编号/列起始编号/行结束编码/列结束编号
  • 数量不同意义不同
  • 值中有span,单值:跨的行数;双值:跨的行与列数,如果只想设置列数,就必须要设置行数(auto)
  • 值中有span和编号,双值:没有span,默认跨1行1列;三值:省了列结束编号或跨的数量,前面的值可使用auto。
Correcting teacher:天蓬老师天蓬老师

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!