Is there a way to select a DOM’s only_child, n-th-child, etc? I know that there are selectors like some_tag:only-child, .some_class:only-child, #some_id:only-child, but that selects based on the tag or attributes (class, id, etc.) of itself. What I want is to do selection based on the tag or attribute of the parent.
For example, I might want to select the only-child of .some_class, which is the div with id B below, not the only-child that is .some_class, which is A.
<div>
<div class="some_class" id="A">
<div id="B">
</div>
</div>
</div>
If you’re looking for the only child of
.some_class, you need to separate the class selector and the pseudo-class with a combinator. Parent-child and ancestor-descendant relationships between two different sets of selectors are always indicated with a combinator (the child combinator and descendant combinator respectively).Given your HTML, you’ll want to use the child combinator
>to limit it to the only element that’s directly nested within.some_class: