Skip to content
All posts

§ AB-B11 · Web techniques

:has() for parent-aware styling

Style a wrapper by what is inside it. Form fields, radio cards and bulk actions with no state and no JavaScript.

· 4 min read · 3 demos

TL;DR
  • .field:has(input:focus-visible) styles the wrapper when the input inside has focus.
  • Read a choice from anywhere in a form: form:has(#team:checked) .note.
  • Pair :has() with :user-invalid so errors show after the user leaves the field, not on page load.
  • Keep the anchor small. Write .field:has(), never body:has().
  • Works in Chrome 105, Safari 15.4 and Firefox 121. Give old browsers a usable default.

A parent selector, finally

For years CSS could only look down the tree. A parent could not react to its child. So we wrote JavaScript to copy state upward: a focused class on the wrapper, a selected class on the card, a has-items class on the list.

:has() removes that code. It matches an element if something inside it matches. .card:has(img) is a card that contains an image. .field:has(input:focus) is a field whose input has focus.

Here is the first place to use it.

Fields that react

A text field is rarely just an input. It has a wrapper, a label, an icon and a hint. The focus ring belongs on the wrapper. The error belongs on the hint. The input is the only element that knows either state.

A field that styles itself
.has-field {
  --ease: cubic-bezier(.16, 1, .3, 1);
  --line: color-mix(in srgb, var(--color-ink) 14%, transparent);
}
.has-field .field {
  border-radius: 9px;
  box-shadow: inset 0 0 0 1px var(--line);
  transition: box-shadow 200ms var(--ease);
}

/* The wrapper reacts to the input inside it. */
.has-field .field:has(input:focus-visible) {
  box-shadow: inset 0 0 0 1px var(--color-brand), 0 0 0 3px color-mix(in srgb, var(--color-brand) 18%, transparent);
}
.has-field .field:has(input:user-invalid) {
  box-shadow: inset 0 0 0 1.5px var(--color-ink);
}

/* The hint and the check sit in the wrapper too. */
.has-field .hint,
.has-field .ok {
  opacity: 0;
  transform: translateY(-2px);
  transition: opacity 200ms var(--ease), transform 200ms var(--ease);
}
.has-field .field:has(input:user-invalid) + .hint,
.has-field .field:has(input:valid:not(:placeholder-shown)) .ok {
  opacity: 1;
  transform: none;
}

@media (prefers-reduced-motion: reduce) {
  .has-field .hint, .has-field .ok { transform: none; }
}
Focus ring from :focus-visible, error from :user-invalid after you leave the field, check from :valid. Each change fades in 200ms with a 2px rise.

Tab into the field and the whole wrapper lights up. Type something that is not an email and click away. The border darkens and the hint fades in. Type a valid address and a check appears. There is no event handler anywhere.

Two details make it feel right:

  • `:focus-visible`, not `:focus`. The ring shows for keyboard users and stays quiet on a mouse click.
  • `:user-invalid`, not `:invalid`. :invalid matches an empty required field the moment the page loads. :user-invalid waits until the person has typed and moved on. Nobody likes being told they are wrong before they start.

The markup stays plain:

<label class="field">
  <span>Work email</span>
  <input type="email" required placeholder="[email protected]" />
  <span class="ok">✓</span>
</label>
<p class="hint">Use an address like [email protected]</p>

Our Input and Field components use the same idea with React state, because they also need to support older browsers and custom error messages from a server.

Read a choice from anywhere

:has() does not have to sit on the direct parent. Put it on the form and you can style anything inside based on any input.

Radio cards and a live summary
.has-plans {
  --ease: cubic-bezier(.16, 1, .3, 1);
  --line: color-mix(in srgb, var(--color-ink) 14%, transparent);
}
.has-plans .plan {
  box-shadow: inset 0 0 0 1px var(--line);
  transition: box-shadow 200ms var(--ease), transform 200ms var(--ease);
}
.has-plans .plan:has(input:checked) {
  box-shadow: inset 0 0 0 2px var(--color-brand);
}
.has-plans .plan:has(input:focus-visible) {
  outline: 2px solid var(--color-brand);
  outline-offset: 2px;
}

/* The summary reads the choice from anywhere in the form. */
.has-plans .note {
  opacity: 0;
  transform: translateY(4px);
  transition: opacity 250ms var(--ease), transform 250ms var(--ease);
}
.has-plans:has(#plan-team:checked) .note-team,
.has-plans:has(#plan-solo:checked) .note-solo {
  opacity: 1;
  transform: none;
}

@media (prefers-reduced-motion: reduce) {
  .has-plans .note { transform: none; }
}
The checked card gets a 2px brand ring. The summary swaps text with a 250ms fade. Both read the same radio input.

The plan cards highlight themselves with .plan:has(input:checked). The summary under them reads the same radio with .has-plans:has(#plan-team:checked) .note-team. Two parts of the form stay in sync without a line of state.

Use this for small, local reactions:

  • Show a VAT field only when "Business" is picked.
  • Dim the submit button while the form :has(:user-invalid).
  • Switch a card to a compact layout when it :has(.badge).

Keep real logic in your code. If a choice changes what gets sent to the server, it belongs in state. :has() is for how things look.

Bulk actions with CSS

Tables are where :has() saves the most code. Pick a row and it tints. Pick any row and a bulk bar slides in. Count the picks with a CSS counter.

Row selection without JavaScript
.has-bulk {
  --ease: cubic-bezier(.16, 1, .3, 1);
  counter-reset: picked;
}
.has-bulk tr:has(input:checked) {
  background: color-mix(in srgb, var(--color-brand) 7%, transparent);
}

/* Count the picked rows with a CSS counter. No JavaScript. */
.has-bulk tbody input:checked {
  counter-increment: picked;
}
.has-bulk .count::before {
  content: counter(picked) " selected";
}

/* The action bar only appears once a row is picked. */
.has-bulk .bar {
  opacity: 0;
  transform: translateY(8px);
  pointer-events: none;
  transition: opacity 250ms var(--ease), transform 250ms var(--ease);
}
.has-bulk:has(tbody input:checked) .bar {
  opacity: 1;
  transform: none;
  pointer-events: auto;
}

/* No :has()? Keep the bar visible so the actions still work. */
@supports not selector(:has(*)) {
  .has-bulk .bar { opacity: 1; transform: none; pointer-events: auto; }
}

@media (prefers-reduced-motion: reduce) {
  .has-bulk .bar { transform: none; }
}
Rows tint at 7% brand. The bar fades in over 250ms and rises 8px. The count comes from a CSS counter.

Three rules do it:

  1. tr:has(input:checked) tints the row.
  2. .has-bulk:has(tbody input:checked) .bar shows the bar, with a 250ms fade and an 8px rise.
  3. A CSS counter increments on every checked box, and the bar prints it with counter().

The bar animates opacity and transform only, so it stays smooth while you click fast. Our Data table does the same job in React with keyboard range selection on top.

Keep it fast

Every time the DOM changes, the browser checks which :has() rules might now match. A narrow anchor keeps that check cheap.

  • Good: .field:has(input:user-invalid). Only fields are checked.
  • Risky: body:has(.modal-open). Every change anywhere re-checks the whole page.
  • Avoid deep chains like :has(> * > * > input). Put a class closer to the input instead.

On a page with a few hundred fields you will not notice. On a long list that updates every second, a broad :has() can show up in the Performance panel as style recalculation. Measure before and after.

Support

:has() works in Chrome and Edge 105, Safari 15.4 and Firefox 121. That covers most browsers in use today. :user-invalid works in Chrome 119, Safari 16.5 and Firefox 88.

Old browsers drop the whole rule they do not understand. Plan for that:

  • Make the default state usable. In the bulk demo the bar is visible by default when :has() is missing, through @supports not selector(:has(*)).
  • Never hide required content behind :has(). Use it to add polish, not to unlock a feature.

Next

Last stop in this group: keeping all of this smooth on a cheap phone. Next: Smooth on a mid-range phone.

Next · AB-B12 Smooth on a mid-range phone