CSS实现微信朋友圈的九宫格图片自适应

2334次阅读  |  发布于2年以前

大家好, 微信朋友圈都发过吧?它支持最多发9张图片,所以我们也称之为九宫格

要知道发1~9任意数量的图片时,这些图片的排列方式可能有所不同。大体有这四种排列情况:

情况一:只有一张图,完整展示整张图

1张图

情况二:2~3张图的情况,无法完整展示每张图,则在第一排依次排开(一排最多3张)

2张图 & 3张图

情况三:4张图的情况,无法完整展示每张图,每排2张图,共2排

4张图

情况四:5~9张图的情况跟情况二类似,也无法完整展示每张图,每排最多3张图,依次排开

5张图

其实就是对不同数量的图片做了一个排版的自适应,原理也很简单,我们一起来用CSS实现一下吧~

没有完全按照微信的排版是,自己改良了一下,最终完整效果见下面这个视频

创建容器

先创建一个元素作为容器

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

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

.pictures-adaptive {
  display: flex;
  flex-wrap: wrap;
}

1张图片

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

<div class="pictures-adaptive">
  <div class="wrap">
    <img src="demo.png"/>
  </div>
</div>

样式也不用设置,最终效果是这样的

1张图片 多张图片

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

/* 相对定位,img相对于.wrap相对定位 */
.wrap {
  position: relative;
  overflow: hidden;
  margin-bottom: 2%;
}

