---
title: "How do I select a parent element in CSS?"
description: "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."
url: https://www.danny.engineering/answer/css/how-do-i-select-a-parent-element-in-css
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:24:00.000Z
tags: ["CSS"]
---

# How do I select a parent element in CSS?

## 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.


```css
/* style a card that contains an image */
.card:has(img) {
  padding: 0;
}

/* style a field whose input is invalid */
.field:has(input:user-invalid) {
  border-color: crimson;
}
```


## 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.


```css
/* any list item that contains a link */
li:has(a) {
  list-style: none;
}

/* only sections whose direct child is an h2 */
section:has(> h2) {
  margin-block: 3rem;
}
```


### 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.


```css
/* highlight the label row when its checkbox is ticked */
.option:has(input:checked) {
  background: var(--selected-bg);
}

/* show a warning style while any field in the form is focused and invalid */
form:has(:focus-visible:user-invalid) .hint {
  display: block;
}
```


### Select a previous sibling


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


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


### 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.


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


### Check support if you need a fallback


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


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


## 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.

## Related reading

- [MDN: :has()](https://developer.mozilla.org/en-US/docs/Web/CSS/:has)
