比如我要实现 这样的样式如单独循环第一个或者第三个DIV都会只显示一种

01701-uo0af24r1dq.png

一下就是样式DEMO
方法也是自己查资料 和 JOE大佬的讲解

首先参考的是一下资料

<style>
    /* 循环重复的样式   nth-child(n)  */
    li:nth-child(4n+1){
        background-color: aqua;
    }
    li:nth-child(4n+2){
        background-color: blue;
    }
    li:nth-child(4n+3){
        background-color: blueviolet;
    }
    li:nth-child(4n+4){
        background-color: teal;
    }
</style>

以上的样式可以实现 4个不同的样式 并且4个一重复

我要的是前面两个一样 第三个 第六个开始发生改变

.col.mg66 {
width: 50%;
margin-left: auto;
left: auto;
right: auto;
}
.col.mg66:nth-child(3n) {
width: 100%;
margin-left: auto;
left: auto;
right: auto;
}

然后这样写就可以了

然后调用MG66循环就行了

JOE给出了另外一个DEMO

自己可以去学习一下

 <!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>Document</title>
    <style>
        * {
            box-sizing: border-box;
        }
        .box {
            width: 30%;
            border: 2px solid #ff6800;
            margin: 0 auto;
            display: grid;
            padding: 10px;

            grid-template-columns: repeat(2, 1fr);
            gap: 10px;
        }
        .item {
            height: 200px;
            background: #ff6800;
        }
        .item:nth-child(3n) {
            grid-column: 1 / 3;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
    </div>
</body>
</html>

另外有大佬说还可以根据文中类型去判断 这个我暂时没去查看资料 只能大概明白其中意思 你们如果有查到相关资料可以留言,我也去看看学习下


扫描二维码,在手机上阅读!