I am trying to exclude some CSS rules when there is a defined CSS class as a parent with this rule:
.cc div:not(.cc-ei) li { color: red; }
But it doesn’t work for all cases, as you can see in the code below, and the linked Codepen demo:
.item {
color: green;
}
.cc div:not(.cc-ei) li {
color: red;
}
<div class="cc">
<div>
<ul>
<li>Should be red</li>
</ul>
</div>
<div class="cc-ei">
<ul>
<li class="item">Should be green</li>
</ul>
</div>
<div class="toto">
<div class="cc-ei">
<ul>
<li class="item">Should be green</li>
</ul>
</div>
</div>
</div>
How could I define the rule .cc li { color: red; }
to exclude <li>
that are in <div class="cc-ei">
?
3
Answers
Logically your selector looks for list items in any div without the class. Since your list is nested in multiple divs, one without the class, that condition supersedes. You can specify a child relationship (as opposed to a general descendant relationship) to get it to work.
The easiest way to achieve your requirements is to simply use the following rule, to style the
<li>
elements:The revised code is below, with explanatory comments:
References:
color
.font
.:not()
.Simple as the author (me). Just tell it what you want:
li
items within.cc-ei
should be green.