Sub topic 9
Grouping and filtering with :is(), :where(), :not(), and :has()
This chapter assumes you can write type, class, ID, and attribute selectors, and that you know the descendant (space) and child (>) combinators. It also assumes you know a selector's specificity decides which rule wins when several match. The chapter on specificity covers that in full. Here you only need the idea that an ID beats a class and a class beats a type.
The four selectors here are logical pseudo-classes. They don't match a state like :hover or a position like :first-child. They take a selector list as an argument and match based on that list.
What each one does
| Selector | Matches an element that... | Specificity it adds |
|---|---|---|
| `:is(A, B)` | matches A or B | the most specific item in the list |
| `:where(A, B)` | matches A or B | always zero |
| `:not(A, B)` | matches neither A nor B | the most specific item in the list |
| `:has(A)` | contains, or is followed by, something matching A | the most specific item in the list |
The next sections take them one at a time.
:is(): shorten repeated selectors
Without :is(), targeting headings inside two kinds of container takes four selectors:
article h1,
article h2,
section h1,
section h2 {
line-height: 1.2;
}:is() folds the repetition into one rule:
:is(article, section) :is(h1, h2) {
line-height: 1.2;
}Both versions match the same elements. The :is() form grows linearly as you add options. The expanded form grows multiplicatively.
:is() and :where() are forgiving selector lists. If one entry in the list is invalid or unsupported by the browser, the browser ignores that entry and still uses the rest. A normal comma-separated list works differently. One bad selector there makes the whole rule invalid.
/* The unknown pseudo-class is dropped; h1 and h2 still match. */
:is(h1, h2, :not-a-real-pseudo) {
margin-block: 0 0.5em;
}Two limits apply. You can't put a pseudo-element inside :is() or :where(), so :is(p::first-line) never matches. Put the pseudo-element after the group instead: :is(p, li)::first-line.
:where(): the same grouping with zero specificity
:where() matches exactly the same elements as :is(). The only difference is that it contributes nothing to specificity. That makes it good for defaults that other rules can override without a fight.
:where(h1, h2, h3) {
margin-block: 0 0.5em;
}
h2 {
margin-block: 0 1em; /* wins: the :where() rule has zero specificity */
}Compare the two groupings below. In each pair, the first rule uses a class inside the group:
:is(.prose) a {
color: #0645ad; /* specificity: one class + one type */
}
:where(.prose) a {
color: #0645ad; /* specificity: one type only */
}A later rule such as a.external { color: #070; } beats the :where() version but loses to the :is() version. That is the main reason to choose one over the other. Reset stylesheets and component library defaults often use :where() for this reason.
:not(): exclude matches
:not() matches elements that do not match the argument. It is useful when a rule applies to almost everything in a set.
.nav a:not(.active) {
opacity: 0.7;
}You can pass a list. The element must match none of the entries:
li:not(.done, .skipped) {
color: #222;
}That is equivalent to chaining: li:not(.done):not(.skipped). Do not confuse it with li:not(.done .skipped), which excludes only an element with class skipped that sits inside an element with class done. Complex selectors inside :not() are valid in current browsers, but older ones accept only simple selectors.
Unlike :is(), :not() is not forgiving. One invalid entry makes the whole rule invalid, so it is dropped.
Two behaviors surprise people:
:not()matches anything that isn't the argument, including elements you didn't think about.:not(.active)matches<body>,<html>, and every other element unless you restrict it with a type or an ancestor, as in.nav a:not(.active).- Specificity comes from the argument, not from
:notitself.a:not(#home)has ID-level specificity because of#home, even though it matches every link except one.
:has(): select by what an element contains
:has() is the relational pseudo-class, often called the parent selector. It matches an element if the selector list inside it matches something relative to that element. By default, "relative" means a descendant.
.card:has(img) {
display: grid;
grid-template-columns: 8rem 1fr;
}Here only cards that contain an image get the two-column layout. Cards without an image keep the default layout, and you don't need to add a class in the HTML.
You can start the argument with a combinator to change the relationship:
/* A heading immediately followed by a paragraph: tighten the heading's bottom margin */
h2:has(+ p) {
margin-block-end: 0.25em;
}
/* A list that has a direct child li marked as current */
ul:has(> li[aria-current="page"]) {
border-inline-start: 3px solid #1a73e8;
}State can also be the condition. This highlights a whole form row when its input is invalid, and it shows :has() combining with :not():
.form-row:has(input:required:invalid:not(:placeholder-shown)) {
background: #fff3f3;
}
.form-row:not(:has(input)) {
margin-block-end: 1rem; /* rows with no input at all */
}Limits of :has():
- You can't nest
:has()inside another:has(). - Pseudo-elements are not allowed inside its argument.
- Like
:not(), it is not forgiving. One invalid entry invalidates the rule. - Browser support arrived late. All current major browsers support it, but older versions don't. Wrap it in
@supports selector(:has(a))when you need a safe fallback. - A broad
:has()such asbody:has(.modal)makes the browser recheck the document whenever the DOM changes. Prefer anchoring it to a narrow element.
Combining all four
The four selectors nest and compose well. This example styles every link inside article or section content, excludes links in the navigation and links that already have a class, and adds an icon only to links that contain an image:
:where(article, section) a:not(nav a, [class]) {
color: #0645ad;
}
:where(article, section) a:has(> img) {
display: inline-block;
text-decoration: none;
}The :where() wrapper keeps these defaults at the weakest specificity. Any component class can override them without raising its own specificity.
Common mistakes
- Expecting
:is()to have zero specificity. It takes the strongest entry in the list, so:is(#a, .b)counts as an ID even when the element matched.b. - Using
:not(.a, .b)when you meant "not both". The list means "neither one." - Writing
:has()without a type or class on the left, as in:has(img). That matches every ancestor of an image, includinghtmlandbody. - Putting a pseudo-element inside the argument list. Place it after the whole selector.
- Relying on
:is()forgiveness to hide typos. An invalid entry is silently dropped, so you get no error.
Exercise
A stylesheet has :where(.card) h2 { font-size: 1.5rem; } and, earlier in the file, .card h2 { font-size: 2rem; }. Which font size does an h2 inside a .card get, and what would change if :where were replaced with :is?
Show answer
The h2 gets 2rem. :where(.card) h2 has specificity of one type selector (0,0,1), while .card h2 has one class and one type (0,1,1), so the earlier rule wins even though it comes first. With :is(.card) h2, both rules have specificity (0,1,1), so the later rule wins and the h2 gets 1.5rem.
Exercise
Write one selector that matches every li inside a ul.menu that does not have the class disabled and whose list contains at least one li with the class current.
Show answer
ul.menu:has(li.current) li:not(.disabled)