使用 :not(selector) 选择除少数元素之外的所有元素



我正在尝试选择所有"类"类,除了前 3 个。我不确定为什么我的语法不正确。有人可以解释我在这里做错了什么吗?

我尝试了几种不同的组合,例如在第 n 个类型之前将类名包含在选择器中,等等。

.classes {
background: red;
width: 200px;
height: 30px;
margin-bottom: 5px;
}
.classes:not(.classes:nth-of-type(1)), .classes:not(nth-of-type(2)), .classes:not (nth-of-type(3)) {
background: blue;
}
.classes:nth-of-type(6) {
background: orange;
}
<div class='classes'>test</div>
<div class='classes'>test</div>
<div class='classes'>test</div>
<div class='classes'>test</div>
<div class='classes'>test</div>
<div class='classes'>test</div>
<div class='classes'>test</div>

你可以使用这个:

.classes {
background: green;   
}
.classes:nth-child(n+4) {
background: red;
}

您可以在此处找到更多有用的:nth-child示例

如果你想完全使用 :not 选择器,你可以试试这个:

.classes:not(:first-child):not(:nth-child(2)):not(:nth-child(3)) {
background: yellow;
}

最新更新