What does the CSS :has() selector do?

IntermediateCSS interview questionBy , updated 17 November 2026

Short answer

:has() is a relational pseudo-class: A:has(B) matches an element A if selector B matches something relative to it. .card:has(img) selects cards that contain an image, and h2:has(+ p) selects headings directly followed by a paragraph. It finally lets CSS select a parent or a previous sibling based on what comes after it, and it works in all current major browsers.

Why it matters

CSS selectors only ever looked down and forward: descendants, children and later siblings. You could style the image inside a card, but not the card because it holds an image. That needed JavaScript adding a class like .card--with-image. :has() turns the question around. The subject of the selector is still the element before :has(), but the condition can look at its contents or its following siblings.

How the argument works

The argument is a relative selector list. Each one is measured from the element being tested:

Selector Matches
.card:has(img) A .card with an img anywhere inside
.card:has(> img) A .card whose direct child is an img
h2:has(+ p) An h2 immediately followed by a p
li:has(~ li.active) Every li that comes before an .active sibling
.card:has(img, video) A .card containing an image or a video (either matches)
.card:has(img):has(h3) A .card containing both
.card:not(:has(img)) A .card with no image

Note the difference between the last two forms: a comma inside :has() means "or", chaining two :has() calls means "and".

Practical examples

A form or field with an invalid input:

/* Highlight the whole field, label included, once the user has made a mistake */
.field:has(input:user-invalid) {
  border-left: 4px solid #b00020;
}
.field:has(input:user-invalid) .field__hint {
  color: #b00020;
}

/* Show a notice at the top while any field the user has touched is invalid */
.form-notice { display: none; }
form:has(:user-invalid) .form-notice {
  display: block;
}

:user-invalid only matches after the user has interacted with the field, so the form does not start red. Styling alone is not enough for accessibility, though: the error still needs text linked with aria-describedby, as explained in How do you make forms accessible?.

A card that changes layout when it has an image:

.card { display: grid; gap: 1rem; padding: 1rem; }
.card:has(> img) {
  grid-template-columns: 8rem 1fr;
  padding: 0;
}

Checked state without JavaScript:

label:has(input[type="checkbox"]:checked) { background: #e8f0fe; }

Quantity queries:

/* Switch to two columns once the list has at least six items */
ul:has(> li:nth-child(6)) { columns: 2; }

Page-level state:

/* Stop the page scrolling behind an open modal dialog */
html:has(dialog[open]) { overflow: hidden; }

Specificity

:has() itself adds nothing; it takes the specificity of the most specific selector in its argument, the same rule as :is() and :not().

.card:has(img)         /* (0,1,1): class + type */
.card:has(#hero, img)  /* (1,1,0): the ID wins, even when img is what matched */

That second case surprises people: specificity is fixed by the argument list, not by which item matched. The full rules are in How does CSS specificity work?.

Rules and limits

  • No nesting. :has() cannot appear inside another :has().
  • No pseudo-elements in the argument. :has(::before) is invalid.
  • It is unforgiving. If one selector in the list is invalid or unsupported, the whole rule is dropped. Wrap the list in :is(), which is forgiving, if you mix in something newer.
  • It works in JavaScript too. document.querySelectorAll(".card:has(img)") is valid wherever the browser supports :has().

Performance, honestly

Most selectors can be checked by looking at an element and its ancestors. :has() makes the browser watch the other direction: a change deep inside a subtree can change whether an ancestor matches, so the engine has to track that and restyle the right elements. Browser engines put real work into making this fast, and ordinary uses such as .field:has(input:user-invalid) or .card:has(> img) are not something you usually need to think about.

The cost grows when the anchor is very broad and the argument is a deep descendant search on a large, frequently changing page, for example body:has(.some-class) or *:has(...) on a page of thousands of elements updated many times a second. To keep it cheap:

  • Anchor on a specific class, not body, html or *, unless you mean page-wide state.
  • Prefer > and + over a plain descendant search when that is what you mean.
  • Measure in the browser's performance panel before rewriting anything. "Recalculate style" time is the number to watch.

Support

:has() is supported in all current major browsers; Firefox was the last to ship it, at the end of 2023. For older browsers, feature-detect and treat the rule as an enhancement:

@supports selector(:has(a)) {
  .card:has(> img) { grid-template-columns: 8rem 1fr; }
}

Other relational and logical selectors are covered in What are pseudo-classes and pseudo-elements in CSS?.

Practise it

Common follow-up questions

Is :has() a parent selector?

It is more general. .child's parent is selected with :has(> .child), but :has() can also match any ancestor, a previous sibling (:has(+ .x)) or an element with several conditions. "Parent selector" is just the most famous use.

Can I select a previous sibling with :has()?

Yes. a:has(+ b) matches an a immediately followed by a b, and a:has(~ b) matches an a followed somewhere later by a b. Before :has(), there was no way to do this in CSS.

What is the difference between :has() and :is()?

:is(A, B) asks "is this element A or B?" :has(A) asks "does something relative to this element match A?" They share the specificity rule, but :is() is forgiving and :has() is not.

Does :has() replace JavaScript class toggling?

For state the DOM already knows, often yes: checked inputs, open dialogs, invalid fields, empty containers. For state that lives only in your app (a fetched result, a user preference), you still need a class or attribute.

Mistakes to avoid

  • Writing .card:has(img) when you mean a direct child, and matching images nested inside unrelated parts

  • Treating a comma inside :has() as "and"

  • Putting an ID or unsupported selector in the argument without realising it changes specificity or drops the rule

  • Using colour from :has(:invalid) as the only error signal

  • Hanging every rule off body:has(...) for convenience on large, busy pages

Practise it in code

Interviewers rarely stop at the definition. These questions open in an editor with tests.

  1. Predict which elements CSS selectors match: nth-child, nth-of-type, :not, :has and sibling combinatorsEasy20 min
  2. Style form validation states with :user-invalid, :placeholder-shown and :focus-visibleEasy20 min
  3. Choose cheap selectors and CSS containment: :has(), contain and content-visibility: autoMedium30 min
  4. Style a custom checkbox and radio button that stay keyboard and screen reader accessibleEasy20 min
  5. Predict which color wins: specificity, !important and cascade layersEasy20 minFree