image.png

用grid实现

用grid实现,只需要在容器上声明为grid容器,并且设置行列的宽度分片三个1fr,设置列的间距gap间距就ok了
三步骤

  • 声明grid容器;
  • 设置使用分片设置列宽,行也可以省略,那样就变成更自适应了,不再限制于九个元素;
  • 设置gap,行列的间距 ```css / grid实现 /

    container-grid {

    display: grid; grid-template-columns: 1fr 1fr 1fr; grid-template-rows: 1fr 1fr 1fr; grid-row-gap: 20px; grid-column-gap: 20px; }
  1. <a name="vnEeY"></a>
  2. ### flex实现比较麻烦
  3. 需要用margin控制间距,并在容器上设置便宜,让最左边和最上边的元素多余的margin切去。容器内元素的宽度也需要计算,好在也不是太难,也不需要js控制。<br />具体步骤:
  4. - 容器声明flex,设置容器内元素可以换行
  5. - 设置元素的margin-top和margin-left做间距效果
  6. - 用css计算属性计算元素的宽度
  7. - 设置容器的margin-top和margin-left把最左边和最上边多余的margin切去
  8. ```css
  9. /* flex实现 */
  10. #container-flex {
  11. display: flex;
  12. flex-wrap: wrap;
  13. margin-top: -20px;
  14. margin-left: -20px;
  15. }
  16. .item-flex {
  17. font-size: 4em;
  18. text-align: center;
  19. width: calc((100% - 60px) / 3);
  20. margin-top: 20px;
  21. margin-left: 20px;
  22. }

完整代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  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>Document</title>
  8. <style>
  9. html,
  10. body {
  11. margin: 0;
  12. padding: 0;
  13. background-color: black;
  14. box-sizing: border-box;
  15. }
  16. .item {
  17. font-size: 4em;
  18. text-align: center;
  19. }
  20. .item-1 {
  21. background-color: #ef342a;
  22. }
  23. .item-2 {
  24. background-color: #f68f26;
  25. }
  26. .item-3 {
  27. background-color: #4ba946;
  28. }
  29. .item-4 {
  30. background-color: #0376c2;
  31. }
  32. .item-5 {
  33. background-color: #c077af;
  34. }
  35. .item-6 {
  36. background-color: #f8d29d;
  37. }
  38. .item-7 {
  39. background-color: #b5a87f;
  40. }
  41. .item-8 {
  42. background-color: #d0e4a9;
  43. }
  44. .item-9 {
  45. background-color: #4dc7ec;
  46. }
  47. /* grid实现 */
  48. #container-grid {
  49. border: 0px solid red;
  50. display: grid;
  51. grid-template-columns: 1fr 1fr 1fr;
  52. grid-row-gap: 20px;
  53. grid-column-gap: 20px;
  54. }
  55. /* flex实现 */
  56. #container-flex {
  57. display: flex;
  58. flex-wrap: wrap;
  59. margin-top: -20px;
  60. margin-left: -20px;
  61. }
  62. .item-flex {
  63. font-size: 4em;
  64. text-align: center;
  65. width: calc((100% - 60px) / 3);
  66. margin-top: 20px;
  67. margin-left: 20px;
  68. }
  69. </style>
  70. </head>
  71. <body>
  72. <div id="container-flex">
  73. <div class="item-flex item-1">1</div>
  74. <div class="item-flex item-2">2</div>
  75. <div class="item-flex item-3">3</div>
  76. <div class="item-flex item-4">4</div>
  77. <div class="item-flex item-5">5</div>
  78. <div class="item-flex item-6">6</div>
  79. <div class="item-flex item-7">7</div>
  80. <div class="item-flex item-8">8</div>
  81. <!-- <div class="item-flex item-9">9</div> -->
  82. </div>
  83. <h1 style="color: #fff">flex实现</h1>
  84. <br />
  85. <br />
  86. <hr />
  87. <br />
  88. <br />
  89. <div id="container-grid">
  90. <div class="item item-1">1</div>
  91. <div class="item item-2">2</div>
  92. <div class="item item-3">3</div>
  93. <div class="item item-4">4</div>
  94. <div class="item item-5">5</div>
  95. <div class="item item-6">6</div>
  96. <div class="item item-7">7</div>
  97. <div class="item item-8">8</div>
  98. <!-- <div class="item item-9">9</div> -->
  99. </div>
  100. <h1 style="color: #fff">grid实现</h1>
  101. </body>
  102. </html>

参考

css计算属性
https://developer.mozilla.org/zh-CN/docs/Web/CSS/calc())