更优雅的博客图片排版


Demo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<!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>
img {
object-fit: cover;
width: 100%;
//max-height: 200px;
}
.pictures-adaptive {
display: flex;
flex-wrap: wrap;
background-color: "#f3f5f7";
width: 66%;
}
.wrap {
position: relative;
overflow: hidden;
margin-bottom: 2%;
width: 100%;
}
.wrap:not(:nth-child(1):nth-last-child(1)) img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
object-fit: cover;
}
.wrap:nth-child(1):nth-last-child(2), /* 2张图片 */
.wrap:nth-child(2):nth-last-child(1),
.wrap:nth-child(1):nth-last-child(4), /* 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%;
}
.wrap:nth-child(1):nth-last-child(3), /* 3张图片 */
.wrap:nth-child(2):nth-last-child(2),
.wrap:nth-child(3):nth-last-child(1),
.wrap:nth-child(n + 5), /* 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%;
}
/* 每行的两张图片中间间隔2%的宽度 */
.wrap:nth-child(2):nth-last-child(1),
.wrap:nth-child(2):nth-last-child(3),
.wrap:nth-child(4):nth-last-child(1),
.wrap:nth-child(2):nth-last-child(2),
.wrap:nth-child(3):nth-last-child(1),
.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="/images/covers/cover_050620.jpg">
</div>
<div class="wrap">
<img src="/images/covers/cover_050620.jpg">
</div>
<!-- More imgs -->
</div>
</body>
</html>

Stylus 源文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
.article-photo-grid
@media mq-mini
width: 100%;
display: flex
flex-wrap: wrap
width: 66%
.wrap
position: relative
overflow: hidden
margin-bottom: 2%
width: 100%
/* 除只有1张外 */
&:not(:nth-child(1):nth-last-child(1)) img, video
position: absolute
top: 0
left: 0
width: 100%
height: 100%
object-fit: cover
/* 2张/4张图片 */
&:nth-child(1):nth-last-child(2), // 2张
&:nth-child(2):nth-last-child(1),
&:nth-child(1):nth-last-child(4), // 4张
&:nth-child(2):nth-last-child(3),
&:nth-child(3):nth-last-child(2),
&:nth-child(4):nth-last-child(1)
width: 49%
padding-bottom: 49%
/* 3张/5张图片 */
&:nth-child(1):nth-last-child(3), // 3张
&:nth-child(2):nth-last-child(2),
&:nth-child(3):nth-last-child(1),
&:nth-child(n + 5), // 5张及以上
&:nth-child(1):nth-last-child(n + 5),
&:nth-child(1):nth-last-child(n + 5) ~ &
width: 32%
padding-bottom: 32%
/* 间隔 */
&:nth-child(2):nth-last-child(1), // 2张
&:nth-child(2):nth-last-child(3), // 4张
&:nth-child(4):nth-last-child(1),
&:nth-child(2):nth-last-child(2), // 3张
&:nth-child(3):nth-last-child(1),
&:nth-child(n + 5):not(:nth-child(3n + 1)), // 5张及以上
&:nth-child(1):nth-last-child(n + 5) ~ &:not(:nth-child(3n + 1))
margin-left: 2%

使用:

1
2
3
4
5
6
<div class="article-photo-grid">
<div class="wrap"><img src="/images/covers/cover_050620.jpg"/></div>
</div><div class="article-photo-grid">
<div class="wrap"><img src="/images/covers/cover_050620.jpg"/></div>
<div class="wrap"><img src="/images/covers/cover_050620.jpg"/></div>
</div>

参考资料