博客列表 >PHP大牛成长之路:Grid 布局实现PHP中文网首页课程列表

PHP大牛成长之路:Grid 布局实现PHP中文网首页课程列表

周Sir-BLOG
周Sir-BLOG原创
2020年06月29日 19:01:43746浏览

Grid 布局实现PHP中文网首页课程列表

实战项目图如下:

使用Grid布局效果:

HTML&DOM结构:

  1. <!DOCTYPE html>
  2. <html lang="zh-cn">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>Grid布局实战</title>
  7. <link rel="stylesheet" href="./static/css/style.css" />
  8. </head>
  9. <body>
  10. <div class="container">
  11. <div class="item">php入门精品课程</div>
  12. <div class="item"><img src="./static/img/ad01.jpg" alt="" /></div>
  13. <div class="item">
  14. <img src="./static/img/1.png" alt="" />
  15. <a href=""><i>初级</i>ThinkPHP6.0极速入门(视频教程)</a>
  16. <span>4W+次播放</span>
  17. </div>
  18. <div class="item">
  19. <img src="./static/img/2.jpg" alt="" />
  20. <a href=""><i>初级</i>ThinkPHP6.0极速入门(视频教程)</a>
  21. <span>4W+次播放</span>
  22. </div>
  23. <div class="item">
  24. <img src="./static/img/3.jpg" alt="" />
  25. <a href=""><i>初级</i>ThinkPHP6.0极速入门(视频教程)</a>
  26. <span>4W+次播放</span>
  27. </div>
  28. <div class="item">
  29. <img src="./static/img/4.jpg" alt="" />
  30. <a href=""><i>初级</i>ThinkPHP6.0极速入门(视频教程)</a>
  31. <span>4W+次播放</span>
  32. </div>
  33. <div class="item">
  34. <img src="./static/img/5.jpg" alt="" />
  35. <a href=""><i>初级</i>ThinkPHP6.0极速入门(视频教程)</a>
  36. <span>4W+次播放</span>
  37. </div>
  38. <div class="item">
  39. <img src="./static/img/6.jpeg" alt="" />
  40. <a href=""><i>初级</i>ThinkPHP6.0极速入门(视频教程)</a>
  41. <span>4W+次播放</span>
  42. </div>
  43. <div class="item">
  44. <img src="./static/img/7.jpg" alt="" />
  45. <a href=""><i>初级</i>ThinkPHP6.0极速入门(视频教程)</a>
  46. <span>4W+次播放</span>
  47. </div>
  48. <div class="item">
  49. <img src="./static/img/8.jpg" alt="" />
  50. <a href=""><i>初级</i>ThinkPHP6.0极速入门(视频教程)</a>
  51. <span>4W+次播放</span>
  52. </div>
  53. <div class="item">
  54. <img src="./static/img/9.jpg" alt="" />
  55. <a href=""><i>初级</i>ThinkPHP6.0极速入门(视频教程)</a>
  56. <span>4W+次播放</span>
  57. </div>
  58. <div class="item">
  59. <img src="./static/img/10.jpg" alt="" />
  60. <a href=""><i>初级</i>ThinkPHP6.0极速入门(视频教程)</a>
  61. <span>4W+次播放</span>
  62. </div>
  63. <div class="item">
  64. <img src="./static/img/11.jpg" alt="" />
  65. <a href=""><i>初级</i>ThinkPHP6.0极速入门(视频教程)</a>
  66. <span>4W+次播放</span>
  67. </div>
  68. <div class="item">
  69. <img src="./static/img/12.jpg" alt="" />
  70. <a href=""><i>初级</i>ThinkPHP6.0极速入门(视频教程)</a>
  71. <span>4W+次播放</span>
  72. </div>
  73. <div class="item">
  74. <img src="./static/img/13.jpg" alt="" />
  75. <a href=""><i>初级</i>ThinkPHP6.0极速入门(视频教程)</a>
  76. <span>4W+次播放</span>
  77. </div>
  78. </div>
  79. </body>
  80. </html>

CSS样式:

  1. * {
  2. margin: 0;
  3. padding: 0;
  4. box-sizing: border-box;
  5. }
  6. html,
  7. body {
  8. width: 100%;
  9. height: 100%;
  10. overflow: hidden;
  11. overflow-y: auto;
  12. font: 14px Helvetica Neue, Helvetica, PingFang SC, 微软雅黑, Tahoma, Arial,
  13. sans-serif;
  14. background: #f3f5f7;
  15. }
  16. li,
  17. dd {
  18. list-style: none;
  19. }
  20. a {
  21. text-decoration: none;
  22. }
  23. button,
  24. img {
  25. border: none;
  26. outline: none;
  27. }
  28. .container {
  29. width: 1200px;
  30. height: 650px;
  31. margin: 10px auto;
  32. background: #fff;
  33. display: grid;
  34. grid-template-columns: repeat(5, 20%);
  35. grid-template-rows: 80px 190px 190px 190px;
  36. }
  37. .container i{
  38. font-style: normal;
  39. font-size: 12px;
  40. color: #fff;
  41. border-radius: 1px;
  42. padding: 2px;
  43. background-color: #93999f;
  44. margin-right: 2px;
  45. }
  46. .container .item:first-of-type {
  47. grid-column: span 5;
  48. line-height: 80px;
  49. text-align: center;
  50. font-size: 20px;
  51. font-weight: 600;
  52. color: #4d555d;
  53. }
  54. .container .item:nth-child(2) {
  55. grid-row: span 2;
  56. /* background: lightseagreen; */
  57. margin: 0 auto;
  58. }
  59. .container .item:nth-child(2) img {
  60. border-radius: 8px;
  61. }
  62. .container .item:nth-child(n + 3) {
  63. margin: 0 auto;
  64. position: relative;
  65. }
  66. .container .item:nth-child(n + 3) img {
  67. width: 217px;
  68. height: 124px;
  69. border-radius: 8px;
  70. border: none;
  71. }
  72. .container .item:nth-child(n + 3) a {
  73. display: block;
  74. border-radius: 8px;
  75. padding: 15px 20px;
  76. position: absolute;
  77. top: 92px;
  78. transition: top 0.3s;
  79. height: 80px;
  80. width: 217px;
  81. background: #fff;
  82. color: #555;
  83. }
  84. .container .item:nth-child(n + 3) a:hover {
  85. color: #333;
  86. top: 55px;
  87. }
  88. .container .item:nth-child(n + 3) span {
  89. position: absolute;
  90. top: 130px;
  91. left: 0;
  92. width: 217px;
  93. height: 42px;
  94. padding: 0 20px;
  95. line-height: 42px;
  96. background: #fff;
  97. color: #93999f;
  98. font-size: 12px;
  99. border-bottom-left-radius: 8px;
  100. border-bottom-right-radius: 8px;
  101. box-shadow: 0 4px 8px 0 rgba(7, 17, 27, 0.1);
  102. }

总结:

1、使用Grid布局轻松实现以上效果,对比基础布局及FlexBox盒子布局好太多了;
2、Grid布局主要属性如下:

  1. /*1、父容器中定义Grid布局 */
  2. display: grid;
  3. /*-----------------------------------*/
  4. /*2、父容器中设定行/列*/
  5. /*设定行*/
  6. grid-template-rows: 30px 1fr 30px;
  7. /*设定列*/
  8. grid-template-columns: 30px 1fr 30px;
  9. /*以上为3行3列布局,fr为剩余空间分为几份(相当与百分比),同样支持%为单位。*/
  10. /*如多个行或列均为相同尺寸,可以使用repeat参数,比如: grid-template-columns: repeat(5, 25px);*/
  11. /*-----------------------------------*/
  12. /*3、网格合并*/
  13. /*方法1、指定开始和结束的网格线*/
  14. grid-column-start: 1;
  15. grid-column-end: 2;
  16. /*方法2、结束属性指定跨越几行或几列*/
  17. grid-column-start:1;
  18. grid-column-end: span 2;
  19. /*方法3、简化( grid-column: 起始列号 / 结束列号)*/
  20. grid-column: 1 / 2;
  21. /*结束参数支持指定跨越几行或几列*/
  22. /*-----------------------------------*/
  1. /*Grid自定义名称模板布局*/
  2. /*父容器中定义*/
  3. grid-template-areas:
  4. "head head head"
  5. "left main right"
  6. "footer footer footer";
  7. /*项目中需命名*/
  8. grid-area: 网格名称;
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议
老周2020-06-29 19:05:491楼
示例地址:http://106.13.227.76/grid/