pseudo-class

:has() Relational Pseudo-Class

Matches an element if any of the relative selectors passed as arguments match at least one element rooted at that element. It effectively enables a parent selector in CSS.

:has(selector)

Example

article:has(img) { padding-top: 0; } /* Articles that contain an <img> */

✓ Matches

  • <article><img src="x.jpg"></article>
  • <article><p>text</p><img></article>
  • <article><figure><img></figure></article>

✗ Does not match

  • <article><p>text only</p></article>
  • <article></article>

Browser support

Supported in all modern browsers since late 2023.

Related selectors

← All CSS selectors · CSS selectors cheat sheet