# :has() The Parent Selector

> Style an element based on what's inside it or what comes after it. The holy grail of CSS selectors.

Subject: CSS | Lesson 38 of 44 | 4 min read
Interactive lesson: https://codewithanimation.in/css/has-parent-selector
Previous: [CSS Nesting](https://codewithanimation.in/css/css-nesting.md) | Next: [Cascade Layers (@layer)](https://codewithanimation.in/css/cascade-layers.md)

## Selecting a parent based on its child

For over 20 years, CSS could only style an element based on its parents (e.g., `.card img` styles the image inside a card). There was no way to style the `.card` itself if it happened to contain an image.

The `:has()` pseudo-class solves this. It acts as a relational selector, letting you apply styles to an element if the selector passed inside the `:has()` parenthesis matches.

It is commonly called the 'parent selector', but it's even more powerful than that—it can look ahead at siblings too.

## Replacing JavaScript class toggling

Before `:has()`, if you had a form group and wanted the label to turn red when the input inside it was invalid, you had to write JavaScript to listen for validation events and attach an `.is-invalid` class to the parent div.

With `:has()`, you can simply write `.form-group:has(input:invalid) { color: red; }`. The browser handles the state automatically, perfectly in sync, with zero JavaScript.

This drastically reduces frontend logic and keeps styling concerns entirely inside your CSS.

## Descendants and Siblings

To style a parent based on a child, just pass the child selector into `:has()`. `article:has(video)` selects any article containing a video.

To style an element based on its state, combine pseudo-classes. `label:has(input:checked)` styles a label differently if its internal checkbox is checked.

You can also use combinators to look at siblings. `h2:has(+ p)` selects an `h2` only if it is immediately followed by a `<p>` tag, letting you adjust margins dynamically.

## Common mistake: Performance and limitations

Because `:has()` requires the browser to look ahead or deep into the DOM tree before deciding how to style the current element, it can be computationally expensive. Avoid using universal selectors with it, like `*:has(.active)`. Always scope it to a specific element.

Additionally, the CSS specification prevents nesting `:has()` inside another `:has()` to prevent infinite loops and massive performance hits.

## Tip: Previous sibling selector

CSS never had a 'previous sibling' selector, only 'next sibling' (`+` or `~`). With `:has()`, you can finally select a previous sibling! `div:has(+ .active)` selects the `div` that comes immediately before the `.active` element.

## Try it: Try it

Interact with the child elements below and watch how the parent container restyles itself instantly using purely CSS.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/css/has-parent-selector

## Check yourself

### Question 1

How do you select a `.card` only if it contains an `<img>` element?

- A. .card:has(img)
- B. .card img
- C. .card < img
- D. img:parent(.card)

Correct answer: A

The `:has()` pseudo-class evaluates the condition inside the parenthesis. If `.card` has a descendant `img`, the `.card` itself is selected.

- Why not B: This selects the `img` element inside the card, not the `.card` itself.
- Why not C: The `<` symbol is not a valid CSS combinator.
- Why not D: There is no `:parent` pseudo-class in CSS.

### Question 2

Which selector correctly targets an `<h1>` that is immediately followed by an `<h2>`?

- A. h1:has(+ h2)
- B. h1 + h2
- C. h1:has(h2)
- D. h1 ~ h2

Correct answer: A

By placing the adjacent sibling combinator `+` inside `:has()`, you check what comes directly after the `<h1>`. If it's an `<h2>`, the `<h1>` is selected.

- Why not B: This selects the `<h2>` that follows the `<h1>`, not the `<h1>` itself.
- Why not C: This selects an `<h1>` that *contains* an `<h2>` as a descendant, not as a sibling.
- Why not D: This selects all `<h2>` elements that come after an `<h1>` as siblings.

### Question 3

Why should you avoid writing `*:has(.active) { border: 1px solid red; }`?

- A. It causes performance issues by forcing the browser to check every single element on the page.
- B. It is invalid CSS and will cause a parse error.
- C. It only selects the `<html>` and `<body>` tags.
- D. The `*` selector is deprecated in modern CSS.

Correct answer: A

`:has()` can be computationally heavy because the browser must evaluate the condition for the element. Doing this for every single element `*` on a large page can significantly slow down rendering.

- Why not B: It is technically valid syntax, just very bad for performance.
- Why not C: It would select *every* ancestor of the `.active` element, all the way up the tree.
- Why not D: The universal selector `*` is still perfectly valid, but dangerous when combined with complex relational selectors.

## Remember this

- The `:has()` pseudo-class lets you select an element based on its descendants or subsequent siblings.
- It eliminates the need for JavaScript when toggling parent classes based on child states (like `:checked` or `:invalid`).
- You can use combinators inside `:has()`, such as `:has(+ p)` to look at the very next sibling.
- Avoid using `:has()` with universal selectors (`*`) to maintain high rendering performance.
