日本語

“CSS :has(): Style a Parent Based on Its Child’s State”

​​CSS​Forms,HTML,Playwright

On a signup form, you can highlight the selected plan card, or show a text field only when "Other" is chosen, by writing :has() on the parent element. This works with CSS alone. You don't need JavaScript to listen for change events and add or remove classes.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and headless Chromium 153. I opened a test form with Playwright and read the computed values with getComputedStyle() after clicks and key presses. I did not test Firefox, Safari, real smartphones, or screen reader announcements.

Video of a signup form changing as it is clicked. A counter above the form shows the click count (1st of 3 clicks). First, the Standard card gets a light-blue border and the other two cards fade. Next, the Premium card is selected and the highlight moves to it. Then the "Other" option under "How did you hear about us" is clicked, and the "Please specify" field appears. A status panel at the bottom shows the state of the selected card and the field and flashes pink when it changes. The labels in the video are in Japanese.
Changes when clicking a card and "Other". The top bar, the cursor circle, and the status panel at the bottom were added for the recording.

Highlighting the selected card

In the form I tested, each plan's radio button and text are wrapped in a <label class="plan">, which forms the card.

html
<fieldset class="plans">
  <legend>プラン</legend>
  <label class="plan">
    <input type="radio" name="plan" value="light">
    <span class="plan-name">ライト</span>
    <span class="plan-note">メモ 100件まで</span>
  </label>
  <label class="plan">
    <input type="radio" name="plan" value="standard">
    <span class="plan-name">スタンダード</span>
    <span class="plan-note">メモ 1,000件まで</span>
  </label>
  <label class="plan">
    <input type="radio" name="plan" value="premium">
    <span class="plan-name">プレミアム</span>
    <span class="plan-note">メモ 無制限</span>
  </label>
</fieldset>

The style for a selected card is written as .plan:has(input:checked), which means "a .plan that contains a checked input." The normal border color (#c5d0d5) and corner rounding are set on .plan, and I left them out of the example below.

css
.plan:has(input:checked) {
  border-color: #00c8e8;
  background: #eefafd;
}

.plans:has(input:checked) .plan:not(:has(input:checked)) {
  opacity: 0.6;
}

The second rule dims the unselected cards once one has been chosen. I also put :has() on the outer .plans, so the rule applies only when some radio button is checked. This way, before anything is selected, all three cards appear normally without being dimmed.

The MDN page for :has() says that parent:has(child) selects the parent, and parent:has(> child) selects only the direct parent.

In my test environment, I read the computed values of each card after each action. The results are below.

Action Light Standard Premium
Just after loading gray border gray border gray border
Click the "Standard" text dimmed light-blue border dimmed
Click the description to the right of "Premium" dimmed dimmed light-blue border
Press the ↑ key dimmed light-blue border dimmed
Set Light's checked to true in JavaScript light-blue border dimmed dimmed
Call form.reset() gray border gray border gray border

In the table, "light-blue border" means border-color is rgb(0, 200, 232) and the background is rgb(238, 250, 253). "Dimmed" means opacity is 0.6. Because each radio button is wrapped in a label, clicking either the plan name or the description on the right selects the plan. The card appearance updated immediately whether the selection changed by keyboard, by setting checked from JavaScript, or by resetting the form.

Showing a field only when "Other" is selected

In the "How did you hear about us" group, I show the "Please specify" text field only when "Other" is selected. The field is hidden with display: none by default and is shown only while the "Other" radio button is checked.

html
<fieldset class="source">
  <legend>知ったきっかけ</legend>
  <label><input type="radio" name="source" value="search"> 検索</label>
  <label><input type="radio" name="source" value="sns"> SNS</label>
  <label><input type="radio" name="source" value="other" id="source-other"> その他</label>
  <div class="other-field">
    <label for="other-text">具体的に:</label>
    <input id="other-text" type="text" name="other">
  </div>
</fieldset>
css
.other-field {
  display: none;
}

.source:has(#source-other:checked) .other-field {
  display: block;
}

Here I change the style of .other-field, not of the selected .source itself. The radio buttons are inside labels and are not siblings of the div holding the text field, so they can't be reached with sibling combinators such as ~. The selector goes up to the common parent and then down into another child.

In the test environment, clicking "SNS" kept the field at none. Clicking "Other" changed it to block, and choosing "Search" again returned it to none.

Warning

The value of a hidden field is still sent with the form. I selected "Other", typed a test value, and then selected "Search" again. The field was hidden, but new FormData(form).get('other') still returned the value I had typed. On the server, read other only when source is other.

Fading the submit button until the user agrees

:has() can also be written on a form. The following rule fades the submit button and stops it from responding to clicks while the "I agree to the terms of service" checkbox (#agree) is unchecked.

css
form:has(#agree:not(:checked)) button[type="submit"] {
  opacity: 0.5;
  pointer-events: none;
}

However, this only changes how the button looks and blocks mouse clicks. It does not stop the form from being submitted. When I tried submitting without checking the box, the results were:

Action (agreement unchecked) No required on #agree required on #agree
Click where the submit button is No submit event fired No submit event fired
Press Enter in the "Please specify" field submit event fired No submit event fired

pointer-events: none only blocks clicks. Pressing Enter in a text field still submitted the form. When I added required to the checkbox, Enter did not submit either, and #agree matched :invalid. I leave blocking the submission to HTML's required attribute, and use :has() only to visually show that the button isn't available yet.

Syntax that makes the whole rule be ignored

If any selector inside :has() is invalid, the entire rule is not used. In the test environment, I loaded a <style> containing only one rule each time, then checked the number of rules left in the CSSOM (sheet.cssRules) and the border of the card after selecting Light.

Selector written Rules left After selecting Light
.plan:has(input:checked) 1 Card border turned light blue
.plan:has(input:checked, input:unknown-state) 0 Still gray border
.plan:has(input:checked), .plan:unknown-state 0 Still gray border
.plan:is(:has(input:checked), :unknown-state) 1 Card border turned light blue
.plans:has(.plan:has(input:checked)) 0 No border on .plans
.plans:has(.plan input:checked) 1 .plans got a 2px light-blue border
.plan:has(::before) 0 Still gray border

:unknown-state is a nonexistent pseudo-class that I used for the test. When one comma-separated item inside the parentheses of :has() was invalid, the whole rule disappeared, including the valid input:checked part. When I wrapped it in :is(), only the invalid part was dropped, and .plan:is(:has(input:checked)) remained as a rule.

Writing :has() inside :has(), and putting a pseudo-element such as ::before inside it, also removed the whole rule. The Selectors Level 4 spec states that :has() cannot be nested and that pseudo-elements are invalid inside :has(). The MDN page explains that many pseudo-elements exist depending on the parent's styles, so letting :has() query them could create a cycle.

If you want to check a grandchild's state, don't nest :has(). Instead, use a descendant selector inside the parentheses, such as .plans:has(.plan input:checked). With this form the rule was kept, and choosing Light added a border to .plans.

Point

When combining :has() with a new pseudo-class, either split it into a separate rule, or wrap the :has() itself in :is(), as in .plan:is(:has(input:checked), :unknown-state).

Browser support

According to the compatibility data in @mdn/browser-compat-data 8.1.8, :has() is supported in Chrome and Edge 105, Firefox 121, and Safari 15.4 (Chrome for Android 105, Firefox for Android 121, and Safari on iOS 15.4). The MDN page says that in browsers without :has() support, the whole rule is ignored unless the selector is inside :is() or :where(). This is the same behavior as the invalid selectors in the table above.

​Posted by 86