2

Haii

Looks like the css selector :not() doesn't work well with the * selector.

Any way around it? Or am I doing something wrong?

*:not(.nope){
  color: green;
}
<div>hai</div>
<div>I</div>
<div class="nope">am</div>
<div>Jhon</div>

I still get 'am' as green.

Thanks in advance!

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
Jhon
  • 135
  • 8

1 Answers1

4

The universal selector (*) is not the problem. It's the inheritance on the color property.

When you say...

*:note(.nope)

that's fine, but you're forgetting that * applies the color to the body and html elements, as well.

So .nope gets the green from its parent.

If you use a property that is not inherited (like border) you won't have this problem.

*:not(.nope){
  border: 1px solid red;
}

* {
  margin: 5px;
}
<div>hai</div>
<div>I</div>
<div class="nope">am</div>
<div>Jhon</div>

Notice how .nope doesn't get the border.

For the color to work as you want, be more specific.

div:not(.nope) {
  color: green;
}
<div>hai</div>
<div>I</div>
<div class="nope">am</div>
<div>Jhon</div>
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
  • 1
    Oh okay thanks. I found that I could also do *{ color: green; } .nope{ color: blue; } – Jhon Jun 16 '20 at 19:11