More CSS answers

by Danny
Published 2 min read

The :has() pseudo-class lets you style an element based on what it contains, so you can finally select a parent in CSS. It works in every major browser.

The short answer

#

Use the :has() pseudo-class. It matches an element if the selector you pass to it matches something relative to that element, usually a descendant. It has been supported in Chrome, Edge, Safari and Firefox since late 2023, so you can use it without a fallback for most audiences.


_10
/* style a card that contains an image */
_10
.card:has(img) {
_10
padding: 0;
_10
}
_10
_10
/* style a field whose input is invalid */
_10
.field:has(input:user-invalid) {
_10
border-color: crimson;
_10
}

Why it happens

#

For most of CSS's history, selectors could only look down and forward: from an ancestor to its descendants, or from an element to its later siblings. There was no way to say "this element, if it contains that one". Browser makers held off for years because a parent selector can force the browser to re-check large parts of the page whenever the DOM changes.

:has() is the answer that shipped. It takes a relative selector list and matches the element it is attached to when any of those selectors match. It is more general than a parent selector: it can look at children, any descendants or following siblings.

How to fix it

#

Select a parent by its children

Attach :has() to the element you want to style, and put the child selector inside it. Use > inside the brackets to match direct children only.


_10
/* any list item that contains a link */
_10
li:has(a) {
_10
list-style: none;
_10
}
_10
_10
/* only sections whose direct child is an h2 */
_10
section:has(> h2) {
_10
margin-block: 3rem;
_10
}

React to state inside the parent

Because :has() accepts pseudo-classes, a parent can respond to the state of its children. This replaces a lot of small JavaScript that toggled classes.


_10
/* highlight the label row when its checkbox is ticked */
_10
.option:has(input:checked) {
_10
background: var(--selected-bg);
_10
}
_10
_10
/* show a warning style while any field in the form is focused and invalid */
_10
form:has(:focus-visible:user-invalid) .hint {
_10
display: block;
_10
}

Select a previous sibling

Combine :has() with the sibling combinators to select an element based on what comes after it.


_10
/* a heading that is immediately followed by a paragraph */
_10
h2:has(+ p) {
_10
margin-bottom: 0.5rem;
_10
}

Style the whole page from deep state

You can attach :has() to body or :root to change global styles, for example stopping the page from scrolling while a modal dialog is open.


_10
body:has(dialog[open]) {
_10
overflow: hidden;
_10
}

Check support if you need a fallback

For older browsers, wrap enhancements in a feature query and keep a sensible default outside it.


_10
@supports selector(:has(a)) {
_10
.card:has(img) {
_10
padding: 0;
_10
}
_10
}

Common pitfalls

#
  • Putting :has() on the child. img:has(.card) looks for a card inside the image. The pseudo-class goes on the element being styled.
  • Nesting :has() inside another :has(). This is not allowed and the rule is ignored.
  • Using pseudo-elements inside it. :has(::before) is invalid.
  • Writing very broad rules such as :root:has(*:hover) on large pages. They work, but every change in the DOM has to be re-checked, so keep the anchor element and the inner selector as specific as you can.
  • Expecting it to raise specificity like a class. :has() takes the specificity of the most specific selector in its argument, which can make it heavier than you expect.
#

Was this resource helpful?