欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Grid 布局(网格布局)实战项目

程序员文章站 2022-03-27 08:33:04
...

Grid实战项目

代码运行效果展示地址

网格布局相册
模拟12列网格布局
公共头部
公共尾部

1、网格布局相册

代码:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>我的相册</title>
  6. <style>
  7. /* 初始化 */
  8. * {
  9. margin: 0;
  10. padding: 0;
  11. box-sizing: border-box;
  12. }
  13. a {
  14. text-decoration: none;
  15. color: #555;
  16. }
  17. a:hover {
  18. color: coral;
  19. }
  20. body {
  21. background-color: burlywood;
  22. }
  23. h1 {
  24. color: white;
  25. text-align: center;
  26. letter-spacing: 5px;
  27. font-size: 2.5rem;
  28. font-weight: 400;
  29. text-shadow: 2px 2px 2px #555;
  30. margin-top: 20px;
  31. overflow: hidden;
  32. }
  33. .container {
  34. display: grid;
  35. /*画网格-自动填充*/
  36. grid-template-columns: repeat(auto-fill, 250px);
  37. grid-template-rows: repeat(auto-fit, 280px);
  38. /*设置项目在容器上水平垂直都平均对齐*/
  39. place-content: space-evenly space-evenly;
  40. row-gap: 10px;
  41. }
  42. .container > .item {
  43. padding: 10px;
  44. background-color: #eee;
  45. border-radius: 10px;
  46. display: flex;
  47. flex-flow: column nowrap;
  48. align-items: center;
  49. justify-content: center;
  50. }
  51. .item img {
  52. width: 100%;
  53. height: 100%;
  54. }
  55. .container > .item:hover {
  56. box-shadow: 0 0 10px #555;
  57. width: calc(100% * 1.02);
  58. background-color: lemonchiffon;
  59. }
  60. </style>
  61. </head>
  62. <body>
  63. <h1>我的相册</h1>
  64. <div class="container">
  65. <div class="item">
  66. <a href=""><img src="images/pic1.jpg" alt="" /></a>
  67. <a href="">图片1</a>
  68. </div>
  69. <div class="item">
  70. <a href=""><img src="images/pic2.jpg" alt="" /></a>
  71. <a href="">图片2</a>
  72. </div>
  73. <div class="item">
  74. <a href=""><img src="images/pic3.jpg" alt="" /></a>
  75. <a href="">图片3</a>
  76. </div>
  77. <div class="item">
  78. <a href=""><img src="images/pic4.jpg" alt="" /></a>
  79. <a href="">图片4</a>
  80. </div>
  81. <div class="item">
  82. <a href=""><img src="images/pic5.jpg" alt="" /></a>
  83. <a href="">图片5</a>
  84. </div>
  85. <div class="item">
  86. <a href=""><img src="images/pic6.jpg" alt="" /></a>
  87. <a href="">图片6</a>
  88. </div>
  89. <div class="item">
  90. <a href=""><img src="images/pic7.jpg" alt="" /></a>
  91. <a href="">图片7</a>
  92. </div>
  93. <div class="item">
  94. <a href=""><img src="images/pic8.jpg" alt="" /></a>
  95. <a href="">图片8</a>
  96. </div>
  97. <div class="item">
  98. <a href=""><img src="images/pic9.jpg" alt="" /></a>
  99. <a href="">图片9</a>
  100. </div>
  101. <div class="item">
  102. <a href=""><img src="images/pic10.jpg" alt="" /></a>
  103. <a href="">图片10</a>
  104. </div>
  105. <div class="item">
  106. <a href=""><img src="images/pic11.jpg" alt="" /></a>
  107. <a href="">图片11</a>
  108. </div>
  109. <div class="item">
  110. <a href=""><img src="images/pic12.jpg" alt="" /></a>
  111. <a href="">图片12</a>
  112. </div>
  113. </body>
  114. </html>

2、12列网格布局

html代码:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>模拟12列网格布局</title>
  6. <style>
  7. /*初始化*/
  8. * {
  9. margin: 0;
  10. padding: 0;
  11. box-sizing: border-box;
  12. }
  13. body {
  14. display: flex;
  15. /*水平居中*/
  16. justify-content: center;
  17. /*视口*/
  18. max-width: 100vw;
  19. min-height: 100vh;
  20. }
  21. .container {
  22. min-width: 1000px;
  23. display: grid;
  24. /*网格布局默认创建一列多行*/
  25. gap: 5px;
  26. }
  27. .container > .row {
  28. /*把 container 下的每一行都划为12等分*/
  29. display: grid;
  30. /*把第一行划分为12份*/
  31. grid-template-columns: repeat(12, 1fr);
  32. min-height: 50px;
  33. gap: 5px;
  34. }
  35. .container > .row > .item {
  36. background-color: dodgerblue;
  37. padding: 10px;
  38. border: 1px solid;
  39. }
  40. /* 常用网格列样式 */
  41. /*将项目填充到单元格*/
  42. .col-1 {
  43. /* 项目从当前默认位置开始放置,所以起始列编号可省略 */
  44. /*grid-column-start: 1;*/
  45. grid-column-end: span 1;
  46. }
  47. .col-2 {
  48. grid-column-end: span 2;
  49. }
  50. .col-3 {
  51. grid-column-end: span 3;
  52. }
  53. .col-4 {
  54. grid-column-end: span 4;
  55. }
  56. .col-5 {
  57. grid-column-end: span 5;
  58. }
  59. .col-6 {
  60. grid-column-end: span 6;
  61. }
  62. .col-7 {
  63. grid-column-end: span 7;
  64. }
  65. .col-8 {
  66. grid-column-end: span 8;
  67. }
  68. .col-9 {
  69. grid-column-end: span 9;
  70. }
  71. .col-10 {
  72. grid-column-end: span 10;
  73. }
  74. .col-11 {
  75. grid-column-end: span 11;
  76. }
  77. .col-12 {
  78. grid-column-end: span 12;
  79. }
  80. </style>
  81. </head>
  82. <body>
  83. <!--创建容器-->
  84. <div class="container">
  85. <!--创建行-->
  86. <div class="row">
  87. <!--划分列,二等分-->
  88. <span class="item col-6">6</span>
  89. <span class="item col-6">6</span>
  90. </div>
  91. <!--三等分-->
  92. <div class="row">
  93. <span class="item col-4">4</span>
  94. <span class="item col-4">4</span>
  95. <span class="item col-4">4</span>
  96. </div>
  97. <div class="row">
  98. <span class="item col-2">2</span>
  99. <span class="item col-8">8</span>
  100. <span class="item col-2">2</span>
  101. </div>
  102. <div class="row">
  103. <span class="item col-2">2</span>
  104. <span class="item col-8">8</span>
  105. </div>
  106. <div class="row">
  107. <span class="item col-1">1</span>
  108. <span class="item col-1">2</span>
  109. <span class="item col-1">3</span>
  110. <span class="item col-1">4</span>
  111. <span class="item col-1">5</span>
  112. <span class="item col-1">6</span>
  113. <span class="item col-1">7</span>
  114. <span class="item col-1">8</span>
  115. <span class="item col-1">9</span>
  116. <span class="item col-1">10</span>
  117. <span class="item col-1">11</span>
  118. <span class="item col-1">12</span>
  119. </div>
  120. </div>
  121. </body>
  122. </html>

3、部份实战项目组件

3.1 公共头部

html代码:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>公共头部</title>
  6. <link rel="stylesheet" href="public_header.css">
  7. <link rel="stylesheet" href="./font/iconfont.css">
  8. </head>
  9. <body>
  10. <nav class=public-header>
  11. <a href="">网站首页</a>
  12. <a href="">专题</a>
  13. <a href="">网站导航</a>
  14. <a href="">二手商品</a>
  15. <a href="">讨论区</a>
  16. <span>
  17. <a href=""><i class="iconfont icon-huiyuan2"></i>登陆</a>
  18. <a href="">免费注册</a>
  19. </span>
  20. </nav>
  21. </body>
  22. </html>

css代码:

  1. @import "public_reset.css";
  2. .public-header {
  3. width: 1200px;
  4. height: 50px;
  5. display: flex;
  6. flex-wrap: nowrap;
  7. background-color: black;
  8. align-items: center;
  9. /*justify-content: space-evenly;*/
  10. }
  11. .public-header a {
  12. color: white;
  13. font-size: 15px;
  14. }
  15. .public-header a:first-of-type {
  16. margin-left: 30px;
  17. }
  18. .public-header > a {
  19. margin: auto 15px;
  20. }
  21. .public-header > span {
  22. display: flex;
  23. margin-left: 560px;
  24. }
  25. .public-header > span a {
  26. margin: auto 10px;
  27. }
  28. .public-header > span .iconfont {
  29. margin: auto 10px;
  30. }

html代码:

3.2 公共尾部

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>公共尾部</title>
  6. <link rel="stylesheet" href="public_footer.css">
  7. </head>
  8. <body>
  9. <footer class="public-header">
  10. <div>
  11. <a href="">简介</a>
  12. <a href="">联系我们</a>
  13. <a href="">招聘信息</a>
  14. <a href="">友情链接</a>
  15. <a href="">用户服务协议</a>
  16. <a href="">隐私权声明</a>
  17. <a href="">法律投诉声明</a>
  18. </div>
  19. <div><span>LOGO</span></div>
  20. <div>
  21. <p>2019 fengniao.com. All rights reserved . 安徽闹着玩有限公司(无聊网)版权所有</p>
  22. <p>皖ICP证150110号 京ICP备14323013号-2 皖公网安备110108024357788号</p>
  23. <p>违法和不良信息举报电话: 0551-1234567 举报邮箱: admin@baidu.com</p>
  24. </div>
  25. <div>
  26. <p>关注公众号</p>
  27. <img src="erwei-code.png" alt="">
  28. </div>
  29. </footer>
  30. </body>
  31. </html>

css代码:

  1. @import "public_reset.css";
  2. footer {
  3. height: 250px;
  4. background-color: black;
  5. color: white;
  6. display: grid;
  7. grid-template-columns: 130px 650px 300px;
  8. grid-template-rows: 30px 180px;
  9. align-content: center;
  10. }
  11. footer div:first-of-type {
  12. grid-column: 1 / span 2 ;
  13. grid-row: 1 / span 1;
  14. margin: auto 10px;
  15. }
  16. footer div:first-of-type > a {
  17. margin: auto 12px;
  18. color: white;
  19. }
  20. footer div:first-of-type > a:first-of-type {
  21. margin-left: 140px;
  22. }
  23. footer div:nth-of-type(2) {
  24. grid-column: 1 / span 1 ;
  25. grid-row: 2 / span 1;
  26. font-size: 35px;
  27. margin: 50px 120px;
  28. }
  29. footer div:nth-of-type(3) {
  30. grid-column: 2 / span 1 ;
  31. grid-row: 2 / span 1;
  32. margin-left: 160px;
  33. }
  34. footer div:nth-of-type(3) > p {
  35. margin: 22px auto;
  36. }
  37. footer div:last-of-type {
  38. grid-column: 3 / span 1 ;
  39. grid-row: 1 / span 2;
  40. margin-left: 30px;
  41. }

4、总结

相比flex,Grid布局知识点很多很细,要慢慢尝试,细细品味。