Understanding CSS :has() Child Selector
The CSS :has() pseudo-class is a powerful tool that allows you to select elements based on their descendants. One of its most useful applications is selecting elements based on their child elements. Let's dive into the world of CSS :has() child selector and explore its capabilities.
What is CSS :has() Child Selector?
The :has() pseudo-class in CSS is used to select elements that have a specific descendant, regardless of its relationship to the selected element. In other words, it allows you to select an element based on its child elements. This can be particularly useful when you want to apply styles to an element only if it contains certain child elements.
Syntax
The syntax for the :has() child selector is straightforward. It follows this pattern:

:has(selector)
Where 'selector' is the CSS selector that matches the desired descendant. For example, :has(p) will select any element that has a <p> element as a descendant.
Selecting Elements with Child Elements
Let's say you want to select all <div> elements that have at least one <p> element as a child. You can achieve this using the :has() child selector like this:
div:has(p) {
/* Styles will be applied here */
}
In this example, the styles within the curly braces will be applied to all <div> elements that have at least one <p> element as a child.

Selecting Elements Based on Multiple Child Elements
The :has() child selector also allows you to select elements based on multiple child elements. For instance, you can select all <div> elements that have both a <p> element and an <img> element as children like this:
div:has(p, img) {
/* Styles will be applied here */
}
In this case, the styles will be applied to <div> elements that have both a <p> element and an <img> element as children.
Selecting Elements Based on Child Element Attributes
You can also select elements based on the attributes of their child elements. For example, you can select all <div> elements that have an <img> element with the 'src' attribute set to 'example.jpg' like this:

div:has(img[src="example.jpg"]) {
/* Styles will be applied here */
}
This can be particularly useful when you want to apply styles to an element based on the attributes of its child elements.
Browser Support
As of now, the :has() pseudo-class is supported in Chrome 91 and later, and in Safari Technology Preview 123 and later. It's not yet supported in Firefox or Edge. However, you can use feature detection to apply fallbacks for browsers that don't support :has().
Use Cases
The :has() child selector has a wide range of use cases. Here are a few examples:
- Styling elements based on their content. For example, you can style a <div> element differently if it contains an image.
- Creating interactive elements. For example, you can show or hide elements based on the presence of certain child elements.
- Optimizing performance. For example, you can lazy-load images only if the element that contains them is visible.
The :has() child selector is a powerful tool that can greatly enhance your CSS capabilities. Whether you're styling elements based on their content, creating interactive elements, or optimizing performance, the :has() child selector has a lot to offer. So, start exploring and see what you can create!





















