CSS Portal

:has() CSS Pseudo Class

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
:has()
CSS Pseudo ClassThis pseudo class is supported in some modern browsers, but not all.
Updated 31st December 2025

Description

The :has() pseudo-class is a powerful CSS relational selector that allows you to style elements based on the presence of certain descendants or conditions within them. Essentially, it enables a parent element to be selected if it contains a child element that matches a specified selector. This capability goes beyond traditional CSS selectors, which typically flow from parent to child, allowing for a more context-aware and dynamic approach to styling elements. For instance, you can highlight a div if it contains an img without needing JavaScript.

One of the most common use cases of :has() is conditional styling based on form interactions. For example, you can style a form when it contains an input that is currently focused:

form:has(input:focus) {
    border: 2px solid blue;
    background-color: #f0f8ff;
}

Here, any form that contains a focused input will have a highlighted border and subtle background color. This selector is also extremely useful for styling lists dynamically, such as highlighting a li item when it contains a checked input of type checkbox:

li:has(input[type="checkbox"]:checked) {
    font-weight: bold;
    text-decoration: underline;
}

The :has() pseudo-class can also be combined with other selectors, pseudo-classes, and pseudo-elements to create complex, context-sensitive designs. However, since it can involve checking child elements across potentially large DOM trees, it may have performance implications if overused, especially in pages with many elements. Its syntax is straightforward: element:has(selector), where selector can be any valid CSS selector or a comma-separated list of selectors.

Overall, :has() provides designers and developers with a method to express styles that depend on internal document structure, previously only possible with JavaScript. It marks a significant evolution in CSS by introducing a “parent-aware” mechanism for styling content.

Syntax

:has(<relative-selector-list>) {
  /* ... */
}

Example

<div class="card">
<h2>Just Text</h2>
<p>This card does not have an image, so it stays basic.</p>
</div>

<div class="card">
<img src="images/2.png" alt="Placeholder">
<h2>Image Card</h2>
<p>This card has an image, so the parent container changes!</p>
</div>
/* Default card styling */
.card {
border: 1px solid #ccc;
padding: 20px;
margin: 10px;
border-radius: 8px;
font-family: sans-serif;
transition: all 0.3s ease;
}

/* The :has() selector */
/* This targets the .card DIV itself */
.card:has(img) {
background-color: #f0f9ff;
border-color: #007bff;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

/* You can also style children based on the parent's state */
.card:has(img) h2 {
color: #007bff;
}

img {
max-width: 100%;
border-radius: 4px;
}
Open in editor

Browser Support

The following information will show you the current browser support for the CSS :has() pseudo class. Hover over a browser icon to see the version that first introduced support for this CSS pseudo class.

This pseudo class is supported in some modern browsers, but not all.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee