简体   繁体   中英

CSS :not selector doesn't work

This is probably a really stupid question, but I can't get the :not selector to work in css. I want to color all text in my page unless it has a class called "nocolor". Here's the code, I don't see the problem:

 *:not(.nocolor) { color: #f00; } 
 <h2>Hello</h2> <h2 class="nocolor">Hello</h2> 

I was surprised by this behavior but the * selector applies to everything so you have to look out for application to parent elements as well (like body and html tags themselves).

You can fix it by adding body to the selector, like so:

body *:not(.nocolor) {
   color: red;
}

Your selector should be written like so:

:not(.nocolor) {
    color: #f00;
}

Remove the '*', and this will select everything on the page.

See docs here: MDN

这是因为*应用于包含body每个元素,而没有.nocolor类。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM