CSS3 Advanced Selectors · 9 min read

Modern CSS Selectors: :has(), :is(), and :where()

Lesson 18 of 41 in the CSS3 learning path.

How do :has(), :is(), and :where() change selector lists and specificity?

:has(), :is(), and :where() change how you group selectors and how specificity is scored. :has() matches an element when a relative selector inside it matches, so a plan card can restyle itself when it contains a checked radio. No script is required. :is() groups selectors, and the whole :is() takes the specificity of its most specific argument. :where() groups the same way, but its specificity is always zero, so a later element rule can override it. That makes :where() the right tool for a reset you expect classes to beat.

The sample is a plan picker. The checked card changes border, fill, and name color with :has(input:checked). One :is() rule shares a focus outline across links, buttons, and plans. A :where() margin reset loses to a normal p rule, so the lead keeps the element margin. Use :has() when a parent must react to state inside it, not as a substitute for a class you already control.

Syntax & Parameters

Syntax
.plan:has(input:checked) { border-color: #0f766e; }
:is(a, button, .plan):focus-visible { outline: 3px solid #0f766e; }
:where(h2, p) { margin: 0; }

Example

Interactive preview available
styles.css
body {
  margin: 0;
  font-family: "Segoe UI", system-ui, sans-serif;
  line-height: 1.45;
  color: #1c2430;
  background: #f4f1ea;
}
.page {
  max-width: 32rem;
  margin-inline: auto;
  padding: 1.1rem;
}
h1 { margin: 0 0 0.35rem; font-size: 1.4rem; }
p { margin: 0 0 0.8rem; }
:where(h2, p) { margin: 0; }
h2 { font-size: 0.8rem; letter-spacing: 0.05em; text-transform: uppercase; color: #5c6b7a; }
.plans {
  display: grid;
  gap: 0.6rem;
  margin: 0.4rem 0 0.9rem;
}
.plan {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 0.6rem;
  padding: 0.75rem 0.85rem;
  border: 2px solid #e4e0d8;
  border-radius: 12px;
  background: #fff;
}
.plan-name { font-weight: 700; }
.plan-price { color: #5c6b7a; font-size: 0.9rem; }
.plan:has(input:checked) {
  border-color: #0f766e;
  background: #f0fdfa;
}
.plan:has(input:checked) .plan-name { color: #0f766e; }
:is(a, button, .plan):focus-visible,
.plan:has(:focus-visible) {
  outline: 3px solid #0f766e;
  outline-offset: 3px;
}
button, .more {
  font: inherit;
  font-weight: 600;
}
button {
  padding: 0.45rem 0.75rem;
  border: 0;
  border-radius: 8px;
  background: #0f766e;
  color: #f8fafc;
}
.more { color: #0f766e; }

Expected Output

Three plan rows stack with a 0.6rem gap. Studio is checked, so its card has a teal border, a mint fill, and a teal name. Starter and Team stay white with a warm gray border and a dark name. Choosing another radio moves that treatment to the chosen card and clears it from Studio. The price stays gray.

The lead paragraph has space under it. The "Plans" heading does not add its own margin, so it sits close to the list. Tab to the compare link, the Continue button, or a plan and the same 3px teal outline appears. The outline rule is one :is() group, not three copies.

Line-by-Line Walkthrough

  1. .plan:has(input:checked) matches a plan label only when a checked input sits inside it. The Studio card is checked in the HTML, so it paints on first view.
  2. The checked plan gets a teal border and a mint background. .plan:has(input:checked) .plan-name paints only that card's name teal. The other names stay dark.
  3. Clicking another label checks its radio and unchecks Studio, because they share a name. :has() updates with the state. No script listens for the click.
  4. Specificity of .plan:has(input:checked) adds .plan, which is one class, to the most specific selector inside :has(). input:checked is one element plus one pseudo-class.
  5. :is(a, button, .plan) groups three targets for one focus outline. You do not repeat the outline declarations three times.
  6. :is() takes the specificity of its most specific argument. .plan is a class, so this :is() counts as one class, not as a plain element, even for the anchor and the button.
  7. :where(h2, p) looks like the same kind of group, but its specificity is zero and the arguments do not add any. It is a reset, not a locked-in rule.
  8. p { margin: 0 0 0.8rem } is only an element, score (0, 0, 1). It beats :where() even though the :where() rule comes later. The lead keeps its bottom margin.
  9. h2 has no element rule of its own for margin, so the zero-specificity :where() margin of 0 is what the heading keeps. That is why the heading sits tight on the plans.
  10. Reach for :has() when the parent must react to state inside it, such as a checked input or an empty list. If you already have a class for that state, the class is simpler.

:is() is the grouping tool when you want real specificity. :where() is the grouping tool when you want none.

Common Pitfalls & Security

:is() is not a low-specificity helper. That is :where(). Mixing them up is how a reset becomes impossible to override, or a focus rule loses to a plain element.

  • :is(h2, .plan-name) has the specificity of .plan-name, a class. A later h2 rule will not beat it. If you wanted a weak group, use :where().
  • :where() will not win against an element selector. A margin reset in :where() disappears as soon as p or h2 sets margin, which is what the lead demonstrates on purpose.
  • :has() on a huge, frequently changing DOM can be expensive. Use it for a component, such as a plan card, not for a selector that walks the entire document on every input.
  • :has(input:checked) does not replace the name grouping of the radios. Without a shared name, more than one card can look selected.
  • Do not let users inject selector text. A crafted :has() or attribute selector in a user stylesheet can probe hidden values. Keep selectors in your file.

Best Practices & Modern Standards

Use :where() for resets and for base element margins you expect every component to replace. Use :is() when several real components should share a focus ring or a type rule and you want that rule to hold. Use :has() when the parent is the thing that should look different, as with a selected plan, an invalid fieldset, or a card that contains an image.

Keep the plan row itself on grid or flex. :has() changes paint. It does not create the columns. Specificity still decides fights, so a class on the card remains the right tool when you control the markup. Custom properties can carry the teal accent so the :has() rule sets one token instead of repeating the color. A container query can still change the plan row when the picker gets wide. None of that requires a script to toggle a class.

Real-World Use

A checkout picker lets the student choose Starter, Studio, or Team. The card that contains the checked radio turns mint and teal through :has(). Keyboard focus on the link, the button, and the card shares one outline through :is(). A :where() margin reset gives headings a tight default, and the lead paragraph keeps the margin from the element rule.

The same trio fits a settings list with a selected row, a form section that highlights when it contains an error, and a reset stylesheet that must stay easy to override.

Frequently Asked Questions

What does :has(input:checked) match?

It matches an element that contains a checked input. On .plan, the label becomes the subject, so the card can change border and background when the radio inside it is checked. The input itself is not the thing being painted. Sibling cards that are unchecked do not match.

How is :is() specificity calculated?

The whole :is() takes the specificity of its most specific argument, then you add the rest of the selector. :is(a, button, .plan) counts as one class because .plan is the strongest argument. That is why this focus rule is harder to override than a plain button or anchor selector.

Why did my :where() reset lose to a simple p rule?

:where() and everything inside it contribute zero specificity. An element selector is (0, 0, 1), which is already stronger, and source order cannot save the :where() rule. That is the feature. Put forgiving resets in :where() so a normal p, h2, or class can override them without climbing the score.

Should I use :has() instead of a modifier class?

Use :has() when the state already lives in the document, such as a checked radio, and the parent should reflect it. If you control a class like is-selected and the state does not exist in a child, the class is clearer. :has() is not a reason to stop naming components.

Need more coins?

You do not have enough coins for this action.

Your balance: 0 coins

Share

Reactions

Loading reactions…