:not
negation pseudo classThe negation CSS pseudo-class,
:not(X)
, is a functional notation taking a simple selector X as an argument. It matches an element that is not represented by the argument. X must not contain another negation selector.
You can use :not
to exclude any subset of matched elements, ordered as you would normal CSS selectors.
div:not(.class)
Would select all div
elements without the class .class
div:not(.class) {_x000D_
color: red;_x000D_
}
_x000D_
<div>Make me red!</div>_x000D_
<div class="class">...but not me...</div>
_x000D_
:not(div) > div
Would select all div
elements which arent children of another div
div {_x000D_
color: black_x000D_
}_x000D_
:not(div) > div {_x000D_
color: red;_x000D_
}
_x000D_
<div>Make me red!</div>_x000D_
<div>_x000D_
<div>...but not me...</div>_x000D_
</div>
_x000D_
With the notable exception of not being able to chain/nest :not
selectors and pseudo elements, you can use in conjunction with other pseudo selectors.
div {_x000D_
color: black_x000D_
}_x000D_
:not(:nth-child(2)){_x000D_
color: red;_x000D_
}
_x000D_
<div>_x000D_
<div>Make me red!</div>_x000D_
<div>...but not me...</div>_x000D_
</div>
_x000D_
:not
is a CSS3 level selector, the main exception in terms of support is that it is IE9+
The spec also makes an interesting point:
the
:not()
pseudo allows useless selectors to be written. For instance:not(*|*)
, which represents no element at all, orfoo:not(bar)
, which is equivalent tofoo
but with a higher specificity.