首页
归档
笔记
树洞
搜索
友言

文章详情

Interesting People Record Interesting.

/ CSS / 文章详情

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

Sonder
2022-12-29
2733字
7分钟
浏览 (2k)

image.png
image.png
复制代码
<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>
        .box {
            display: flex;
            flex-wrap: wrap;
        }
        .imageBox {
            position: relative;
            overflow: hidden;
            margin-bottom: 2%;
            width: 300px;
        }
        .imageBox img:nth-child(2):nth-last-child(1),
        .imageBox img:nth-child(1):nth-last-child(2) {
            width: 49%;
        }
            /* 2/3 */
        .imageBox img:nth-child(1):nth-last-child(3),
        .imageBox img:nth-child(2):nth-last-child(2),
        .imageBox img:nth-child(3):nth-last-child(1) {
            width: 32%;
        }

        /* 4 or 1/2 */
        .imageBox img:nth-child(1):nth-last-child(4),
        .imageBox img:nth-child(2):nth-last-child(3),
        .imageBox img:nth-child(3):nth-last-child(2),
        .imageBox img:nth-child(4):nth-last-child(1) {
            width: 49%;
        }

        /*  5张以上图片  */
        .imageBox img:nth-child(1):nth-last-child(n + 5),
        .imageBox img:nth-child(1):nth-last-child(n + 5)~img {
            width: 32%;
        }
    </style>
</head>

<body>
<div class="box">
    <div class="imageBox">
        <!--<img src="https://t7.baidu.com/it/u=1595072465,3644073269&fm=193&f=GIF" />-->
        <!--<img src="https://t7.baidu.com/it/u=1595072465,3644073269&fm=193&f=GIF" />-->
        <!--<img src="https://t7.baidu.com/it/u=1595072465,3644073269&fm=193&f=GIF" />-->
        <!--<img src="https://t7.baidu.com/it/u=1595072465,3644073269&fm=193&f=GIF" />-->
        <img src="https://t7.baidu.com/it/u=1595072465,3644073269&fm=193&f=GIF" />
        <img src="https://t7.baidu.com/it/u=1595072465,3644073269&fm=193&f=GIF" />
        <img src="https://t7.baidu.com/it/u=1595072465,3644073269&fm=193&f=GIF" />
        <img src="https://t7.baidu.com/it/u=1595072465,3644073269&fm=193&f=GIF" />
        <img src="https://t7.baidu.com/it/u=1595072465,3644073269&fm=193&f=GIF" />
    </div>
</div>
</body>

</html>

参考:https://www.jianshu.com/p/8391e5068628

下一篇 / vue获取当前点击元素的父元素、子元素、上级元素等

🎯 相关文章

💡 推荐文章

🕵️‍♂️ 评论 (0)