Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

:has() — relational pseudo-class

:has() selects an element based on its relationships with other elements. Its arguments are relative selectors evaluated against the element being tested.

Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Syntax

E:has(relative-selector-list)

E:has(...) matches E if at least one argument selector finds a matching element relative to E. It selects E, not the element found within the argument.

.card:has(.badge) checks descendants; .card:has(> .badge) checks only direct children. h2:has(+ p) checks the next sibling element. A comma means OR, while chained :has() conditions must all hold.

The specificity of :has() equals its most specific argument, regardless of which matches. Nesting :has() is forbidden; pseudo-elements require explicit specification permission. :not(:has(.badge)) means no such descendant exists, whereas :has(:not(.badge)) requires a descendant without that class.

Examples

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>:has() example</title>
<style>
  .card { color: rgb(0, 0, 0); }
  .card:has(> .badge) { color: rgb(0, 128, 0); }
</style>
<div class="card" id="a">A <span class="badge">New</span></div>
<div class="card" id="b">B</div>
</html>

Result

#a: rgb(0, 128, 0)
#b: rgb(0, 0, 0)

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources