:has()

W3C CSS Snapshot 2026

Match an element based on a relationship, for example when it has a matching child or next sibling.

On this page

Syntax and values

Value or syntaxWhat it does
section:has(> h2)Matches a section with an h2 direct child.
h2:has(+ p)Matches an h2 immediately followed by p.
label:has(+ input[required])Matches a label immediately followed by a required input.

Try the example

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>:has()</title>
  <style>
section:has(> h2) { border: 2px solid teal; }
  </style>
</head>
<body>
<section><h2>Workshop</h2><p>Bring a notebook.</p></section>
<section><p>Open seating.</p></section>
</body>
</html>

What to expect Only the section with the heading gets a border.

Official references