The css not selector, :not(), matches every element that does not match the selector list inside the parentheses. For example, a:not(.button) selects every link except those with the class button. It is a negation pseudo-class that lets you write one general rule and carve out exceptions instead of writing a rule and then overriding it.
Modern browsers accept a full selector list inside :not(), so you can exclude several things at once. This guide shows the syntax, the specificity rules and the patterns that make it useful.
Put the selector you want to exclude inside the parentheses. The rule applies to any element that matches the part outside :not() and fails the part inside.
The first two links get the plain style, and the call-to-action link is left alone. No override is needed, so the rules do not fight each other.
:not() also accepts type selectors, attribute selectors, IDs and other pseudo-classes:
The li:not(:last-child) pattern is a common way to add spacing between items without extra space after the last one.
In the Selectors Level 4 specification, :not() takes a list. Writing :not(.a, .b) excludes elements that match .a or .b. This is supported in all current major browsers.
The logic differs from what many people assume. :not(.a, .b) means "not A and not B". Chaining :not(.a):not(.b) means the same thing. To express "not both A and B on the same element", use a compound selector inside: :not(.a.b).
| Selector | Matches an element that... |
|---|---|
:not(.a) | does not have class a |
:not(.a, .b) | has neither class a nor class b |
:not(.a):not(.b) | has neither class a nor class b |
:not(.a.b) | does not have both classes together |
:not(:not(.a)) | has class a (double negation) |
The :not() pseudo-class itself adds no specificity. Instead, the specificity of the selector inside it is what counts, and with a list it is the specificity of the most specific item in the list.
| Selector | Specificity (id, class, type) |
|---|---|
a:not(.cta) | (0,1,1) |
a:not(#main) | (1,0,1) |
a:not(.x, #y) | (1,0,1) |
:not(p) | (0,0,1) |
So an ID inside :not() raises the weight of the whole rule to an ID level, which can make later overrides painful. Prefer classes inside :not(). If you want an exclusion without any specificity cost, :where(:not(.cta)) resets it to zero, a technique covered with :is() and :where().
This adds a slash separator before every breadcrumb item except the first.
Disabled buttons do not react to hover, and you avoid writing a second rule to cancel it.
This marks absolute links that do not point to your own domain. Replace example.com with your real host.
Showing the error only after the user has typed something avoids red borders on an untouched form.
:not() cannot contain a pseudo-element. p:not(::first-line) is invalid and the rule is dropped.:not() instead of using a comma list. One invalid selector inside an old-style list invalidates the entire rule.:not(.a) matches elements that have no class at all, and also elements like html and body. Combine it with a type or class selector in front, such as li:not(.a), instead of writing a bare :not(.a).:not() calls, since a positive class on the elements is usually easier to read.:not(:hover) is not the opposite of an action. It matches elements the pointer is not over, so putting it on a base style can create confusing hover effects. Style the base state normally and the :hover state separately.Use a list, :not(.a, .b), or chain them, :not(.a):not(.b). Both mean the element must have neither class.
:not() itself adds nothing, but the selector inside it does. :not(.cta) adds the weight of one class, and :not(#id) adds the weight of one ID.
:not() negates any selector, while [class="x"] matches only an exact attribute value. :not([hidden]) matches any element that lacks the hidden attribute.
Yes, in modern browsers, though :not(:not(.a)) is just a roundabout .a. Avoid nesting unless it expresses a real condition.
:not() selects elements that do not match a selector, and with selector lists you can exclude several cases in one step. It adds the specificity of its argument, not of itself, it cannot contain pseudo-elements, and it works best for small, readable exceptions such as li:not(:last-child) or button:not(:disabled). When the exclusion logic grows complex, use a class on the element instead.