What :has actually selects

The :has pseudo-class selects elements on the condition that they contain elements matching the selector passed into the function. Its most obvious use is as a parent selector: to style every <div> that holds a <p>, you can now express that relationship instead of being limited to styling downward.

div:has(p) {
  background: red;
}
/*
  <div> <!-- selected! -->
     <p></p>
  <div>

  <div></div> <!-- not selected -->
  <div> <!-- not selected -->
    <section></section>
  </div>
*/

Browsers did not support it at the time of writing, but it entered Safari Technology Preview 137, so implementation was underway. The feature is defined in the CSS Selectors Level 4 specification, the same document that defines :not.

Why this changes the mental model

CSS historically flows one way: parent to child, never back up the tree, which is why there were no parent selectors. Parsing HTML and CSS this way avoids the performance costs that conditionally selecting a parent could introduce. :has removes that constraint, and a range of uses opens up with it.

.parent .child {
  color: red;
}

The selector is more than a parent selector. In some of these patterns the parent appears only in the condition, while the element actually styled is a descendant. The subheading example shows this: chained :has() selectors such as .header-group:has(h2):has(.subtitle) mean “select this <h2> only if the .header-group contains an <h2> and that <h2> has a .subtitle.” That differs from .header-group:has(h2 .subtitle), a single argument list that asks whether the group holds another element that is an <h2> carrying the .subtitle class — which does not produce the desired result here.

h2,
.subtitle {
  margin: 0 0 1.5rem 0;
}
.header-group:has(h2):has(.subtitle) h2 {
  margin: 0 0 0.2rem 0; /* reduce spacing on header, because subtitle will handle it */
}

/*
  <div class="header-group">
    <h2>Blog Post Title</h2> <!-- main spacing applied here -->
  </div>

  <div class="header-group">
    <h2>Blog Post Title</h2>
    <div class="subtitle"> <!-- main spacing applied here -->
      This is a subtitle
    </div>
  </div>
*/
The CSS :has selector being helpful in spacing headers with subtitles (or not).
On the left, the main spacing happens after the h2, on the right, the main spacing happens after the subtitle.

Practical patterns

Reference the condition without styling the parent directly, and the matched ancestor can stay untouched. Applied to links, this selects links that contain images; seen this way, :has also supports conditionally adding margin and padding to elements based on their content.

a:has(> img) {
  border: 20px solid white;
}
/*  Matches <figure> elements that have a <figcaption> as a child element */
figure:has(figcaption) { … }

/* Matches <img> elements that is a child of a <figure> that contains a <figcaption> child element */
figure:has(figcaption) img { … }

Chaining and selector lists

Calls can be chained, or a list can be supplied as the argument.

article:has(h2):has(ul) {

}
article:has(h2, ul) {

}

The list is no longer forgiving. After the W3C adopted a resolution in December 2020 in response to a reported issue, a single invalid argument causes the entire list to be ignored.

/* 👎 */
article:has(h2, ul, ::-blahdeath) {
  /* ::blahdeath is invalid, making the entire selector invalid. */
}

Nesting a forgiving selector such as :is() or :where() works around this.

/* 👍 */
article:has(:where(h2, ul, ::-blahdeath)) {
  /* :where is a forgiving selector, making this valid. */
}

Feature detection

@supports(selector(:has(p))) {
  /* Supported! */
}

The other Selectors Level 4 pseudo-classes

Unlike :has, :not has broad browser support. It excludes what you don't want and reads almost as plain language.

ul li:not(:first-of-type) {
  color: red;
}

For spacing, the same pseudo-class targets everything except the last item, which is handy for a stack of elements inside a card:

ul li:not(:last-of-type) {
  margin-bottom: 20px;
}

The spec also defines :is, usable today in a large number of browsers:

:is(section, article, aside, nav) :is(h1, h2, h3, h4, h5, h6) {
  color: #BADA55;
}

/* ... which would be the equivalent of: */
section h1, section h2, section h3, section h4, section h5, section h6, 
article h1, article h2, article h3, article h4, article h5, article h6, 
aside h1, aside h2, aside h3, aside h4, aside h5, aside h6, 
nav h1, nav h2, nav h3, nav h4, nav h5, nav h6 {
  color: #BADA55;
}

Further reading