CSS :not()
Pseudo-Class
The :not()
pseudo-class allows you to exclude elements that match a specific selector. It’s useful when you want to style everything except certain elements.
1. Syntax
✅ The :not()
function takes one simple selector (not a full selector chain).
2. Example – Exclude a Specific Element
✅ All <li>
elements EXCEPT .exclude
turn green.
3. Excluding Multiple Elements
✅ All <p>
elements except those with .important
or .highlight
turn gray.
4. Combining :not()
with Other Selectors
Exclude the Last Child
✅ Adds a bottom border to all <li>
except the last one.
Exclude Disabled Inputs
✅ Only enabled inputs get a light yellow background.
Target Everything Except div
✅ Every element except <div>
gets margin: 10px
.
5. :not()
with Multiple Conditions
✅ Styles all <p>
elements except:
✔ The first child
✔ Elements with .special
class
6. :not()
vs. :has()
(Upcoming Feature)
-
:not()
excludes elements matching a condition. -
:has()
(not widely supported yet) selects elements that contain certain children.
7. Browser Support
✅ Fully supported in Chrome, Edge, Firefox, Safari, and Opera.
8. Best Practices
✔ Use :not()
for exclusion instead of writing complex selectors.
✔ Combine it with pseudo-classes (:last-child
, :nth-child()
, etc.).
✔ Avoid overly complex :not()
rules to maintain readability.