Logical pseudo-classes
Definition and purpose
:is(), :where() and :not() let a selector express alternatives or exclusions. They are useful for grouping patterns without repeating a long selector prefix. Matching and specificity are separate concerns.
Strongest argument wins
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } .item:is(p, #unused) { color: navy; } .item.muted { color: green; } </style> </head> <body> <p id="a" class="item">Regular</p> <p id="b" class="item muted">Muted</p> <h2 id="c" class="item">Heading</h2> </body></html>The unused ID raises specificity for both paragraphs.
Syntax and mental model
:is() and :not() contribute the specificity of their most specific argument, even if that argument did not match the element. :where() and its arguments contribute zero. Other parts of the selector still contribute normally. For example, .item:is(p, #unused) has specificity (1,1,0).
Low-weight default
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } .item { color: green; } :where(p, h2) { color: navy; } </style> </head> <body> <p id="a" class="item">Regular</p> <p id="b" class="item muted">Muted</p> <h2 id="c" class="item">Heading</h2> </body></html>The class beats the later zero-weight rule.
Common mistakes
Do not assume :is() behaves like a comma list for specificity. Use :where() for intentionally easy-to-override defaults; :not(.a, .b) means neither class, not merely one of them missing.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Pseudo-elements and generated boxes, Importance and inline styles. Next lesson: Relational selectors.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Logical pseudo-classes — knowledge check
Five questions on logical pseudo-classes.
What is the specificity contribution of :where(#x)?
What is the specificity of :is(p, #x)?
What matches :not(.a, .b)?
Which function best fits low-weight defaults?
Does the strongest :is() argument need to match?