HTML:
<div id="parent">
<div class="tt">tt</div>
<div class="tt">tt</div>
<div class="huh">huh</div>
</div>
CSS:
.tt:nth-child(2) {
color:blue;
}
#parent .tt:last-child {
color:red;
}
是否可以选择具有提供类的最后一个子元素?
最佳答案
div.tt:last-child {
background:yellow;
}
实际上选择了最后一个 child 。但是,如果它不是父类中的最后一个实例,则它不会选择类的最后一个实例。
因此您必须将 HTML 更改为:
<div id="parent">
<div id="ttwrppaer">
<div class="tt">tt</div>
<div class="tt">tt</div>
<div class="tt">tt</div>
</div>
<div class="hhhd">tt</div>
</div>
这是一个 demo
关于带有类选择器的 CSS last-child,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20299851/