Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 139 ยท Selectors

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

Learn the CSS selector functions :is(), :where(), :not() and :has(), how their specificity differs, forgiving selector lists, and practical parent-selector patterns.

As selectors grow, they tend to repeat themselves. Styling headings inside articles, sections and asides can mean writing article h2, section h2, aside h2, article h3, section h3, aside h3. And for most of CSS history, some questions were impossible to ask at all, such as "style this card if it contains an image".

Four functional pseudo-classes fix both problems. :is() and :where() shorten repetitive lists. :not() excludes elements. :has() finally lets you select an element based on what is inside it or after it. They look similar, but they differ in one important detail, specificity, and this lesson shows when that matters.

:is(): match any of a list

:is() takes a selector list and matches any element that matches at least one item in it. Its main job is to factor out repetition:

CSS
/* Before */
article h2, section h2, aside h2 { โ€ฆ }

/* After */
:is(article, section, aside) h2 { โ€ฆ }

It can go anywhere in a selector, and you can use more than one:

CSS
:is(article, section) :is(h2, h3) { โ€ฆ }   /* 2 ร— 2 = 4 combinations */
.card :is(a, button):hover { โ€ฆ }
Shortening selectors with :is()

Specificity of :is()

:is() takes the specificity of its most specific argument, regardless of which one actually matched. That has a surprising consequence:

CSS
:is(#sidebar, .content) p { color: navy; }   /* (1, 0, 1) */

A paragraph inside .content gets this rule with the weight of an ID, because #sidebar is in the list. Mixing IDs and classes inside :is() quietly raises the weight of the whole rule.

:where(): the zero-specificity twin

:where() matches exactly like :is(), but its specificity is always zero, whatever is inside.

CSS
:where(article, section, aside) h2 { โ€ฆ }   /* (0, 0, 1): just the h2 */

That makes :where() ideal for defaults: base styles, resets and library styles that any later rule should be able to override with a single class.

:is() vs :where() in the cascade

Both rules match the links, but only :where() lets the later .muted class take over. Use :is() when you want normal specificity, and :where() when you want styles that are easy to override.

Forgiving selector lists

Remember from Selector Basics that one invalid selector in a plain list drops the whole rule. :is() and :where() are different: they take a forgiving selector list. Invalid or unsupported items are simply ignored, and the rest still work.

CSS
/* If a browser doesn't know ::-moz-range-thumb, the whole rule dies */
input::-webkit-slider-thumb, input::-moz-range-thumb { โ€ฆ }

/* Here, an unknown pseudo-class is skipped and the rest still match */
:is(:focus-visible, :some-future-state) { โ€ฆ }

Forgiveness only covers selectors that can legally appear inside :is(). Pseudo-elements such as ::before aren't allowed in :is() or :where() at all, so the vendor-prefixed slider example above can't be rescued this way; you still need separate rules.

:not(): exclude matches

:not() matches elements that do not match any selector in its argument. Since 2021, all major browsers accept a full list and complex selectors inside it:

CSS
a:not([href^="https://"]) { โ€ฆ }       /* links that aren't external */
.btn:not(:disabled):hover { โ€ฆ }       /* hover only when enabled */
li:not(:last-child) { โ€ฆ }             /* every item except the last */
input:not([type="checkbox"], [type="radio"]) { โ€ฆ }

A list inside :not() means "matches none of these". :not(.a, .b) excludes elements that have either class.

Like :is(), :not() takes the specificity of its most specific argument: :not(#main) weighs as much as an ID. And unlike :is(), its list is not forgiving; one invalid selector inside invalidates the rule.

Excluding elements with :not()

:not() is evaluated per element, which can surprise you with descendants. :not(.dark) p doesn't mean "paragraphs outside .dark". It means "a paragraph with some ancestor that isn't .dark", and <body> and <html> almost always qualify. To exclude a subtree, write p:not(.dark p).

:has(): the relational selector

:has() matches an element if the relative selector inside it matches something relative to that element. By default it looks at descendants:

CSS
.card:has(img) { โ€ฆ }          /* a card that contains an image */
.card:has(> img) { โ€ฆ }        /* a card with an image as a direct child */
h2:has(+ p) { โ€ฆ }             /* an h2 immediately followed by a p */

This is why :has() is often called the parent selector: you style the container based on its contents, or an element based on the sibling after it. It has been supported in every major browser since late 2023.

Cards that adapt to their content

Practical :has() patterns

  • Form groups that react to their field: .field:has(input:user-invalid) label { color: #b91c1c; }
  • Layouts that adapt to content: .grid:has(> :nth-child(5)) switches layout once there are five or more items.
  • Look backward: li:has(+ li.active) styles the item before the active one, something no combinator can do.
  • Page-level state: body:has(dialog[open]) { overflow: hidden; } stops the page scrolling while a dialog is open.
  • Quantity queries: ul:has(> li:nth-child(4):last-child) matches a list with exactly four items.
Styling a form group from its field

Type an invalid email or an age of 12, then move to the other field. The whole group reacts, with no JavaScript.

:has() follows the same specificity rule as :is() and :not(): its most specific argument counts. Two restrictions are worth knowing: :has() can't be nested inside another :has(), and pseudo-elements aren't allowed inside it.

Choosing between them

Function Matches Specificity Forgiving list?
:is(A, B) A or B most specific argument yes
:where(A, B) A or B always zero yes
:not(A, B) neither A nor B most specific argument no
:has(A) element with A relative to it most specific argument no

A good rule of thumb: reach for :where() in base styles and shared defaults, :is() to shorten selectors in component styles, :not() for exceptions, and :has() whenever an element's style depends on its contents or its neighbors.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the specificity of :where(#nav, .menu) a?

    :where() always contributes zero, so only the a type selector counts.
  2. 2. Which selector styles a .card that contains a <video> anywhere inside it?

    .card video styles the video itself. .card:has(video) styles the card, based on its contents.
  3. 3. What happens to :is(.ok, :unknown-thing) { color: green; } in a browser that doesn't know :unknown-thing?

    :is() takes a forgiving selector list, so the unknown item is dropped and .ok keeps working.
  4. 4. How do you select every list item except the last one?

    :not(:last-child) excludes the last child and matches all the others.
  5. 5. Which selector styles a list item that comes immediately BEFORE an item with class active?

    Combinators only look forward. :has(+ li.active) checks the next sibling and styles the item before it.

Summary

  • :is() matches any selector in its list and takes the specificity of its most specific argument.
  • :where() matches the same way but always has zero specificity, which is perfect for defaults.
  • :is() and :where() use forgiving lists: invalid items are skipped instead of killing the rule.
  • :not() excludes elements matching any item in its list; watch out for :not() with descendants.
  • :has() selects an element by what is inside it or after it: the long-awaited parent selector.
  • :not() and :has() also take their most specific argument's weight.

You now know every major kind of selector. Next: How the Cascade Works โ€” what the browser does when several rules target the same element.