博客列表 >12.28用Grid网格布局重写网页组件

12.28用Grid网格布局重写网页组件

曾
原创
2019年12月28日 21:11:56563浏览

今天学习了Grid网格布局,以下是HTML代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>grid布局</title>
  6. <link rel="stylesheet" href="public-second-hand.css">
  7. </head>
  8. <body>
  9. <div class="public-second-hand">
  10. <div class="item1">
  11. <a href="">抢好货</a>
  12. <div>
  13. <span>0低价</span>
  14. <span>安全</span>
  15. <span>便捷</span>
  16. <span>快速</span>
  17. </div>
  18. </div>
  19. <div class="item2">
  20. <span>热门分类</span>
  21. <div>
  22. <a href="">美女写真</a>
  23. <a href="">日本美女</a>
  24. <a href="">美国美女</a>
  25. <a href="">国内美女</a>
  26. <a href="">AV美女</a>
  27. </div>
  28. </div>
  29. <div class="item3">
  30. <div class="item3-pic">
  31. <img src="../static/images/shop/shop8.jpg" alt="">
  32. <span>美女性感写真海报墙艺术装饰画帖画图1</span>
  33. <span>&yen;333</span>
  34. <span>美女</span>
  35. </div>
  36. <div class="item3-pic">
  37. <img src="../static/images/shop/shop8.jpg" alt="">
  38. <span>美女性感写真海报墙艺术装饰画帖画图1</span>
  39. <span>&yen;333</span>
  40. <span>美女</span>
  41. </div>
  42. <div class="item3-pic">
  43. <img src="../static/images/shop/shop8.jpg" alt="">
  44. <span>美女性感写真海报墙艺术装饰画帖画图1</span>
  45. <span>&yen;333</span>
  46. <span>美女</span>
  47. </div>
  48. <div class="item3-pic">
  49. <img src="../static/images/shop/shop8.jpg" alt="">
  50. <span>美女性感写真海报墙艺术装饰画帖画图1</span>
  51. <span>&yen;333</span>
  52. <span>美女</span>
  53. </div>
  54. <div class="item3-pic">
  55. <img src="../static/images/shop/shop8.jpg" alt="">
  56. <span>美女性感写真海报墙艺术装饰画帖画图1</span>
  57. <span>&yen;333</span>
  58. <span>美女</span>
  59. </div>
  60. <div class="item3-pic">
  61. <img src="../static/images/shop/shop8.jpg" alt="">
  62. <span>美女性感写真海报墙艺术装饰画帖画图1</span>
  63. <span>&yen;333</span>
  64. <span>美女</span>
  65. </div>
  66. <div class="item3-pic">
  67. <img src="../static/images/shop/shop8.jpg" alt="">
  68. <span>美女性感写真海报墙艺术装饰画帖画图1</span>
  69. <span>&yen;333</span>
  70. <span>美女</span>
  71. </div>
  72. <div class="item3-pic">
  73. <img src="../static/images/shop/shop8.jpg" alt="">
  74. <span>美女性感写真海报墙艺术装饰画帖画图1</span>
  75. <span>&yen;333</span>
  76. <span>美女</span>
  77. </div>
  78. </div>
  79. <div class="item4">
  80. <div class="item4-quick">
  81. <a href=""><img src="../static/images/ad/1.png" alt=""></a>
  82. </div>
  83. <div class="item4-quick">
  84. <a href=""><img src="../static/images/ad/1.png" alt=""></a>
  85. </div>
  86. <div class="item4-quick">
  87. <a href=""><img src="../static/images/ad/1.png" alt=""></a>
  88. </div>
  89. <div class="item4-quick">
  90. <a href=""><img src="../static/images/ad/1.png" alt=""></a>
  91. </div>
  92. <div class="item4-ad1"><img src="../static/images/ad/image.png" alt=""></div>
  93. <div class="item4-ad2"><img src="../static/images/ad/ad2.jpg" alt=""></div>
  94. </div>
  95. </div>
  96. </body>
  97. </html>

CSS代码

  1. @import "../public-reset.css";
  2. /*设置网格大小*/
  3. .public-second-hand{
  4. width: 1200px;
  5. height: 580px;
  6. display: grid;
  7. /*grid-gap: 10px 10px;*/
  8. box-sizing: border-box;
  9. background-color: white;
  10. margin: 30px auto;
  11. /*画网格列方向4列*/
  12. grid-template-columns: 1fr 1fr 1fr;
  13. /*画网格行方向3行*/
  14. grid-template-rows: 60px 60px 1fr;
  15. outline: 1px dashed red;
  16. border-radius: 10px;
  17. }
  18. /*设置第一个项目第一行排列合并*/
  19. .public-second-hand>.item1{
  20. /*background-color: red;*/
  21. grid-column: 1 / 4;
  22. grid-row: 1 ;
  23. display: flex;
  24. align-items: end;
  25. border-bottom: 1px solid lightgray;
  26. margin: 0 10px;
  27. padding: 10px 0;
  28. box-sizing: border-box;
  29. }
  30. /*设置第一个标题A标签大小*/
  31. .public-second-hand>.item1>a{
  32. font-size: 24px;
  33. }
  34. /*设置A标签旁边项目的距离*/
  35. .public-second-hand>.item1>div{
  36. margin-left: 10px;
  37. }
  38. /*设置SPAN颜色*/
  39. .public-second-hand>.item1>div>span{
  40. color: red;
  41. }
  42. /*设置第二个项目第二行排列合并*/
  43. .public-second-hand>.item2{
  44. /*background-color: yellowgreen;*/
  45. grid-column: 1 / 4;
  46. grid-row: 2 / 3;
  47. display: flex;
  48. align-items: end;
  49. margin: 0 10px;
  50. padding-bottom: 20px;
  51. box-sizing: border-box;
  52. }
  53. .public-second-hand>.item2>span{
  54. font-size: 24px;
  55. color: red;
  56. }
  57. .public-second-hand>.item2>div>a{
  58. margin: 0 10px;
  59. }
  60. /*设置第三个项目第三行和第一到第二列合并*/
  61. .public-second-hand>.item3{
  62. width: 800px;
  63. height: 460px;
  64. /*background-color: lightsalmon;*/
  65. grid-column: 1 / 3;
  66. grid-row: 3 / 4 ;
  67. display: grid;
  68. /*画网格列方向4个相同大小*/
  69. grid-template-columns: repeat(4,190px);
  70. /*画网格行方向2个相同大小*/
  71. grid-template-rows: repeat(2,1fr);
  72. padding-left: 10px;
  73. box-sizing: border-box;
  74. grid-gap: 20px 0;
  75. }
  76. .public-second-hand>.item3 img{
  77. width: 180px;
  78. height: 124px;
  79. border: 1px solid lightgray;
  80. border-radius: 5px;
  81. }
  82. .public-second-hand>.item3>.item3-pic{
  83. display: grid;
  84. justify-items: center;
  85. /*列方向2个相同大小*/
  86. grid-template-columns: 1fr 1fr;
  87. /*行方向3行*/
  88. grid-template-rows: 130px 1fr 1fr;
  89. }
  90. .public-second-hand>.item3>.item3-pic > img{
  91. /*合并网格从列1号线开始,相邻两条列线,行方向第一行*/
  92. grid-column: 1 / span 2;
  93. grid-row: 1 / 2;
  94. }
  95. .public-second-hand>.item3>.item3-pic >span:first-of-type{
  96. /*合并网格从列1号线开始,相邻两条列线,行第2条线开始,第三条线结束,相交区域合并*/
  97. grid-column: 1 / span 2;
  98. grid-row: 2 / 3;
  99. align-self: center;
  100. }
  101. .public-second-hand>.item3>.item3-pic >span:nth-of-type(2){
  102. /*单独设置项目在列方向的对齐方式*/
  103. align-self: center;
  104. /*设置项目在行方向的对齐方式*/
  105. justify-self: flex-start;
  106. }
  107. .public-second-hand>.item3>.item3-pic >span:last-of-type{
  108. /*单独设置项目在列方向的对齐方式*/
  109. align-self: center;
  110. /*设置项目在行方向的对齐方式*/
  111. justify-self: end;
  112. background-color: lightgreen;
  113. padding: 3px;
  114. border-radius: 5px;
  115. color: white;
  116. }
  117. .public-second-hand>.item4{
  118. /*转为网格布局*/
  119. display: grid;
  120. /*两列*/
  121. grid-template-columns:1fr 1fr;
  122. /*四行*/
  123. grid-template-rows:130px 130px 1fr 1fr;
  124. padding-right:5px;
  125. grid-gap: 10px 0;
  126. box-sizing: border-box;
  127. }
  128. .public-second-hand>.item4>.item4-quick{
  129. padding-right:10px;
  130. box-sizing: border-box;
  131. }
  132. .public-second-hand>.item4>.item4-ad1{
  133. grid-column: 1 / 3;
  134. grid-row: 3 / 4;
  135. align-self: end;
  136. margin-top: 10px;
  137. }
  138. .public-second-hand>.item4>.item4-ad2{
  139. grid-column: 1 / 3;
  140. grid-row: 4 / 5;
  141. align-self: center;
  142. }

效果图

第一次使用Grid网格不就,第一个感觉就是别扭,需要去计算每一行,每一列要去给多少像素,最开始整体布局用Grid布置出来了,在两个标题还是嵌套了Flex去写,后来想了想今天学的Grid布局,最好就用网格布局来写,在下面主体内容都是用网格布局,慢慢写出来的,发现在项目对齐上有些属性和Flex有共同处。
最近接受的知识有点多,需要慢慢的去消化一下才行,没事还得复习一下前几天的内容,不然久了容易忘记。
比如FORM表单的内容,现在感觉都已经很模糊了= =,今天把作业提交了,明天就开始复习一下,不然就是猴子掰苞谷,一直拿一直丢。

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议