Course contentsShow
← Web Development
Lesson 446 of 2,9828. Advanced CSS Selectors and FeaturesFree lesson

:not() with Complex Selectors

Use :not() with selector lists and compound selectors to exclude multiple conditions at once.

:not() with Complex Selectors

You'll learn how to exclude multiple elements at once by combining :not() with complex selector patterns.


You've already seen how :not() can exclude a single simple selector, but its real power emerges when you pass in selector lists or use it with compound selectors. This lets you write one rule that says "apply this style to everything except these multiple conditions."

Using :not() with Selector Lists

You can pass multiple selectors separated by commas inside :not() to exclude anything that matches any of those conditions:

/* Style all buttons EXCEPT .primary and .danger */
button:not(.primary, .danger) {
  background-color: gray;
}

This is like saying "apply styles to all buttons, but skip any button that is either .primary or .danger."

Using :not() with Compound Selectors

You can also combine :not() with compound selectors (multiple conditions joined together):

/* Target divs that are NOT both .card AND .featured */
div:not(.card.featured) {
  opacity: 0.7;
}

This only excludes divs that have both classes simultaneously.

Practical Example

/* Style all links except external links with icons */
a:not([href^="http"], .icon-link) {
  text-decoration: underline;
}

/* Target inputs that are NOT disabled checkboxes */
input:not([type="checkbox"][disabled]) {
  border: 1px solid blue;
}

Think of complex :not() selectors like a bouncer with a detailed exclusion list: "Everyone gets in except people wearing these specific combinations of things."

Key Takeaway: :not() accepts selector lists (comma-separated) to exclude multiple conditions and works with compound selectors for precise exclusion patterns, keeping your CSS concise and expressive.