我只需要选择first
和third
/ last元素。
在我决定建立链接之前,一切都很好。在我的实际来源divs
是img
小号
body {
background-color: black;
color: white;
}
.this:first-child {
color: red;
}
.this:last-child {
color: aqua;
}
<div class="main">
<a href="">
<div class="this">
FIRST
</div>
</a>
<a href="">
<div class="this">
SECOND
</div>
</a>
<a href="">
<div class="this">
THIRD
</div>
</a>
</div>
您的.this
元素被另一个元素(a
)包裹,并且它们是唯一的子元素。这意味着它们既是第一个也是最后一个孩子,所以最后一条规则将接管。将:first-child
和:last-child
选择器移至a
元素:
body {
background-color: black;
color: white;
}
a:first-child .this {
color: red;
}
a:last-child .this {
color: aqua;
}
<div class="main">
<a href="">
<div class="this">
FIRST
</div>
</a>
<a href="">
<div class="this">
SECOND
</div>
</a>
<a href="">
<div class="this">
THIRD
</div>
</a>
</div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句