I'm trying to use the :not()
property to exclude a pair of classes from a rule, e.g.:
*:not(.class1, class2) { display: none; }
However, it looks like the not()
property doesn't support comma separated classes, as show in this fiddle.
HTML:
<div class='one'> foo </div> <div class='two'> foo </div> <div class='three'> foo </div> <div class='four'> foo </div>
CSS:
div { background-color: #CBA; } div:not(.one) { background-color: #ABC; } div:not(.one, .three) { color: #F00; }
The first and second rules get applied, but the third doesn't.
I can't do *:not(.class1), *:not(.class2)
because any element which has class2
will be selected by *:not(.class1)
and vice versa.
I don't want to do
* { display: none;} .class1, .class2 { display: inline; }
because not all .class1
and .class2
elements have the same original display property, and I want them to retain it.
How can I exclude multiple classes from a rule, either with the not()
property or otherwise?
To specify multiple classes, separate the class names with a space, e.g. <span class="left important">. This allows you to combine several CSS classes for one HTML element. Naming rules: Must begin with a letter A-Z or a-z.
There are no logical combinators with :not() , like and or or , but you can chain them, which is effectively like and . The :not() selector doesn't add any specificy by itself, but what is inside does, so :not(.
HTML elements can be assigned multiple classes by listing the classes in the class attribute, with a blank space to separate them.
You can use:
div:not(.one):not(.three) { color: #F00; }
Fiddle
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