CSS

创建容器

先创建一个元素作为容器

  1. <div class="pictures-adaptive">
  2. <!-- 里面放 img 图片 -->
  3. </div>

并将其设置为 Flex 布局,且可换行

  1. .pictures-adaptive {
  2. display: flex;
  3. flex-wrap: wrap;
  4. }

1张图片

只有1张图片的情况最简单,就让图片直接展示就好,但大于1张图时,所有图片都是无法完全展示的,它们是以一个正方形的形式展现的,所以需要给 img 外侧包一个容器,便于后续控制图片的展示

  1. <div class="pictures-adaptive">
  2. <div class="wrap">
  3. <img src="demo.png"/>
  4. </div>
  5. </div>

样式也不用设置,最终效果是这样的
CSS 实现微信朋友圈的九宫格图片自适应 - 图1
1张图片

多张图片

大于1张图片时,图片需要在正方形容器中非完整居中展示,可以让 img 标签相对于外部容器元素相对定位

  1. /* 相对定位,img相对于.wrap相对定位 */
  2. .wrap {
  3. position: relative;
  4. overflow: hidden;
  5. margin-bottom: 2%;
  6. }
  7. /* 大于1张图片时,img标签的样式 */
  8. .wrap:not(:nth-child(1):nth-last-child(1)) img {
  9. position: absolute;
  10. top: 0;
  11. left: 0;
  12. width: 100%;
  13. height: 100%;
  14. object-fit: cover;
  15. }

分析了一下,多张图片的情况可以分为 2、4张图片 或者 3张图片、5~9张图片

2或4张图片

2或4张图片时都是一行只有两个图片,且两张图片各占一半左右的宽度

  1. /* 2张图片 */
  2. .wrap:nth-child(1):nth-last-child(2),
  3. .wrap:nth-child(2):nth-last-child(1),
  4. /* 4张图片 */
  5. .wrap:nth-child(1):nth-last-child(4),
  6. .wrap:nth-child(2):nth-last-child(3),
  7. .wrap:nth-child(3):nth-last-child(2),
  8. .wrap:nth-child(4):nth-last-child(1)
  9. {
  10. width: 49%;
  11. padding-bottom: 49%;
  12. }
  13. /* 每行的两张图片中间间隔2%的宽度 */
  14. /* 2张图片 */
  15. .wrap:nth-child(2):nth-last-child(1),
  16. /* 4张图片 */
  17. .wrap:nth-child(2):nth-last-child(3),
  18. .wrap:nth-child(4):nth-last-child(1)
  19. {
  20. margin-left: 2%;
  21. }

最终效果是这样的
CSS 实现微信朋友圈的九宫格图片自适应 - 图2
2张图片
CSS 实现微信朋友圈的九宫格图片自适应 - 图3
4张图片

3张图片

3张图片的情况是:三张图排列在同一行

  1. /* 3张图片 */
  2. .wrap:nth-child(1):nth-last-child(3),
  3. .wrap:nth-child(2):nth-last-child(2),
  4. .wrap:nth-child(3):nth-last-child(1)
  5. {
  6. width: 32%;
  7. padding-bottom: 32%;
  8. }
  9. /* 间隔 */
  10. .wrap:nth-child(2):nth-last-child(2),
  11. .wrap:nth-child(3):nth-last-child(1)
  12. {
  13. margin-left: 2%;
  14. }

最终效果是这样的
CSS 实现微信朋友圈的九宫格图片自适应 - 图4
3张图片

5~9张图片

5~9张图片的情况其实跟3张图片的排版是一样的,不过可以简化写法

  1. /* 5张以上图片 */
  2. .wrap:nth-child(n + 5),
  3. .wrap:nth-child(1):nth-last-child(n + 5),
  4. .wrap:nth-child(1):nth-last-child(n + 5) ~ .wrap
  5. {
  6. width: 32%;
  7. padding-bottom: 32%;
  8. }
  9. /* 间隔 */
  10. .wrap:nth-child(n + 5):not(:nth-child(3n + 1)),
  11. .wrap:nth-child(1):nth-last-child(n + 5) ~ .wrap:not(:nth-child(3n + 1))
  12. {
  13. margin-left: 2%;
  14. }

最终效果是这样的
CSS 实现微信朋友圈的九宫格图片自适应 - 图5
9张图片

完整代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>微信朋友圈图片九宫格排版自适应(改编版)</title>
  8. <style>
  9. .pictures-adaptive {
  10. display: flex;
  11. flex-wrap: wrap;
  12. }
  13. .wrap {
  14. position: relative;
  15. overflow: hidden;
  16. margin-bottom: 2%;
  17. }
  18. /* 3张图片 */
  19. .wrap:nth-child(1):nth-last-child(3),
  20. .wrap:nth-child(2):nth-last-child(2),
  21. .wrap:nth-child(3):nth-last-child(1)
  22. {
  23. width: 32%;
  24. padding-bottom: 32%;
  25. }
  26. /* 间隔 */
  27. .wrap:nth-child(2):nth-last-child(2),
  28. .wrap:nth-child(3):nth-last-child(1)
  29. {
  30. margin-left: 2%;
  31. }
  32. .wrap:not(:nth-child(1):nth-last-child(1)) img {
  33. position: absolute;
  34. top: 0;
  35. left: 0;
  36. width: 100%;
  37. height: 100%;
  38. object-fit: cover;
  39. }
  40. /* 2张图片 */
  41. .wrap:nth-child(1):nth-last-child(2),
  42. .wrap:nth-child(2):nth-last-child(1),
  43. /* 4张图片 */
  44. .wrap:nth-child(1):nth-last-child(4),
  45. .wrap:nth-child(2):nth-last-child(3),
  46. .wrap:nth-child(3):nth-last-child(2),
  47. .wrap:nth-child(4):nth-last-child(1)
  48. {
  49. width: 49%;
  50. padding-bottom: 49%;
  51. }
  52. /* 每行的两张图片中间间隔2%的宽度 */
  53. /* 2张图片 */
  54. .wrap:nth-child(2):nth-last-child(1),
  55. /* 4张图片 */
  56. .wrap:nth-child(2):nth-last-child(3),
  57. .wrap:nth-child(4):nth-last-child(1)
  58. {
  59. margin-left: 2%;
  60. }
  61. /* 5张以上图片 */
  62. .wrap:nth-child(n + 5),
  63. .wrap:nth-child(1):nth-last-child(n + 5),
  64. .wrap:nth-child(1):nth-last-child(n + 5) ~ .wrap
  65. {
  66. width: 32%;
  67. padding-bottom: 32%;
  68. }
  69. .wrap:nth-child(n + 5):not(:nth-child(3n + 1)),
  70. .wrap:nth-child(1):nth-last-child(n + 5) ~ .wrap:not(:nth-child(3n + 1))
  71. {
  72. margin-left: 2%;
  73. }
  74. </style>
  75. </head>
  76. <body>
  77. <div class="pictures-adaptive">
  78. <div class="wrap">
  79. <img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAoHCBYVFRgWFRYYGBgYGBgYGBgaGBgYGBgYGBgZGRgYGBgcIS4lHB4rIRgYJjgmKy8xNTU1GiQ7QDs0Py40NTEBDAwMEA8QHhISHzQrISQ0N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