博客列表 >使用Grid布局来完成商品列表组件的展示样式--PHP线上培训十期1228

使用Grid布局来完成商品列表组件的展示样式--PHP线上培训十期1228

高的PHP十期培训学习笔记
高的PHP十期培训学习笔记原创
2020年01月03日 11:12:45773浏览

今天我们使用Grid布局的方法重新前面写的商品列表组件案例:

案例:商品列表页

要达到的效果

首先分析出来需要使用Grid布局的地方结构

HTML代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>二手商品列表</title>
  6. <!-- <link rel="stylesheet" href="public_second_hand.css">-->
  7. <link rel="stylesheet" href="public_second_grid.css">
  8. </head>
  9. <body>
  10. <!--标题组件-->
  11. <div class="public-headline">
  12. <span>二手交易</span>
  13. </div>
  14. <!--交易专区-->
  15. <div class="publi-second-hand">
  16. <!-- 抢好货标题-->
  17. <div class="title1">
  18. <a href="">抢好货</a>
  19. <span>0元低价,便捷,安全</span>
  20. </div>
  21. <!-- 分类标题-->
  22. <div class="title2">
  23. <span>热门分类</span>
  24. <!-- 在光标所在行按CTRL+D可以快速复制当前行-->
  25. <a href="">美女写真</a>
  26. <a href="">日本美女</a>
  27. <a href="">美国美女</a>
  28. <a href="">国内美女</a>
  29. </div>
  30. <!-- 商品内容区-->
  31. <div class="goods">
  32. <!-- 商品列表-->
  33. <div class="goods-list">
  34. <!-- 单个商品块-->
  35. <div class="intro">
  36. <a href=""><img src="../../../static/images/shop1.jpg" alt="" width="176" height="120"></a>
  37. <a href="">美女性感写真海报墙艺术装饰画贴画图1</a>
  38. <div>
  39. <span>&yen; 333</span>
  40. <span>美女</span>
  41. </div>
  42. </div>
  43. <!-- 单个商品块-->
  44. <div class="intro">
  45. <a href=""><img src="../../../static/images/shop2.jpg" alt="" width="176" height="120"></a>
  46. <a href="">美女性感写真海报墙艺术装饰画贴画图1</a>
  47. <div>
  48. <span>&yen; 333</span>
  49. <span>美女</span>
  50. </div>
  51. </div>
  52. <!-- 单个商品块-->
  53. <div class="intro">
  54. <a href=""><img src="../../../static/images/shop3.jpg" alt="" width="176" height="120"></a>
  55. <a href="">美女性感写真海报墙艺术装饰画贴画图1</a>
  56. <div>
  57. <span>&yen; 333</span>
  58. <span>美女</span>
  59. </div>
  60. </div>
  61. <!-- 单个商品块-->
  62. <div class="intro">
  63. <a href=""><img src="../../../static/images/shop4.jpg" alt="" width="176" height="120"></a>
  64. <a href="">美女性感写真海报墙艺术装饰画贴画图1</a>
  65. <div>
  66. <span>&yen; 333</span>
  67. <span>美女</span>
  68. </div>
  69. </div>
  70. <!-- 单个商品块-->
  71. <div class="intro">
  72. <a href=""><img src="../../../static/images/shop5.jpg" alt="" width="176" height="120"></a>
  73. <a href="">美女性感写真海报墙艺术装饰画贴画图1</a>
  74. <div>
  75. <span>&yen; 333</span>
  76. <span>美女</span>
  77. </div>
  78. </div>
  79. <!-- 单个商品块-->
  80. <div class="intro">
  81. <a href=""><img src="../../../static/images/shop6.jpg" alt="" width="176" height="120"></a>
  82. <a href="">美女性感写真海报墙艺术装饰画贴画图1</a>
  83. <div>
  84. <span>&yen; 333</span>
  85. <span>美女</span>
  86. </div>
  87. </div>
  88. <!-- 单个商品块-->
  89. <div class="intro">
  90. <a href=""><img src="../../../static/images/shop7.jpg" alt="" width="176" height="120"></a>
  91. <a href="">美女性感写真海报墙艺术装饰画贴画图1</a>
  92. <div>
  93. <span>&yen; 333</span>
  94. <span>美女</span>
  95. </div>
  96. </div>
  97. <!-- 单个商品块-->
  98. <div class="intro">
  99. <a href=""><img src="../../../static/images/shop8.jpg" alt="" width="176" height="120"></a>
  100. <a href="">美女性感写真海报墙艺术装饰画贴画图1</a>
  101. <div>
  102. <span>&yen; 333</span>
  103. <span>美女</span>
  104. </div>
  105. </div>
  106. </div>
  107. <!-- 右侧图片功能区-->
  108. <div class="quick-entry">
  109. <div>
  110. <a href=""><img src="../../../static/images/1.png" alt=""></a>
  111. <a href=""><img src="../../../static/images/2.png" alt=""></a>
  112. <a href=""><img src="../../../static/images/3.png" alt=""></a>
  113. <a href=""><img src="../../../static/images/4.png" alt=""></a>
  114. </div>
  115. <div>
  116. <a href=""><img src="../../../static/images/ad1.png" alt=""></a>
  117. <a href=""><img src="../../../static/images/ad2.jpg" alt=""></a>
  118. </div>
  119. </div>
  120. </div>
  121. </div>
  122. </body>
  123. </html>

运行后的效果

添加公共样式表

