[英]CSS - selector for first element anywhere under a parent
How can I get the selector for "Label 1" below without having to use specific divs?如何在不使用特定 div 的情况下获取下面“标签 1”的选择器? I thought
#wrapper label:first-of-type
would do the trick but that appears to be selecting every label.我认为
#wrapper label:first-of-type
可以解决问题,但这似乎是选择每个标签。
<div id="wrapper">
<div class="row">
<div class="content-1">
<label>Label 1</label>
</div>
<div class="content-2">
<label>Label 2</label>
</div>
<div class="content-3">
<label>Label 3</label>
</div>
<div class="content-4">
<label>Label 4</label>
</div>
</div>
</div>
#wrapper label:first-of-type
won't work as every label
is the first of it's type within it's immediate parent. #wrapper label:first-of-type
不起作用,因为每个label
都是其直接父级中的第一个类型。 That's the key here, these sorts of selectors are always relative to the immediate parent.这是这里的关键,这些类型的选择器总是相对于直接父级。
So, you could do something like this:所以,你可以做这样的事情:
#wrapper div:first-child > label
which would select any label
elements which are an immediate child of a div
which is the first child within it's parent这将选择任何
label
元素,这些元素是div
的直接子元素, div
是其父元素中的第一个子元素
use nth-child使用 nth-child
#wrapper .content-1:nth-child(1) label{ background: red; }
<div id="wrapper"> <div class="row"> <div class="content-1"> <label>Label 1</label> </div> <div class="content-2"> <label>Label 2</label> </div> <div class="content-3"> <label>Label 3</label> </div> <div class="content-4"> <label>Label 4</label> </div> </div> </div>
Xpath can do this easily, it would be something like this: (//*[@id="wrapper"]//label)[1] Xpath 可以很容易地做到这一点,它会是这样的: (///*[@id="wrapper"]//label)[1]
I have not found its equivalence in css yet.我还没有在 css 中找到它的等效性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.