:not()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
The :not()
CSS pseudo-class represents elements that do not match a list of selectors. Since it prevents specific items from being selected, it is known as the negation pseudo-class.
Try it
p:not(.irrelevant) {
font-weight: bold;
}
p > strong,
p > b.important {
color: crimson;
}
p > :not(strong, b.important) {
color: darkmagenta;
}
Mars is one of the most Earth-like planets. Mars day is almost
the same as an Earth day, only 37 minutes longer.
NASA's Jet Momentum Propulsion Laboratory
is designing mission concepts to survive the Venus extreme temperatures
and atmospheric pressure.
The :not()
pseudo-class has a number of quirks, tricks, and unexpected results that you should be aware of before using it.
Syntax
The :not()
pseudo-class requires a selector list, a comma-separated list of one or more selectors, as its argument. The list must not contain a pseudo-element, but any other simple, compound, and complex selectors are allowed.
:not() {
/* ... */
}
Description
There are several unusual effects and outcomes when using :not()
that you should keep in mind when using it:
- Useless selectors can be written using this pseudo-class. For example,
:not(*)
matches any element which is not an element, which is obviously nonsense, so the accompanying rule will never be applied. - This pseudo-class can increase the specificity of a rule. For example,
#foo:not(#bar)
will match the same element as the simpler#foo
, but has the higher specificity of twoid
selectors. - The specificity of the
:not()
pseudo-class is replaced by the specificity of the most specific selector in its comma-separated argument of selectors; providing the same specificity as if it had been written:not(:is(argument))
. :not(.foo)
will match anything that isn't.foo
, includingand
.
- This selector will match everything that is "not an X". This may be surprising when used with descendant combinators, since there are multiple paths to select a target element. For instance,
body :not(table) a
will still apply to links inside a