Skip to content

Topics

Jump to a topic.

Search questions

Find a question by title, tag, or summary.

What does :has() make possible, and what are its limits?

Questions in this chapter (7)
  • Draft
  • medium
  • Askedvery often
  • Verified October 2026

Answer

Selectors used to look only down and forward: the subject of .card img is the image. :has() lets the subject depend on what's around it. Its argument is a relative selector, which starts with an implied or explicit combinator, anchored at the element being tested:

  • .card:has(img): a card containing an image anywhere (descendant).
  • .card:has(> img): a card with an image child.
  • h2:has(+ p): a heading immediately followed by a paragraph, which is previous-sibling selection.
  • h2:has(~ .note): a heading with any later sibling .note.

It works with state, so styles can react to the inside of a component with no JavaScript: form:has(:invalid), label:has(+ input:invalid), html:has(#dark:checked). Quantity queries become simple too: ul:has(> li:nth-child(4)) matches lists with four or more items.

Limits, all in Selectors 4:

  • No nesting: :has() isn't valid inside :has(), even wrapped in :is() or :not().
  • No pseudo-elements inside it, and it can't follow one (::before:has(a) is invalid).
  • Unforgiving list: one unknown selector in :has(.a, :foo) makes the whole selector invalid, so the rule is dropped. It was forgiving until a 2022 spec change.
  • Specificity is that of its most specific argument, like :is(): div:has(#target) is (1, 0, 1) and beat div.k.l.

Order with :not() matters: section:not(:has(h2)) is a section with no h2; section:has(:not(h2)) is a section with any non-h2 descendant.

:has() also works in JavaScript (querySelectorAll, matches). How engines keep it fast is a browser-internals topic.

Support: Chrome 105, Firefox 121, Safari 15.4.

Example

<style>
  .card:has(> img) { display: grid; }
  label:has(+ input:invalid) { color: red; }
  ul:has(> li:nth-child(4)) { columns: 2; }
</style>
<div class="card" id="with-image"><img alt=""><p>Text</p></div>
<div class="card" id="text-only"><p>Text</p></div>
<label for="email">Email</label>
<input id="email" type="email" required>
<ul id="short"><li>1</li><li>2</li><li>3</li></ul>
<ul id="long"><li>1</li><li>2</li><li>3</li><li>4</li></ul>
<script>
  const style = (id) => getComputedStyle(document.getElementById(id));
  console.log(style("with-image").display, style("text-only").display);
  // → grid block
  console.log(getComputedStyle(document.querySelector("label")).color);
  // → rgb(255, 0, 0)
  document.getElementById("email").value = "a@b.co";
  console.log(getComputedStyle(document.querySelector("label")).color);
  // → rgb(0, 0, 0)
  console.log(style("short").columnCount, style("long").columnCount);
  // → auto 2
</script>

Chrome 156 and Firefox 159 agreed. The label sits before the input yet reacts to its validity, and updates as soon as the value changes.

Pitfalls

  • Adding a fallback selector to the list: :has(.a, :new-thing) drops the whole rule in browsers that don't know :new-thing. Use separate rules, or wrap the arguments in :is(), which is forgiving.
  • Forgetting the specificity: an ID inside :has() counts as an ID, so :has(#x) can override component styles unexpectedly. Wrap it in :where() to make it zero.
  • Writing .card:has(img) when you mean a direct child; nested cards make an outer card match too. Use :has(> img).

Follow-ups

How do you test for :has() support?
@supports selector(:has(a)) in CSS or CSS.supports("selector(:has(a))") in JavaScript. Both browsers returned false for selector(:has(:has(a))), so the same check catches invalid uses.
Can :has() appear inside :is()?
Yes: selector(:is(:has(a))) was supported. But :has(:is(:has(a))) and :has(:not(:has(a))) were not, because the inner :has() is still inside :has().
What does document.querySelector(":has(:has(a))") do?
It throws a SyntaxError, like any invalid selector passed to the selectors API.

Exercises

Which sections get a yellow background?

<style>
  section:not(:has(h2)) { outline: 1px solid red; }
  section:has(:not(h2)) { background: yellow; }
</style>
<section id="a"><h2>Title</h2><p>Body</p></section>
<section id="b"><h2>Title only</h2></section>

Comments

Comments are GitHub Discussions: you sign in with GitHub to post or react.