A component's appearance can depend on the content inside it. CSS :has() styles a parent element when a descendant matches a selector, so a warning badge or checked control can change its container without JavaScript.
The element before :has() receives the style. For instance, .notice-card:has(.notice-card__badge--warning) matches the card containing a warning badge, while a card containing only a normal badge keeps its base appearance. Without a combinator, the condition can match a descendant at any depth; :has(> .notice-card__badge--warning) limits it to a direct child.
Current browsers support :has(), but a readable base style still matters for older browsers. Keeping the warning text visible outside a selector support query preserves the message even when the parent highlight is unavailable. A text editor and browser are enough for a local HTML file; no server is needed.
Steps to style parent elements with CSS :has():
- Create an HTML document named index.html in your text editor with the following structure.
- Insert the cards and checkbox label immediately before the closing </body> tag.
<h1>Cards and child state</h1> <section class="notice-grid" aria-label="Cards"> <article class="notice-card"> <span class="notice-card__badge">Ready</span> <h2>Profile</h2> <p>This card keeps its base style.</p> </article> <article class="notice-card"> <span class="notice-card__badge notice-card__badge--warning">Action needed</span> <h2>Billing</h2> <p>The warning badge changes this parent.</p> </article> </section> <label class="field-label"> <input type="checkbox"> Annual plan selected </label>
The warning class belongs to the badge, not its card. The checkbox is inside the label, making the label another parent that can respond to a child's state.
- Add the base styles between the <style> and </style> tags in index.html.
body { margin: 24px; font: 18px/1.5 system-ui, sans-serif; color: #334155; } h1 { font-size: 24px; } h2 { margin: 12px 0 4px; font-size: 22px; } p { margin: 0; } .notice-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; } .notice-card { padding: 16px; border: 3px solid #cbd5e1; border-radius: 16px; background: #fff; } .notice-card__badge { padding: 4px 10px; border-radius: 20px; background: #e2e8f0; font-size: 14px; font-weight: 700; } .notice-card__badge--warning { background: #fed7aa; color: #9a3412; } .field-label { display: block; margin-top: 20px; padding: 16px; border: 3px solid #cbd5e1; border-radius: 16px; font-weight: 600; } input { width: 18px; height: 18px; accent-color: #15803d; }
The base rules give both cards neutral borders and leave the warning badge readable without :has().
- Append the parent selectors after the base rules, immediately before </style>.
@supports selector(.notice-card:has(.notice-card__badge--warning)) { .notice-card:has(:where(.notice-card__badge--warning)) { border-color: #c2410c; background: #fff7ed; } .field-label:has(input:checked) { color: #166534; border-color: #15803d; background: #f0fdf4; } }
The first rule highlights only the card containing the warning badge. The second highlights the label while its checkbox is checked. :where() keeps the badge condition from adding specificity; normally :has() takes the specificity of its most specific argument. A subject scoped to notice-card avoids making unrelated page changes relevant to the condition, as body:has(...) would.
Related: How to debug CSS specificity - Save index.html as a plain-text HTML file.
- Open index.html in your browser.
The address starts with file:/// and ends with index.html; the folder depends on where you saved it.
- Select the Annual plan selected checkbox.
Before selection, the label is neutral and only the Billing card has an orange border. If both cards remain neutral, the parent rule is not active; a current browser and the selector block above are required.
- Verify that the checked label is green while Billing stays orange and Profile keeps its neutral border.
The green border and background belong to the label around the checkbox. Only Billing contains the warning badge, so the separate Profile card remains unchanged.