How to select parent element with CSS?

Sometimes, we want to select parent element with CSS.

In this article, we’ll look at how to select parent element with CSS.

How to select parent element with CSS?

To select parent element with CSS, we can use the has pseudo-class.

For instance, we write

li:has(> a.active) {
  /* styles to apply to the li tag */
}

to select the li that has the anchor element that has the active class.

We use > to get the direct children.

Conclusion

To select parent element with CSS, we can use the has pseudo-class.