Blogger Information
Blog 11
fans 0
comment 0
visits 12884
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
grid网格布局-仿PHP中文网板块
写代码的张先森
Original
814 people have browsed it

我们下面用grid布局仿照实现一个PHP中文网的板块布局


注: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-仿PHP中文网板块</title>
  7. <style>
  8. /* 初始化 */
  9. *{
  10. padding: 0;
  11. margin: 0;
  12. box-sizing: border-box;
  13. background-color: #ececec;
  14. }
  15. .container{
  16. width: 1000px;
  17. height: 500px;
  18. margin: 20px auto 0 auto;
  19. /* 转为grid网格布局 */
  20. display: grid;
  21. /* 声明几列 例:5列 每列1等份*/
  22. grid-template-columns: repeat(5,1fr);
  23. /* 声明几行 例:3行 每行1等份*/
  24. grid-template-rows: repeat(3,1fr);
  25. /* 为所有网格项目设置边距 */
  26. gap: 10px;
  27. }
  28. .container > .item:first-of-type{
  29. /* 第一个项目跨2行合并 变为网格区域 */
  30. grid-row: span 2;
  31. }
  32. .container > .item > img{
  33. width: 100%;
  34. height: 100%;
  35. }
  36. </style>
  37. </head>
  38. <body>
  39. <div class="container">
  40. <div class="item item1"><img src="static/images/1.jpg" alt=""></div>
  41. <div class="item item2"><img src="static/images/2.png" alt=""></div>
  42. <div class="item item3"><img src="static/images/3.png" alt=""></div>
  43. <div class="item item4"><img src="static/images/4.png" alt=""></div>
  44. <div class="item item5"><img src="static/images/5.png" alt=""></div>
  45. <div class="item item6"><img src="static/images/6.png" alt=""></div>
  46. <div class="item item7"><img src="static/images/7.png" alt=""></div>
  47. <div class="item item8"><img src="static/images/8.png" alt=""></div>
  48. <div class="item item9"><img src="static/images/9.png" alt=""></div>
  49. <div class="item item10"><img src="static/images/10.png" alt=""></div>
  50. <div class="item item11"><img src="static/images/11.png" alt=""></div>
  51. <div class="item item12 "><img src="static/images/12.png" alt=""></div>
  52. <div class="item item1"><img src="static/images/13.png" alt=""></div>
  53. <div class="item item2"><img src="static/images/14.png" alt=""></div>
  54. </div>
  55. </body>
  56. </html>

效果图:

看似复杂的一个布局,用简单的几行grid布局代码就这么完成啦 !
里面的内容并没有做过多的美化,撸这个小实例的目的就是为了清晰明了的看清楚grid布局的简单与效率,传统布局可能需要很多行代码去实现这个一个布局,而grid就这个几行代码就实现了!

小结:

1.建议看到的小伙伴可以去PHP中文网下载css文档了解一下grid布局的常用方法
2.根据学到的常用方法去结合小案例动手实践每个方法的用法和作用

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:每个项目的dom结构不对, 下次不要偷懒了
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
Author's latest blog post