Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I target the first child that does not have a particular class?

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

like image 959
SystemicPlural Avatar asked May 07 '14 11:05

SystemicPlural


People also ask

What selector to use if we want to select the first child of its parent that is of its type?

The :first-child selector is used to select the specified selector, only if it is the first child of its parent.

How do you select an element without a class?

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.

How do you choose not your first child?

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.

How do I choose an immediate child?

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.


2 Answers

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.

like image 120
Hashem Qolami Avatar answered Oct 01 '22 14:10

Hashem Qolami


I think this is what you want

#some_list>li.hide + li:not(.hide){

    color: #CCC;

}
like image 20
sumgeek Avatar answered Oct 01 '22 15:10

sumgeek