首先分成一行两列(将商品列表放在左边)

  1. /*商品内容区*/
  2. .publi-second-hand > .goods {
  3. /*转为GRID*/
  4. display: grid;
  5. /*画列*/
  6. grid-template-columns: 790px 390px;
  7. /*画行*/
  8. grid-template-rows: 440px ;
  9. }

将左侧商品使用Grid分成两行四列显示

  1. .publi-second-hand > .goods > .goods-list {
  2. /*转GRID*/
  3. display: grid;
  4. /*画四列两行*/
  5. /*repeat(4,1fr)属性为重复几次后面的参数*/
  6. grid-template-columns:repeat(4,1fr);
  7. grid-template-rows: repeat(2,1fr);
  8. }

使用Grid布局做右侧功能区

  1. /*右侧图片功能区*/
  2. .publi-second-hand > .goods > .quick-entry {
  3. display: grid;
  4. /*画三行一列,间距10px*/
  5. grid-template-columns: 1fr;
  6. grid-template-rows: repeat(2,1fr);
  7. grid-row-gap: 10px;
  8. }

最后调整细节样式

  1. /*引入初始化样式表*/
  2. @import "../public_reset.css";
  3. /*引入标题样式*/
  4. @import "../../public/public_headline/public-headline.css";
  5. /*交易专区的样式表*/
  6. .publi-second-hand {
  7. width: 1200px;
  8. padding: 10px;
  9. /*防止盒子撑大*/
  10. box-sizing: border-box;
  11. margin: auto;
  12. /*加圆角*/
  13. border-radius: 5px;
  14. background-color: #fff;
  15. }
  16. /*鼠标经过加阴影*/
  17. .publi-second-hand:hover {
  18. box-shadow: 0 0 8px #888;
  19. }
  20. /*抢好货标题*/
  21. .publi-second-hand > .title1 {
  22. height: 50px;
  23. /*一个像素的的底部实线*/
  24. border-bottom: 1px solid #cccccc;
  25. box-sizing: border-box;
  26. }
  27. .publi-second-hand > .title1 > a {
  28. font-size: 23px;
  29. margin-right: 20px;
  30. }
  31. .publi-second-hand > .title1 > span {
  32. color: red;
  33. }
  34. /*分类标题*/
  35. .publi-second-hand > .title2 {
  36. height: 55px;
  37. }
  38. .publi-second-hand > .title2 > span {
  39. color: red;
  40. font-size: 23px;
  41. }
  42. .publi-second-hand > .title2 > span ~ a {
  43. padding-left: 20px;
  44. }
  45. /*鼠标经过分类标题时的颜色变化*/
  46. .publi-second-hand > .title2 > span ~ a:hover {
  47. color: lightcoral;
  48. }
  49. /*商品内容区*/
  50. .publi-second-hand > .goods {
  51. /*转为GRID*/
  52. display: grid;
  53. /*画列*/
  54. grid-template-columns: 790px 390px;
  55. /*画行*/
  56. grid-template-rows: 440px ;
  57. }
  58. .publi-second-hand > .goods > .goods-list {
  59. /*转GRID*/
  60. display: grid;
  61. /*画四列两行*/
  62. /*repeat(4,1fr)属性为重复几次后面的参数*/
  63. grid-template-columns:repeat(4,1fr);
  64. grid-template-rows: repeat(2,1fr);
  65. }
  66. .publi-second-hand > .goods > .goods-list > .intro{
  67. width: 176px;
  68. height: 200px;
  69. padding: 5px;
  70. box-sizing: border-box;
  71. display: grid;
  72. /*画一列三行*/
  73. grid-template-columns:1fr;
  74. grid-template-rows: repeat(3,1fr);
  75. /*设置行间距*/
  76. grid-row-gap: 5px;
  77. }
  78. .publi-second-hand > .goods > .goods-list > .intro img {
  79. border: 1px solid #cccccc;
  80. border-radius: 5px;
  81. }
  82. .publi-second-hand > .goods > .goods-list > .intro > div {
  83. display: grid;
  84. grid-template-columns: repeat(2,1fr);
  85. grid-template-rows: 1fr;
  86. }
  87. .publi-second-hand > .goods > .goods-list > .intro span:first-of-type {
  88. color: red;
  89. }
  90. .publi-second-hand > .goods > .goods-list > .intro > div {
  91. display: flex;
  92. }
  93. .publi-second-hand > .goods > .goods-list > .intro span:last-of-type {
  94. color: white;
  95. background-color: #55ce9f;
  96. padding: 0 5px;
  97. margin-left: auto;
  98. }
  99. /*右侧图片功能区*/
  100. .publi-second-hand > .goods > .quick-entry {
  101. display: grid;
  102. /*画三行一列,间距10px*/
  103. grid-template-columns: 1fr;
  104. grid-template-rows: repeat(2,1fr);
  105. grid-row-gap: 10px;
  106. }
  107. .publi-second-hand > .goods > .quick-entry > div:first-of-type {
  108. display: grid;
  109. /*画两行两列*/
  110. grid-template-columns:repeat(2,1fr);
  111. grid-template-rows: repeat(2,1fr);
  112. grid-gap: 5px;
  113. }
  114. .publi-second-hand > .goods > .quick-entry > div:first-of-type img {
  115. width: 190px;
  116. }
  117. .publi-second-hand > .goods > .quick-entry > div:last-of-type img {
  118. width: 390px;
  119. height: 60px;
  120. margin-top: 10px;
  121. }

Grid网格布局的知识

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