:is(), :where() and :has()

Intermediate · 14 min

How :is() and :where() shorten selector lists and differ in specificity, and how :has() lets a selector look at children and later siblings to style a parent.

Before thisCSS nestingPseudo-classes and Pseudo-elements

Kevin Powell · 14 min

:is(), :where() and :has()

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Kevin PowellReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • `:is()` and `:where()` take a list of selectors and match an element that matches any of them. They turn `main h1, main h2, main h3` into `main :is(h1, h2, h3)`.
  • The only difference between them is specificity. `:is()` counts as its most specific argument. `:where()` always counts as zero, which makes it ideal for defaults that anything can override.
  • `:has()` is a relational selector. `a:has(img)` matches an `<a>` that contains an image, so a selector can finally depend on what is inside or after an element.
  • Common `:has()` patterns: `label:has(+ input:required)`, `form:has(:invalid)`, `.card:has(> img)`, and `body:has(dialog[open])` for locking page scroll.
  • A selector list normally dies if one selector is invalid. Inside `:is()` and `:where()` an invalid item is simply ignored, while an invalid selector in `:has()` makes the whole `:has()` match nothing.

Three small selectors that remove big workarounds

Long selector lists are boring to write and easy to get wrong:

header a:hover, nav a:hover, footer a:hover,
header a:focus-visible, nav a:focus-visible, footer a:focus-visible { ... }

And two problems had no CSS answer at all: “style a parent based on its child” and “give this reset rule no weight, so every real rule beats it”. These three pseudo-classes address them.

:is(): group and match

:is(header, nav, footer) a:is(:hover, :focus-visible) {
  text-decoration: underline;
}

That is the same as the six-selector list above. :is() matches an element that matches any item in its list, so it can go anywhere in a compound selector. Two properties are worth remembering:

  1. Specificity is the highest item. :is(#id, .class) is (1,0,0), even if the element only matched .class. This is also how the & in nested rules behaves.
  2. The list is forgiving. If one item is an invalid or unsupported selector, the others still work. In an ordinary comma list, a single bad selector invalidates the whole rule. I confirmed that in Chrome: .i1, :nonexistent { color: red } was ignored in full, so the element stayed blue.

:is() cannot match pseudo-elements, so :is(::before) does nothing.

:where(): the same, with zero weight

:where() matches exactly like :is() but its specificity is always (0,0,0). That makes it the right tool for defaults you want others to override without a fight:

:where(h1, h2, h3, h4) { margin-block: 1em 0.5em; }
:where(ul, ol)         { padding-inline-start: 1.5rem; }

Any class selector beats these, in any order. A reset or base layer written with :where() rarely produces the specificity wars described in specificity and the cascade. Cascade layers give the same control with a different mechanism, and the two combine well.

I tested in Chrome with .b { color: blue } followed by a later rule for the same element: a :where(.b) rule lost (the element stayed blue), while an :is(.b) rule won on source order.

:has(): look inside, look ahead

CSS selectors normally only look up and back: a descendant, a child, a previous sibling. :has() looks the other way. .card:has(img) matches a .card that contains an <img>.

/* Cards with an image get a two-column layout */
.card:has(> img) { display: grid; grid-template-columns: 8rem 1fr; }

/* A field wrapper reacts to its input's state */
.field:has(input:invalid) { border-color: crimson; }

/* A label that comes before a required input */
label:has(+ input:required)::after { content: " *"; }

/* Lock page scroll while a modal dialog is open */
body:has(dialog[open]) { overflow: hidden; }

/* Grid with more than 4 children */
.grid:has(> :nth-child(5)) { grid-template-columns: repeat(3, 1fr); }

The relative selector inside starts with a combinator, spelled or implied: :has(> img) is a direct child, :has(+ p) is an immediately following sibling, :has(img) is any descendant.

Things to be careful of:

  • Not forgiving. Unlike :is(), an invalid item inside :has() makes the whole thing match nothing. For a selector that may be unsupported, test with @supports selector(:has(a)).
  • No nesting :has() inside :has().
  • Performance. Matching requires looking at descendants or siblings, and changes there can force the browser to re-check the parent. It is well optimised, but keep the subject specific (.card:has(...), not *:has(...)) and prefer > and + to open-ended descendants.
  • Do not hide behaviour in CSS only. Styling a form based on :invalid is great for feedback, but the logic still has to exist in the HTML validation attributes.

Browser support

Per MDN’s compatibility data, :is() and :where() have been available since 2020 (Chrome 88, Firefox 78, Safari 14), and :has() since Chrome 105, Safari 15.4 and Firefox 121 (December 2023), which is why it is safe in most projects now. It is worth a quick look at your own audience before depending on it.

What the video covers

Kevin Powell’s “The new CSS pseudo-classes explained - :is() :where() :has()” (January 2023, about 14 minutes) has chapters on :is() (0:37), :where() (6:53) and :has() (11:00). That matches this lesson’s structure. It was recorded when :has() had around 83% support and :is() and :where() about 92%, as its description says, so the support figures are out of date: :has() has since reached every major browser. The :has() section is a short one in a 14-minute video, so his later, dedicated :has() video in further viewing is the better follow-up. I could not read a transcript.

Where this fits

These selectors extend what you saw in pseudo-classes and pseudo-elements and interact with specificity from the cascade lesson. Next, @property gives custom properties a type.

Check yourself

  1. 01You write `.btn { color: blue }` and then, later in the file, `:where(.btn) { color: red }`. What colour is the button? What if the second rule used `:is(.btn)`?

    Blue with `:where()`: it contributes no specificity, so `.btn` at (0,1,0) beats (0,0,0) even though it comes first. With `:is(.btn)` both rules are (0,1,0), so source order decides and the later red rule wins.

  2. 02What is the specificity of `:is(#id, .class)`, and does it matter which one the element matched?

    (1,0,0), the specificity of the most specific argument, regardless of which one matched. An element matched only through `.class` still gets the ID-level weight, which can make rules unexpectedly hard to override.

  3. 03How would you style a `.field` wrapper differently when the input inside it is invalid, without JavaScript?

    `.field:has(input:invalid) { border-color: crimson; }`. `:has()` makes the selector depend on a descendant's state. To style a label before its input, `label:has(+ input:invalid)` uses the next-sibling combinator inside `:has()`.

  4. 04Why is `:has()` a performance topic?

    The browser has to check an element's descendants or following siblings to decide whether it matches, and changes anywhere in that subtree may require re-evaluating the match. Browser vendors optimised it heavily before shipping, but selectors like `body:has(.x) *` or `:has()` anchored on very general selectors can still invalidate a lot of the tree. Anchor it on a specific element and use direct-child or sibling combinators where possible.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark