Full answer
:has() is a relational pseudo-class. A:has(B) matches an element A if the selector B, evaluated relative to A, matches something. In practice it lets CSS style an element based on its descendants, or on its following siblings, which was impossible before without JavaScript. People call it the "parent selector", but it is more general.
Relative selectors inside it start with a combinator: :has(> img) means a direct child image, :has(+ p) means the next sibling is a paragraph.
Typical uses:
- Style a card differently when it contains an image:
.card:has(img). - Style a label or row when its checkbox is checked:
label:has(input:checked). - Show an error summary when a form has invalid fields:
form:has(:user-invalid)orform:has(:invalid). - Change a heading's spacing when a paragraph follows:
h2:has(+ p). - Quantity-like conditions:
ul:has(li:nth-child(4))matches a list with at least four items.
Its specificity is that of its most specific argument. You cannot nest :has() inside another :has().
The first card becomes a two-column grid because it contains an img. The second stays as a block. Ticking the checkbox gives the label a green background. The list with four items is highlighted yellow, and the one with three is not. The @supports not selector(:has(a)) block is a fallback for browsers that do not know the selector.
Browsers have to re-check :has() rules when the DOM changes anywhere inside the scope, so it is more costly than a simple class selector. Engines optimise common cases, but a broad rule such as body:has(.modal) on a large page is something to measure.
<style>
.card { padding: 8px; border: 1px solid #999; margin-bottom: 8px; }
.card:has(img) { display: grid; grid-template-columns: 60px 1fr; gap: 8px; }
.option { display: block; padding: 6px; border: 1px solid #ccc; }
.option:has(input:checked) { background: #dff5e1; border-color: seagreen; }
ul:has(li:nth-child(4)) { background: #fff3cd; }
@supports not selector(:has(a)) { .option { border-style: dashed; } }
</style>
<div class="card" id="c1"><img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" width="60" height="60" alt=""><p>With picture</p></div>
<div class="card" id="c2"><p>Text only</p></div>
<label class="option" id="o1"><input type="checkbox" id="chk"> Notify me</label>
<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>Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- A:has(B) matches A based on its descendants or later siblings.
- Relative selectors start with a combinator, as in :has(> img) or :has(+ p).
- Specificity equals that of the most specific argument.
- You cannot nest :has() inside :has().
- Scope it narrowly and use @supports selector(:has(a)) for fallbacks.
Common mistakes
- assuming :has() is only for parents. It also works for siblings, as in h2:has(+ p), with a leading combinator.
- shipping a design that breaks completely without :has(). Use @supports selector(:has(a)) to write a sensible fallback.
Follow-ups
- how would you style a form that has any invalid field?
- How do you check support for :has() in CSS?
- Which uses of :has() can replace small JavaScript scripts?
Tips
- Scope :has() narrowly. Prefer .card:has(> img) to :has(img) on a high-level container when you can.
- Give two real uses: a card with an image and a form with invalid fields.
- Mention performance and fallbacks without being asked.