CSS Reference ยท Pseudo-classes
The :not() pseudo-class
Reference for the CSS :not() pseudo-class: match elements that do not match any selector in a list, how specificity works, and common patterns with examples.
The :not() pseudo-class is the negation selector. It takes a list of selectors and matches every element that matches none of them. li:not(.done) selects list items without the class done.
Modern CSS lets you pass complex selectors and comma-separated lists: :not(.a, .b) means "neither .a nor .b". The pseudo-class itself adds no specificity; the argument does.
a:not([href^="http"]) { color: inherit; }
Try it
Open items get the yellow highlight. Items that are neither .done nor .skipped are also bold.
Syntax
:not( <complex-selector-list> ) { }
- Arguments: one or more selectors, separated by commas. Complex selectors (with combinators) are allowed in modern browsers.
- Specificity: the specificity of its most specific argument. The
:not()wrapper adds nothing. - Invalid arguments: unlike
:is()and:where(), the list is not forgiving. One invalid selector makes the whole rule invalid.
Values
| Syntax and arguments | Description |
|---|---|
:not(.a) |
Elements without the class a. |
:not(.a, .b) |
Elements that are neither .a nor .b. |
:not(.a .b) |
Elements that do not match the complex selector .a .b. |
:not(:first-child) |
Any pseudo-class can be negated. |
:not(:not(.a)) |
Double negation; works but is just a longer .a. |
Usage notes
- List means "none of".
:not(.a, .b)excludes both. To exclude only elements that have both classes, write:not(.a.b). - Chaining means "and".
:not(.a):not(.b)is equivalent for plain selectors, but the list form is shorter. Note the specificity differs: the list takes the highest argument, the chain adds both. - Specificity surprises.
p:not(#intro)has ID-level specificity (1, 0, 1) because#introis inside. This can make later rules hard to override. - Spacing rules. The classic
li:not(:last-child) { margin-bottom: 1rem; }avoids a trailing gap without extra markup. - Negation is broad.
:not(.x)also matches<html>,<body>and everything else without.x. Always combine it with a type or other selector. - Pseudo-elements cannot be used inside
:not().
Browser support
Single-selector :not() works everywhere. Selector lists and complex selectors inside :not() are Baseline since 2021 (Chrome 88, Firefox 84, Safari 9).
Related
:is()โ match any of a list:where()โ the zero-specificity version:has()โ match by contents- Lesson: Selector Functions
- Lesson: Specificity