I don't understand why the following code has not desired behaviour:
.toggle {
color: red;
}
:not(.list) .toggle {
font-weight:bold;
}
<div class="container">
<a href="#!" class="toggle">Toggle</a>
<ul class="list">
<li><a href="#!">Link 1</a></li>
<li>
<div class="container">
<a href="#!" class="toggle">SubToggle</a>
<ul class="list">
<li><a href="#!">SubLink 1</a></li>
<li>
<a href="#!">SubLink 2</a>
</li>
<li><a href="#!">SubLink 3</a></li>
</ul>
</div>
</li>
<li><a href="#!">Link 3</a></li>
</ul>
</div>
I thought that using :not()
would result in applying "bold" only to main "Toggle" link but instead it applis "bold" to all of red ones. Why?
Please, note that this code is nested with same class names, I don't want to target specific levels with different css classes, I would like to target elements only with descendant selectors and other operators
Here is present also a jsFiddle to directly try.
I think you might want this:
.toggle {
color: red;
font-weight: bold;
}
div *:not(.list) .toggle {
font-weight: normal;
}
I tried so many times but this is the only way I can do:
.toggle {
color: red;
font-weight:bold;
}
.list .toggle{
//override
font-weight:normal;
}
This is how to use :not the right way:
add specialToggle
for elements you do not want to select
<a href="#!" class="toggle specialToggle">SubToggle</a>
and then css:
.toggle {
color: red;
}
.toggle:not(.specialToggle) {
font-weight:bold;
}
https://jsfiddle.net/s249tyur/3/
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