MDN will be in maintenance mode on Thursday September 21st, starting at 10 AM Pacific / 5 PM UTC, for about 1 hour.

The negation CSS pseudo-class, :not(X), has a functional notation taking a simple selector X as an argument. It matches elements that are not represented by the argument. X must not contain another negation selector. X may also not target a pseudo element.

/* Selects any element that is NOT a paragraph */
body :not(p) {
  color: blue;
}
Notes:
  • Useless selectors can be written using this pseudo-class. For example, :not(*) matches any element which is not any element, so the rule will never be applied.
  • It is possible to rewrite other rules. E.g., .foo:not(.bar) will match the same element as the simpler .foo. Nevertheless the specificity of the first one is higher.
  • :not(.foo) will match anything that isn't .foo, including <html> and <body>.
  • This selector only applies to one element; you cannot use it to exclude all ancestors. For instance, body :not(table) a will still apply to links inside of a table, since <tr> will match with the :not() part of the selector.

Syntax

:not( <selector># )

Example

HTML

<p>This is a boring paragraph.</p>
<p class="shiny">I am so very shiny!</p>
<div>It ain't easy being green.</div>

CSS

.shiny { text-shadow: 1px 1px 2px gold; }

/* Selects all <p> elements that are not in the class .shiny */
p:not(.shiny) { color: red; }

/* Selects all elements that are not <p> */ 
:not(p) { color: green; }

Result

Specifications

Specification Status Comment
Selectors Level 4
The definition of ':not()' in that specification.
Working Draft Extends its argument to allow some non-simple selectors.
Selectors Level 3
The definition of ':not()' in that specification.
Recommendation Initial definition.

Browser compatibility

Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari
Basic support 1.0 (Yes) 1.0 (1.7 or earlier) 9.0 9.5 3.2
Extended arguments No support No support No support No support No support 9.0
Feature Android Edge Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support 2.1 (Yes) 1.0 (1) 9.0 10.0 3.2
Extended arguments No support No support No support No support No support 9.0