Reading history

Modern CSS Selectors: :has(), :is(), :where() and Nesting

6 min read 0 views

Style a parent by its contents with :has(), group selectors with :is() and zero-specificity :where(), and write native CSS nesting with the & selector.

CSS selectors have gained powerful new abilities in recent years. :has() finally lets you style a parent based on what it contains, something developers wanted for twenty years. :is() and :where() shorten long selector lists, and native nesting lets you write related rules inside each other, as in Sass, without any tools. All four work in current browsers.

In this lesson you will learn

  • Styling a parent or previous sibling with :has()
  • Reacting to form states such as :checked and :invalid
  • Grouping selectors with :is()
  • Zero-specificity grouping with :where()
  • Writing nested CSS with &

You should know pseudo-classes and specificity.

The :has() Selector

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>:has()</title>
  <style>
    body { font-family: sans-serif; margin: 12px; font-size: 14px; }
    .card { border: 2px solid #ddd; border-radius: 6px; padding: 8px; margin-bottom: 8px; }
    .card:has(img) { border-color: #42a5f5; }                 /* a card that contains an image */
    .card:has(.badge) { background: #fff8e1; }                /* a card that contains a badge */
    label:has(+ input:invalid) { color: #c62828; }            /* label followed by an invalid input */
    .task:has(input:checked) span { text-decoration: line-through; color: #9e9e9e; }
    img { width: 40px; height: 40px; vertical-align: middle; }
    .badge { background: #ffb300; padding: 1px 6px; border-radius: 10px; font-size: 12px; }
  </style>
</head>
<body>
  <div class="card">Plain card</div>
  <div class="card"><img alt="" src="hills.jpg"> Card with an image</div>
  <div class="card">Card with a <span class="badge">new</span> badge</div>
  <label for="age">Age (must be 5 or more)</label>
  <input id="age" type="number" min="5" value="3">
  <p class="task"><label><input type="checkbox" checked> <span>Finish homework</span></label></p>
  <p class="task"><label><input type="checkbox"> <span>Revise chemistry</span></label></p>
</body>
</html>

Result

  • .card:has(img) selects a card that contains an image. Before :has(), CSS could only look downwards from parent to child, so this needed JavaScript.
  • .card:has(.badge) works with any selector inside, so the card with a badge gets a yellow background.
  • label:has(+ input:invalid) selects a label whose next sibling is an invalid input. The age 3 is below the minimum of 5, so the label turns red without any script.
  • .task:has(input:checked) span crosses out a task when its checkbox is ticked. Click the second checkbox to see it.
  • :has() has the specificity of its strongest argument, just like :is().

:is(), :where() and Nesting

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>:is(), :where() and nesting</title>
  <style>
    body { font-family: sans-serif; margin: 12px; font-size: 14px; }
    /* :is() groups selectors; it takes the specificity of its strongest item */
    :is(h3, h4):hover { color: #1565c0; }
    /* :where() groups too, but adds zero specificity, so it is easy to override */
    :where(.menu) a { color: #555; }
    a.special { color: #d81b60; }
    /* Native CSS nesting */
    .menu {
      display: flex; gap: 12px; padding: 6px; background: #eceff1;
      & a { text-decoration: none; }
      & a:hover { text-decoration: underline; }
      &.dark { background: #263238; & a { color: #fff; } }
    }
  </style>
</head>
<body>
  <h3>Hover this heading</h3>
  <h4>Or this one</h4>
  <nav class="menu"><a href="#">Home</a><a href="#" class="special">Offers</a><a href="#">Contact</a></nav>
  <nav class="menu dark"><a href="#">Home</a><a href="#">About</a></nav>
</body>
</html>

Result

  • :is(h3, h4):hover is shorthand for h3:hover, h4:hover. With long lists such as :is(header, main, footer) :is(h1, h2, h3) it saves many lines.
  • One bad selector in a normal comma list invalidates the whole rule. Inside :is() and :where(), invalid items are simply ignored.
  • :where() works like :is() but always has zero specificity. That is why the plain a.special rule easily overrides the colour set through :where(.menu) a. It is ideal for default styles meant to be overridden.
  • Inside .menu { … }, a nested rule starting with & refers to the parent selector. & a becomes .menu a, and &.dark, with no space, becomes .menu.dark.
  • Rules can nest several levels, as &.dark { & a { … } } shows. Keep nesting shallow, though: deep nesting produces long, high-specificity selectors.

Quick Reference

SelectorMatchesSpecificity
a:has(> img)Links that directly contain an imageStrongest argument (here 0,0,1) plus a
h2:has(+ p)h2 immediately followed by a paragraphAs above
:is(.a, #b) pp inside .a or inside #bStrongest argument: (1,0,0) plus p
:where(.a, #b) pSame elementsOnly p: (0,0,1)
.x { &:hover {} }.x:hover(0,2,0)

Common Mistakes

  1. Using :has() on huge parts of the page, such as body:has(...) for many rules. It works, but forces the browser to re-check a lot; keep it targeted.
  2. Expecting :where() to win a specificity battle. It adds nothing by design.
  3. Forgetting the space: & .dark means a descendant, while &.dark means the same element.
  4. Nesting five levels deep just because you can.

Practice Exercises

Exercise 1. Give a form a red border whenever it contains any invalid input.

Show answer
form:has(:invalid) { border: 2px solid #c62828; }

Exercise 2. Rewrite .nav a:hover, .nav a:focus { color: red; } with nesting and :is().

Show answer
.nav {
  & a:is(:hover, :focus) { color: red; }
}

Frequently Asked Questions

Is :has() supported everywhere?

Yes, in current Chrome, Edge, Safari and Firefox; Firefox was the last to add it, in late 2023. Older browsers ignore the rule, so use it for enhancements rather than essential layout.

Do I still need Sass now that CSS has nesting?

For nesting alone, no. Sass still offers extras such as loops, mixins and splitting styles into many files, but plain CSS now covers nesting, variables and many maths functions.

Summary

  • :has() selects an element by what it contains or what follows it.
  • :is() groups selectors and takes the highest specificity inside it.
  • :where() groups selectors with zero specificity.
  • Native nesting uses & for the parent selector.

Next: Responsive Images in HTML.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *