CSS: negation selector (:not)
tag:not(expr)-
Match tag if it does not match selector expr.
expr is any compound selector or complex selector or selector list.
tag here is compound selector, or none, which means the universal selector (* any tag).
/* match p, only if it is not class x nor class y */ p:not(.x, .y) { border: solid 1px red; } /* match any descendant of p, only if it is not class x nor class y */ p :not(.x, .y) { border: solid 1px blue; } The specificity of
tag:not(expr)is expr.
Examples
/* any element inside body, but is not .y nor .y */ body :not(.x):not(.y) { border: solid 2px orange; }
/* inside h2, and is not span.x */ h2 :not(span.x) { border: solid 2px green; }
note, :not() is part of
pseudo-class functions.
CSS Selector. pseudo-class functions
CSS. Selectors
Selector types
- CSS: types of selectors
- CSS: simple selectors
- CSS: compound selectors
- CSS: complex selector (combinator)
Simple selectors
- CSS: type selector (tag name)
- CSS: universal selector (* any tag)
- CSS: class selector (.x)
- CSS: ID selector (#)
- CSS: attribute selector ([x])
Combinators
- CSS: descendant selector (space)
- CSS: child selector (>)
- CSS: adjacent sibling selector (+)
- CSS: subsequent sibling selector (~)
Selector list
Special selector
- CSS: :root selector
- CSS: no child selector
- CSS: first child, sibling rank selector
- CSS: nth-child selector
- CSS: pseudo-class selector (:)
- CSS: pseudo-element selector (::)
- CSS: negation selector (:not)
- CSS: “is any of” selector (:is, :where)
- CSS: :has descendant selector