1.1 · Selectors & structure

The parent selector: :has()

Style a parent based on what’s inside it, with no JavaScript and no extra classes.

For twenty years CSS could only look down the tree. :has() lets an element match based on what it contains, or on what comes after it. A card can style itself when its checkbox is checked. A form can react when any field is invalid.

Read .plan:has(:checked) as “a plan that has a checked input inside it”. The selector still styles .plan, not the input.

Before
// JS toggles a class on the parent
input.addEventListener('change', () => {
  card.classList.toggle(
    'is-selected',
    input.checked,
  )
})
Now
.plan:has(:checked) {
  border-color: var(--accent);
}

/* Anything can react to state elsewhere */
.signup:has([value='team']:checked)
  .trial-note {
  display: block;
}