Sub topic 9
Grouping and filtering with :is(), :where(), :not(), and :has()
This chapter assumes you know CSS rule syntax, type, class, ID and attribute selectors, and the descendant and child combinators. These four pseudo-classes are called logical (or functional) pseudo-classes. Each takes a selector list in parentheses and matches elements based on that list.
:is() shortens repeated lists
Without :is(), nested selectors multiply quickly. article h1, article h2, section h1, section h2 is four selectors for one idea. :is() takes a list and matches any element that fits one of the entries.
:is(article, section) :is(h1, h2) {
line-height: 1.2;
}This is equivalent to the four-selector version. A useful feature is forgiving parsing. If one entry in the list is invalid or unsupported, the browser ignores that entry and keeps the others. A normal selector list is dropped entirely if one part is invalid.
:where() is :is() with zero specificity
:where() matches the same elements as :is(). The difference is specificity. Anything inside :where() adds nothing to the selector's specificity, so rules built with it are easy to override later.
:where(h1, h2, h3) {
margin-block: 0 0.5em;
}This is useful for base or reset styles. Any later rule, even a plain h2 { ... }, wins over it.
:not() excludes matches
:not() matches elements that do not match the selector list inside it.
.nav a:not(.active) {
opacity: 0.7;
}Every link in .nav gets reduced opacity except the one with the class active. You can list several exclusions: a:not(.active, .disabled).
:has() selects by what an element contains
:has() is often called the parent selector. It matches an element if the selector inside it matches something relative to that element, usually a descendant.
.card:has(img) {
display: grid;
grid-template-columns: 8rem 1fr;
}Cards that contain an image get a two-column layout. Cards without one keep the default layout. Browser support for :has() is newer than for the other three, so check your target browsers.
Specificity at a glance
The specificity of :is(), :not() and :has() equals the most specific selector in their list. :where() always contributes zero.
| Selector | Specificity (IDs, classes, types) |
|---|---|
| `:is(.a, #b) p` | 1, 0, 1 (the ID is the highest entry) |
| `:where(.a, #b) p` | 0, 0, 1 |
| `a:not(.active)` | 0, 1, 1 |
| `.card:has(img)` | 0, 1, 1 |
Common errors
- Using
:is()in a reset and then being unable to override it. The most specific entry in the list sets its weight. Use:where()for defaults. - Expecting
:not(.a .b)to work in older browsers. Complex selectors inside:not()are only supported in newer ones, so prefer simple selectors there. - Nesting
:has()inside:has(). This is not allowed. - Treating
:not(.a):not(.b)and:not(.a, .b)differently. Both exclude elements with either class.
Exercise
A reset uses :is(h1, h2, #title) { margin: 0; }. Later, h2 { margin: 1rem; } has no effect on an h2. Why, and how do you fix the reset?
Show answer
The #title entry gives the whole :is() selector ID-level specificity, so it beats h2. Even for a plain h2, the rule is weighted by its most specific entry. Fix it by using :where(h1, h2, #title), which has zero specificity, so the later h2 rule wins.