Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple classes inside :not() [duplicate]

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?

like image 834
asfallows Avatar asked Jun 17 '14 14:06

asfallows


People also ask

How do you reference multiple classes in CSS?

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.

Can you chain not CSS?

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(.

Can an element have two classes?

HTML elements can be assigned multiple classes by listing the classes in the class attribute, with a blank space to separate them.


1 Answers

You can use:

div:not(.one):not(.three) {     color: #F00; } 

Fiddle

like image 133
Beterraba Avatar answered Nov 11 '22 00:11

Beterraba