HTML
<div class="container">
<div class="row"></div>
<div class="row"></div>
<div class="row"></div>
</div>
css
.container { width: 500px; height: 500px;}
.row { height: 1.5em; width: 100%; background-color: pink;}正确的实现方式
.row:not(:nth-child(1)) { margin-top: 10px; }错误的实现方式
.row:not(.row:nth-child(1)) { margin-top: 10px; }
.row:not(.row:first-child) { margin-top: 10px; }
正确的效果:
我不太明白为什么这两种错误的方式错在哪里?
我查了下文档,大概总结下是因为
:not()
伪类选择器只能接收一个简单选择器作为参数。而你的例子中后两个
:not()
中传入的都不是一个简单选择器,而是一个简单选择器序列,所以是不生效的。这是我根据文档的理解,下面是文档中的相关引用。
简单选择器和简单选择器序列
:not() 选择器
前往文档