If I have the following html
<ul id="some_list">
<li class="hide">Do Not Target This</li>
<li class="hide">Do Not Target This</li>
<li>Target This</li>
<li>Do Not Target This</li>
</ul>
How do I target the first child that does not have the hide class? eg
#some_list>li:not(.hide):first-child {
color: #777;
}
But this doesn't work.
Fiddle
The :first-child selector is used to select the specified selector, only if it is the first child of its parent.
In CSS, to exclude a particular class, we can use the pseudo-class :not selector also known as negation pseudo-class or not selector. This selector is used to set the style to every element that is not the specified by given selector. Since it is used to prevent a specific items from list of selected items.
Use the :not(selector) Selector Not to Select the First Child in CSS. We can use the :not(selector) selector to select every other element that is not the selected element. So, we can use the selector not to select the first child in CSS. We can use :first-child as the selector in the :not(selector) selector.
The child combinator ( > ) is placed between two CSS selectors. It matches only those elements matched by the second selector that are the direct children of elements matched by the first. Elements matched by the second selector must be the immediate children of the elements matched by the first selector.
You could use CSS adjacent sibling selector in order to select the first li
element which doesn't have the .hide
class:
EXAMPLE HERE.
li:first-child:not(.hide),
li.hide + li:not(.hide) {
background-color: gold;
}
Also consider checking the first list item (the first child) for existence of .hide
class name.
I think this is what you want
#some_list>li.hide + li:not(.hide){
color: #CCC;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With