QQ登录

只需要一步,快速开始

APP扫码登录

只需要一步,快速开始

手机号码,快捷登录

手机号码,快捷登录

查看: 2122|回复: 0

[HTML/CSS/JS] 用CSS Grid实现响应式图片布局的效果

[复制链接]

等级头衔

积分成就    金币 : 2841
   泡泡 : 1516
   精华 : 6
   在线时间 : 1294 小时
   最后登录 : 2024-11-21

丰功伟绩

优秀达人突出贡献荣誉管理论坛元老

联系方式
发表于 2021-7-5 09:23:41 | 显示全部楼层 |阅读模式
一、实例效果5 o3 {# G, `! N" G1 Y
PC端预览的效果:+ W( e% Z) V* O8 _. W0 D( |: U
1.jpg % d$ A3 M9 n* j& z9 ?: Z. {- Y/ v; J
移动端的效果:
$ T, A& ^/ N; b: e, m2 Q' S$ |$ T 2.jpg
7 P8 W4 I2 j5 r* g9 R; t二、要点解析
0 m2 z& M6 m8 p9 O/ Y
  • 采用display: grid创建一个合适的响应网格布局。
  • 将样式包装到媒体查询中,以适应于小尺寸屏幕。
    4 B! Y2 l4 K. X: L) [
三、案例代码$ F& K( j1 d& N' @
HTML代码:
3 @; ]$ T  d: ?3 w; ~0 ~6 v/ G( ^
  1. <div class="image-mosaic">
  2.   <div class="card card-tall card-wide" style="background-image: url('http://www.webqdkf.com/skin/default/images/gksj-09.jpg')">
  3.   </div>
  4.   <div class="card card-tall" style="background-image: url('http://www.webqdkf.com/d/file/p/2021/02-11/e1edfa3a0786f4df7c442a1895522109.jpg')">
  5.   </div>
  6.   <div class="card" style="background-image: url('http://www.webqdkf.com/d/file/p/2021/02-11/e2ab18d0d9ef27d2e2f6f589c501e25b.jpg')">
  7.   </div>
  8.   <div class="card" style="background-image: url('http://www.webqdkf.com/d/file/p/2020/11-12/1755875dfb05ec8dc263be2f56d124e5.jpg')">
  9.   </div>
  10.   <div class="card" style="background-image: url('http://www.webqdkf.com/d/file/p/2021/02-11/859ecf02e0f70819ba71d5fee8f72f12.jpg')">
  11.   </div>
  12.   <div class="card" style="background-image: url('http://www.webqdkf.com/d/file/p/2021/02-11/21903f3245940092303fd5a519f813a2.jpg')">
  13.   </div>
  14.   <div class="card card-wide" style="background-image: url('http://www.webqdkf.com/skin/default/images/banner03-873-390.jpg')">
  15.   </div>
  16.   <div class="card" style="background-image: url('http://www.webqdkf.com/d/file/p/2019/09-04/0d3c66ece10207b0d1dca9118560e16d.jpg')">
  17.   </div>
  18.   <div class="card" style="background-image: url('http://www.webqdkf.com/d/file/p/2019/09-04/63fb35a52cd8f01b68d78aa0b6c5b021.jpg')">
  19.   </div>
  20.    <div class="card" style="background-image: url('http://www.webqdkf.com/d/file/p/2019/09-04/63fb35a52cd8f01b68d78aa0b6c5b021.jpg')">
  21.    </div>
  22.   <div class="card"  style="background-image: url('http://www.webqdkf.com/d/file/p/2020/11-12/e973c393fd7fda278977fb2ff686fdc9.jpg')">
  23.   </div>
  24.   <div  class="card" style="background-image: url('http://www.webqdkf.com/d/file/p/2020/11-12/52d11e4aef71cdb14d95cded9ffc3bf8.jpg')">
  25.   </div>
  26.   <div  class="card" style="background-image: url('http://www.webqdkf.com/d/file/p/2020/08-31/a03f0b9879a5361ba0a5519cbe1dbac1.jpg')">
  27.   </div>
  28.   <div class="card"  style="background-image: url('http://www.webqdkf.com/d/file/p/2020/07-16/5ac65c5c5d7ead056d8ac2767b5648e1.jpg')">
  29.   </div>
  30.   <div  class="card" style="background-image: url('http://www.webqdkf.com/d/file/p/2020/07-09/b45e91e2b7c53f658f85f9d76f89aa77.jpg')">
  31.   </div>
  32.   <div  class="card" style="background-image: url('http://www.webqdkf.com/d/file/p/2020/07-08/186d506c004efa7896fa634e5139b6dc.jpg')">
  33.   </div>
  34.   <div  class="card" style="background-image: url('http://www.webqdkf.com/d/file/p/2020/07-09/f731e58e9bf7ee4c5b5b2a85e5dd2cbf.jpg')">
  35.   </div>
  36.    <div class="card"  style="background-image: url('http://www.webqdkf.com/d/file/p/2020/09-11/c0f179887b1f9efa98c4af944e8d699b.jpg')">
  37.   </div>
  38.   <div  class="card" style="background-image: url('http://www.webqdkf.com/d/file/p/2019/09-04/052746e2767ffb56e045a5e7017ccec9.jpg')">
  39.   </div>
  40.   <div  class="card" style="background-image: url('http://www.webqdkf.com/d/file/p/2019/09-05/ede4d9983eefd8f88bf03eb966bceb5c.jpg')">
  41.   </div>
  42.   <div class="card"  style="background-image: url('http://www.webqdkf.com/d/file/p/2019/09-05/2383cd70ff6d2f15350d64b0af572821.jpg')">
  43.   </div>
  44.   <div  class="card" style="background-image: url('http://www.webqdkf.com/d/file/p/2019/09-24/1442b8adc1f03141eb66e844e069caa8.jpg')">
  45.   </div>
  46.   <div  class="card" style="background-image: url('http://www.webqdkf.com/d/file/p/2019/09-04/0d3c66ece10207b0d1dca9118560e16d.jpg')">
  47.   </div>
  48.   <div  class="card" style="background-image: url('http://www.webqdkf.com/d/file/p/2019/09-04/052746e2767ffb56e045a5e7017ccec9.jpg')">
  49.   </div>
  50. </div>
CSS代码:! l* s: g' f/ k% A4 m. s) ^
  1. .image-mosaic {
  2.   display: grid;
  3.   gap: 1rem;
  4.   grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  5.   grid-auto-rows: 240px;
  6. }
  7.     .card {
  8.   display: flex;
  9.   flex-direction: column;
  10.   justify-content: center;
  11.   align-items: center;
  12.   background: #353535;
  13.   font-size: 3rem;
  14.   color: #fff;
  15.   box-shadow: rgba(3, 8, 20, 0.1) 0px 0.15rem 0.5rem, rgba(2, 8, 20, 0.1) 0px 0.075rem 0.175rem;
  16.   height: 100%;
  17.   width: 100%;
  18.   border-radius: 4px;
  19.   transition: all 500ms;
  20.   overflow: hidden;
  21.   background-size: cover;
  22.   background-position: center;
  23.   background-repeat: no-repeat;
  24.   padding: 0;
  25.   margin: 0;
  26. }
  27. @media screen and (min-width: 600px) {
  28.   .card-tall {
  29.     grid-row: span 2 / auto;
  30.   }
  31.   .card-wide {
  32.     grid-column: span 2 / auto;
  33.   }
  34. }
完整代码:0 u, e+ s$ {1 C
  1. <html>
  2. <head>
  3.    <style type="text/css">
  4.    .image-mosaic {
  5.   display: grid;
  6.   gap: 1rem;
  7.   grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  8.   grid-auto-rows: 240px;
  9. }
  10.     .card {
  11.   display: flex;
  12.   flex-direction: column;
  13.   justify-content: center;
  14.   align-items: center;
  15.   background: #353535;
  16.   font-size: 3rem;
  17.   color: #fff;
  18.   box-shadow: rgba(3, 8, 20, 0.1) 0px 0.15rem 0.5rem, rgba(2, 8, 20, 0.1) 0px 0.075rem 0.175rem;
  19.   height: 100%;
  20.   width: 100%;
  21.   border-radius: 4px;
  22.   transition: all 500ms;
  23.   overflow: hidden;
  24.   background-size: cover;
  25.   background-position: center;
  26.   background-repeat: no-repeat;
  27.   padding: 0;
  28.   margin: 0;
  29. }
  30. @media screen and (min-width: 600px) {
  31.   .card-tall {
  32.     grid-row: span 2 / auto;
  33.   }
  34.   .card-wide {
  35.     grid-column: span 2 / auto;
  36.   }
  37. }
  38. </style>
  39. </head>
  40. <body>
  41.   <div class="image-mosaic">
  42.   <div class="card card-tall card-wide" style="background-image: url('http://www.webqdkf.com/skin/default/images/gksj-09.jpg')">
  43.   </div>
  44.   <div class="card card-tall" style="background-image: url('http://www.webqdkf.com/d/file/p/2021/02-11/e1edfa3a0786f4df7c442a1895522109.jpg')">
  45.   </div>
  46.   <div class="card" style="background-image: url('http://www.webqdkf.com/d/file/p/2021/02-11/e2ab18d0d9ef27d2e2f6f589c501e25b.jpg')">
  47.   </div>
  48.   <div class="card" style="background-image: url('http://www.webqdkf.com/d/file/p/2020/11-12/1755875dfb05ec8dc263be2f56d124e5.jpg')">
  49.   </div>
  50.   <div class="card" style="background-image: url('http://www.webqdkf.com/d/file/p/2021/02-11/859ecf02e0f70819ba71d5fee8f72f12.jpg')">
  51.   </div>
  52.   <div class="card" style="background-image: url('http://www.webqdkf.com/d/file/p/2021/02-11/21903f3245940092303fd5a519f813a2.jpg')">
  53.   </div>
  54.   <div class="card card-wide" style="background-image: url('http://www.webqdkf.com/skin/default/images/banner03-873-390.jpg')">
  55.   </div>
  56.   <div class="card" style="background-image: url('http://www.webqdkf.com/d/file/p/2019/09-04/0d3c66ece10207b0d1dca9118560e16d.jpg')">
  57.   </div>
  58.   <div class="card" style="background-image: url('http://www.webqdkf.com/d/file/p/2019/09-04/63fb35a52cd8f01b68d78aa0b6c5b021.jpg')">
  59.   </div>
  60.    <div class="card" style="background-image: url('http://www.webqdkf.com/d/file/p/2019/09-04/63fb35a52cd8f01b68d78aa0b6c5b021.jpg')">
  61.    </div>
  62.   <div class="card"  style="background-image: url('http://www.webqdkf.com/d/file/p/2020/11-12/e973c393fd7fda278977fb2ff686fdc9.jpg')">
  63.   </div>
  64.   <div  class="card" style="background-image: url('http://www.webqdkf.com/d/file/p/2020/11-12/52d11e4aef71cdb14d95cded9ffc3bf8.jpg')">
  65.   </div>
  66.   <div  class="card" style="background-image: url('http://www.webqdkf.com/d/file/p/2020/08-31/a03f0b9879a5361ba0a5519cbe1dbac1.jpg')">
  67.   </div>
  68.   <div class="card"  style="background-image: url('http://www.webqdkf.com/d/file/p/2020/07-16/5ac65c5c5d7ead056d8ac2767b5648e1.jpg')">
  69.   </div>
  70.   <div  class="card" style="background-image: url('http://www.webqdkf.com/d/file/p/2020/07-09/b45e91e2b7c53f658f85f9d76f89aa77.jpg')">
  71.   </div>
  72.   <div  class="card" style="background-image: url('http://www.webqdkf.com/d/file/p/2020/07-08/186d506c004efa7896fa634e5139b6dc.jpg')">
  73.   </div>
  74.   <div  class="card" style="background-image: url('http://www.webqdkf.com/d/file/p/2020/07-09/f731e58e9bf7ee4c5b5b2a85e5dd2cbf.jpg')">
  75.   </div>
  76.    <div class="card"  style="background-image: url('http://www.webqdkf.com/d/file/p/2020/09-11/c0f179887b1f9efa98c4af944e8d699b.jpg')">
  77.   </div>
  78.   <div  class="card" style="background-image: url('http://www.webqdkf.com/d/file/p/2019/09-04/052746e2767ffb56e045a5e7017ccec9.jpg')">
  79.   </div>
  80.   <div  class="card" style="background-image: url('http://www.webqdkf.com/d/file/p/2019/09-05/ede4d9983eefd8f88bf03eb966bceb5c.jpg')">
  81.   </div>
  82.   <div class="card"  style="background-image: url('http://www.webqdkf.com/d/file/p/2019/09-05/2383cd70ff6d2f15350d64b0af572821.jpg')">
  83.   </div>
  84.   <div  class="card" style="background-image: url('http://www.webqdkf.com/d/file/p/2019/09-24/1442b8adc1f03141eb66e844e069caa8.jpg')">
  85.   </div>
  86.   <div  class="card" style="background-image: url('http://www.webqdkf.com/d/file/p/2019/09-04/0d3c66ece10207b0d1dca9118560e16d.jpg')">
  87.   </div>
  88.   <div  class="card" style="background-image: url('http://www.webqdkf.com/d/file/p/2019/09-04/052746e2767ffb56e045a5e7017ccec9.jpg')">
  89.   </div>
  90. </div>
  91. </body>
  92. </html>
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|paopaomj.COM ( 渝ICP备18007172号|渝公网安备50010502503914号 )

GMT+8, 2024-11-22 02:00

Powered by paopaomj X3.5 © 2016-2025 sitemap

快速回复 返回顶部 返回列表