/* 大于1张图片时,img标签的样式 */
.wrap:not(:nth-child(1):nth-last-child(1)) img {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

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

2或4张图片

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

/*  2张图片  */
.wrap:nth-child(1):nth-last-child(2),
.wrap:nth-child(2):nth-last-child(1),
/*  4张图片  */
.wrap:nth-child(1):nth-last-child(4),
.wrap:nth-child(2):nth-last-child(3),
.wrap:nth-child(3):nth-last-child(2),
.wrap:nth-child(4):nth-last-child(1)
{
  width: 49%;
  padding-bottom: 49%;
}


/* 每行的两张图片中间间隔2%的宽度 */
/*  2张图片  */
.wrap:nth-child(2):nth-last-child(1),
/*  4张图片  */
.wrap:nth-child(2):nth-last-child(3),
.wrap:nth-child(4):nth-last-child(1)
{
  margin-left: 2%;
}

最终效果是这样的

2张图片

4张图片

3张图片

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

/*  3张图片  */
.wrap:nth-child(1):nth-last-child(3),
.wrap:nth-child(2):nth-last-child(2),
.wrap:nth-child(3):nth-last-child(1) 
{
  width: 32%;
  padding-bottom: 32%;
}

/*  间隔  */
.wrap:nth-child(2):nth-last-child(2),
.wrap:nth-child(3):nth-last-child(1) 
{
  margin-left: 2%;
}

最终效果是这样的

3张图片

5~9张图片

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

/*  5张以上图片  */
.wrap:nth-child(n + 5),
.wrap:nth-child(1):nth-last-child(n + 5),
.wrap:nth-child(1):nth-last-child(n + 5) ~ .wrap
{
  width: 32%;
  padding-bottom: 32%;
}

/*  间隔  */
.wrap:nth-child(n + 5):not(:nth-child(3n + 1)),
.wrap:nth-child(1):nth-last-child(n + 5) ~ .wrap:not(:nth-child(3n + 1))
{
  margin-left: 2%;
}

最终效果是这样的

9张图片

完整代码

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>微信朋友圈图片九宫格排版自适应(改编版)</title>
  <style>
    .pictures-adaptive {
      display: flex;
      flex-wrap: wrap;
    }

    .wrap {
      position: relative;
      overflow: hidden;
      margin-bottom: 2%;
    }

    /*  3张图片  */
    .wrap:nth-child(1):nth-last-child(3),
    .wrap:nth-child(2):nth-last-child(2),
    .wrap:nth-child(3):nth-last-child(1) 
    {
      width: 32%;
      padding-bottom: 32%;
    }

    /*  间隔  */
    .wrap:nth-child(2):nth-last-child(2),
    .wrap:nth-child(3):nth-last-child(1) 
    {
      margin-left: 2%;
    }

    .wrap:not(:nth-child(1):nth-last-child(1)) img {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      object-fit: cover;
    }

    /*  2张图片  */
    .wrap:nth-child(1):nth-last-child(2),
    .wrap:nth-child(2):nth-last-child(1),
    /*  4张图片  */
    .wrap:nth-child(1):nth-last-child(4),
    .wrap:nth-child(2):nth-last-child(3),
    .wrap:nth-child(3):nth-last-child(2),
    .wrap:nth-child(4):nth-last-child(1)
    {
      width: 49%;
      padding-bottom: 49%;
    }

    /* 每行的两张图片中间间隔2%的宽度 */
    /*  2张图片  */
    .wrap:nth-child(2):nth-last-child(1),
    /*  4张图片  */
    .wrap:nth-child(2):nth-last-child(3),
    .wrap:nth-child(4):nth-last-child(1)
    {
      margin-left: 2%;
    }

    /*  5张以上图片  */
    .wrap:nth-child(n + 5),
    .wrap:nth-child(1):nth-last-child(n + 5),
    .wrap:nth-child(1):nth-last-child(n + 5) ~ .wrap
    {
      width: 32%;
      padding-bottom: 32%;
    }

    .wrap:nth-child(n + 5):not(:nth-child(3n + 1)),
    .wrap:nth-child(1):nth-last-child(n + 5) ~ .wrap:not(:nth-child(3n + 1))
    {
      margin-left: 2%;
    }
  </style>
</head>
<body>
  <div class="pictures-adaptive">
    <div class="wrap">
      <img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAoHCBYVFRgWFRYYGBgYGBgYGBgaGBgYGBgYGBgZGRgYGBgcIS4lHB4rIRgYJjgmKy8xNTU1GiQ7QDs0Py40NTEBDAwMEA8QHhISHzQrISQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0Mf/AABEIAKgBLAMBIgACEQEDEQH/xAAbAAACAwEBAQAAAAAAAAAAAAADBAACBQEGB//EADwQA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NodUOl9bX7pX1ttD/AFFGqX0M6HvofAzOoOh3Oe+Xo1sp2uDuOBEz1qlfCM4FldsrHKG2PAHr0g0aqPRYWpks9P20O6E+0v8AtPymzhMbRLAglG4gixHhwnicUlXDtZgRxBGxHAg8Z6LsXtGlWAWoozjZho39SVT8lZrzh7pcaQt7hhb4d/GYPanaCG9rqeXDyPyhFRqQul3Tiv4h1ExO0cTScEg77gj5RVrNxLyjNxOIJJsb28/rEqmK4G468PvzlKy21Vgeh+UVq4ph7y35n6/zKzJOqLu/l0/iB05/GCNdT7pseRgjW6Sqkk6PB3nbySS5znc0gbkJJIAWzczLKfsySQAgl1tJJAAgfp5yFyeMkk0CuflIBzkkgBcPJe8kkwCXllkkgBYvLJJJA0IGkLTskwDoeWV7SSTDQqVNIfD1LX75JIrGR1KlmjS1NOkkkUZFzUuJFe+0kkDTnruesqXINxJJMMPT9j9to6iliFzIdATqUJ5dPvvX7Z7OOHYOhuh1VhuOUkkn8lV5Rodl+keZclQm40Vxrfvi/abK1yPa5g6HvBkkmqVoJtoxbA+65HNW3E49QrodufD+JJI5MXrop1tbqNoCx5ySSgjP/9k=">
      <!-- More imgs -->
    </div>
  </div>
</body>
</html>

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8