Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

How Do You Use the :not() Selector in CSS?

CSS
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedNov 24, 2026
5 min read

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.

Table of Contents

  • Basic syntax of the :not() selector
  • Selector lists inside :not()
  • Specificity of :not()
  • Practical patterns
    • Style everything except the last or first item
    • Apply hover only to enabled controls
    • Style external links
    • Form fields that are not valid
  • Limits and pitfalls
  • Frequently Asked Questions
    • How do you exclude multiple classes with :not()?
    • Does :not() change specificity?
    • What is the difference between :not() and an attribute selector like [class="x"]?
    • Can :not() be nested?
  • Summary
  • Related Articles

Basic syntax of the :not() selector

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.

html
css

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:

css

The li:not(:last-child) pattern is a common way to add spacing between items without extra space after the last one.

Selector lists inside :not()

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.

css

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).

SelectorMatches 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)

Specificity of :not()

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.

SelectorSpecificity (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().

Practical patterns

Style everything except the last or first item

css

This adds a slash separator before every breadcrumb item except the first.

Apply hover only to enabled controls

css

Disabled buttons do not react to hover, and you avoid writing a second rule to cancel it.

Style external links

css

This marks absolute links that do not point to your own domain. Replace example.com with your real host.

Form fields that are not valid

css

Showing the error only after the user has typed something avoids red borders on an untouched form.

Limits and pitfalls

  1. :not() cannot contain a pseudo-element. p:not(::first-line) is invalid and the rule is dropped.
  2. Selector lists were not allowed in old browsers. If you support very old browsers, chain :not() instead of using a comma list. One invalid selector inside an old-style list invalidates the entire rule.
  3. :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).
  4. Negation can be slow to reason about. Avoid stacking many :not() calls, since a positive class on the elements is usually easier to read.
  5. :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.

Frequently Asked Questions

How do you exclude multiple classes with :not()?

Use a list, :not(.a, .b), or chain them, :not(.a):not(.b). Both mean the element must have neither class.

Does :not() change specificity?

: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.

What is the difference between :not() and an attribute selector like [class="x"]?

:not() negates any selector, while [class="x"] matches only an exact attribute value. :not([hidden]) matches any element that lacks the hidden attribute.

Can :not() be nested?

Yes, in modern browsers, though :not(:not(.a)) is just a roundabout .a. Avoid nesting unless it expresses a real condition.

Summary

: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.

Related Articles

  • What Are Pseudo-Elements in CSS? ::before and ::after Explained
  • What Do the :is() and :where() Selectors Do in CSS?
  • Learn CSS: Complete Study Roadmap from Beginner to Advanced
Share