I try to select all li
s in the page except whose are inside a container(ascendant) with class .foo
Output: The li 22
should not be colored.
I can't use direct descendants because there could be nested lis and uls.
I try to select all li
s in the page except whose are inside a container(ascendant) with class .foo
Output: The li 22
should not be colored.
I can't use direct descendants because there could be nested lis and uls.
It's not working because you haven't specified that it's that exact element that can't have the class. The ul
element that contains the li
element doesn't have that class, so that matches the :not(.foo)
part of the selector.
Make sure that the :not(.foo)
part applies only to the right element, for example by specifying exactly where the element is in relation to the list element:
:not(.foo) > ul > li { background: red; }
<ul>
<li>11</li>
</ul>
<div class="foo">
<ul>
<li>22</li>
</ul>
</div>
I guess css selectors are using an OR
or ORELSE
relation. So when you use a selector like :not(.foo) li
something similar happens:
statement1
: In first place it will check: is actual element's tagname is li
? (true
)
statement2_1
:In second place it will check: is closest parents class not .foo
? (true
)
statement2_2
:In next place it will check next parent: is next parent class not .foo
(false
)
Theoretically the following statement evaluates:
statement1 AND (statement2_1 ORELSE statement2_2)
true AND (true ORELSE false) => true AND true = > true
What if you just try other way:
li{
background-color: red;
}
.foo li{
background-color: initial;
}
<ul>
<li>11</li>
</ul>
<div class=foo>
<ul>
<li>22</li>
</ul>
</div>