我希望选择不同父母中的第一个div .labelinput
。我不想选择父母,因为我有不同的父母。是否可以只定义一次?
尝试:
div.labelinput:first-child {
border-top: 1px solid #efefef;
}

<div class="one">
<div class="labelinput">style this</div>
<div class="labelinput">not this</div>
<div class="labelinput">not this</div>
</div>
<div class="two">
<div class="labelinput">style this</div>
<div class="labelinput">not this</div>
<div class="labelinput">not this</div>
</div>
<!-- ... -->
<div class="seventeen">
<div class="labelinput">style this</div>
<div class="labelinput">not this</div>
<div class="labelinput">not this</div>
</div>
&#13;
答案 0 :(得分:3)
div .labelinput:first-child {
border-top: 1px solid #efefef;
}
答案 1 :(得分:2)
您的示例更具体:
div div.labelinput:first-child {
color: red;
}
&#13;
<div class="one">
<div class="labelinput">style this</div>
<div class="labelinput">not this</div>
<div class="labelinput">not this</div>
</div>
<div class="two">
<div class="labelinput">style this</div>
<div class="labelinput">not this</div>
<div class="labelinput">not this</div>
</div>
<div class="seventeen">
<div class="labelinput">style this</div>
<div class="labelinput">not this</div>
<div class="labelinput">not this</div>
</div>
&#13;
OP评论后,最终的解决方案是:
div div.labelinput:nth-of-type(2) {
border-top: 1px solid red;
}
答案 2 :(得分:1)
第一种类型也有效:
div.labelinput:first-of-type {
border-top: 1px solid #efefef;
}