CSS Garden All articles
Accessibility

Train the Selector, Don't Strangle the Page: A Sane Approach to :has()

CSS Garden
Train the Selector, Don't Strangle the Page: A Sane Approach to :has()

Photo: Acabashi, CC BY-SA 4.0, via Wikimedia Commons

Every few years a CSS selector arrives and the internet immediately asks it to do every job in the garden. :has() is the current favourite. It can style a parent based on its children, detect a checked checkbox, highlight a form field with an invalid input, and make a card behave differently when it contains an image. That is genuinely useful. It is also exactly the kind of power that encourages people to build a trellis out of fishing line and then wonder why the tomatoes are on the floor.

My view is simple: :has() is a trellis, not a weedkiller. It should guide structure and express relationships that already exist in the markup. It should not become a blunt instrument for replacing JavaScript logic, patching inaccessible markup, or writing selectors so broad that every component on the page gets tangled in them.

The trellis test

A trellis does not create the plant. It gives the plant something to grow against. In CSS terms, that means the relationship should be visible in the DOM and the selector should describe it clearly. A form field with an invalid input is a good example. The field wrapper contains the label, input, and error message. :has() can style the wrapper when the input is invalid, and the markup still makes sense without CSS.

.form-field:has(input:invalid:not(:placeholder-shown)) {
  border-color: #b3261e;
  background: #fff5f4;
}

.form-field:has(input:invalid:not(:placeholder-shown)) .form-error {
  display: block;
}

That is a trellis. The error message is already in the DOM, the input is already invalid, and the CSS is simply making the relationship visible. It is not inventing state. It is not hiding the error from assistive technology. It is not pretending a div is a button.

A weedkiller selector looks different. It might be something like body:has(.modal-open) * or a rule that scans half the document to find one state. It might style a parent based on a deeply nested descendant with no clear boundary. It might rely on :has() to make a custom control keyboard accessible when the real fix is to use a native element. Those patterns are difficult to reason about, expensive to maintain, and often fragile when the DOM changes.

Forms: useful, but keep the semantics

Forms are where :has() shines. You can highlight a fieldset when any radio button inside it is selected, style a consent row when the checkbox is checked, or show a character-count warning when a textarea exceeds a limit. The key is to keep the accessible name, description, and error association in the HTML.

.consent-row:has(input:checked) {
  border-color: #2e7d32;
  background: #f3fbf4;
}

.quantity-field:has(input[type='number']:out-of-range) {
  outline: 2px solid #b3261e;
  outline-offset: 2px;
}

Do not use :has() to replace aria-describedby or a live region. Do not rely on colour alone to communicate the state. The selector is a visual hint, not the accessible truth. If the form still works and makes sense with CSS disabled, you are probably on the right track.

Cards: relationship styling, not layout magic

Card patterns are another strong use case. A card with an image might need different padding. A card with a badge might need a bit more top space. A card with no media might need a simpler border treatment. These are structural relationships, and :has() expresses them neatly:

.card:has(.card-media) {
  padding-block-start: 0;
}

.card:has(.card-badge) .card-title {
  margin-block-start: 0.5rem;
}

.card:has(.card-media:empty) {
  border-style: dashed;
}

That is fine. What is less fine is using :has() to build an entire interactive card with hidden state, focus management, and event handling in CSS. If clicking the card should navigate, use a link. If selecting the card should update a basket, use a button and JavaScript. CSS can style the result, but it should not be the only thing holding the interaction together.

Navigation: state is not the same as structure

Navigation is where teams often get overexcited. :has() can style a nav item when a submenu is open, or highlight a section when a child link is current. That can be genuinely helpful, especially for visual state that matches an existing aria-current or aria-expanded attribute.

.nav-item:has([aria-current='page']) {
  border-block-end: 3px solid currentColor;
}

.nav-item:has(.submenu[aria-expanded='true']) {
  background: #f4f6f2;
}

But be careful not to create a second source of truth. If the visual state depends on :has() finding a class that JavaScript toggles, document that convention. If the open state is expressed with aria-expanded, style from that attribute rather than a separate .is-open class. The selector should read the same state that assistive technology and tests can read.

Guardrails for teams

A few conventions keep :has() from becoming a creeping vine. First, scope it to a component. Avoid document-wide selectors unless you genuinely need them. Second, prefer explicit descendant relationships over broad wildcards. Third, wrap enhancements in a feature query if the fallback matters:

@supports selector(:has(*)) {
  .card:has(.card-media) {
    padding-block-start: 0;
  }
}

Fourth, test with CSS disabled and with a keyboard. Fifth, write down the team rule: :has() is for styling relationships and state that already exist in the DOM. It is not for replacing semantic HTML, focus management, or application logic. If a selector needs a comment to explain which element it is really looking for, that is a sign the DOM or the component boundary needs work.

A trellis is a quiet piece of structure. You notice it when it is doing its job, and you notice it when it is missing. :has() should feel the same: precise, supportive, and a little boring in the best way. Train it around clear relationships, and it will help your components grow. Let it strangle everything in sight, and you will spend January untangling it.


All articles

Related Articles

Grey November Contrast: Palettes That Survive a Manchester Drizzle

Grey November Contrast: Palettes That Survive a Manchester Drizzle

Before the Tills Get Busy: A Winter CSS Audit with @supports

Before the Tills Get Busy: A Winter CSS Audit with @supports

Planting by Space, Not Device: Responsive CSS for Real UK Rooms

Planting by Space, Not Device: Responsive CSS for Real UK Rooms