:has selector in CSS opens up a world of new possibilities. Now that it's landed in Firefox 121, it's supported in all modern browsers.
In this blog, we're diving into some examples to learn all about
:has in a practical way. Join me in exploring the power of this CSS selector!
1. Parent selector
The primary use of the
:has() selector is as a parent selector. It helps check for the existence of an element within its parent.
For instance, consider a scenario where you want to verify the existence of an icon within a button:
Or, if you wish to append a dropdown arrow to a navbar item containing a submenu, it can be achieved like this:
We're searching for a list item (
<li>) that contains a list (
<ul>). Once we find it, we'll pick the link (
<a>) inside and add a plus sign to it using a pseudo element. Here is a working demo for this example:
:has() selector operates just like any other in your selector chain. You can include anything before, after, or within it. For example, targeting the
.card element when it contains a
<img> followed immediately by a paragraph:
This allows us to create an adaptive layout for our cards based on their children:
You can also provide a list of elements separated by commas inside
:has(), checking their presence within the parent element:
This ensures the
article has at least one of those children—either an
<iframe> or a
Additionally, you can combine
:not() for more complex scenarios:
As you can see, one can get very creative with this selector but remember, you can't nest
:has() selectors, however, you can chain them:
And finally, it's possible to check the state of child elements, to do things like form validation:
You can check out the following demo for this example:
2. Previous sibling selector
:has() selector isn't limited to targeting parents; it can also select previous siblings.
For instance, you can style a label based on the state of its immediate sibling, such as a checkbox:
And in your CSS:
This CSS selector targets a
<label> element that immediately follows a checked
I added a working demo on CodePen for this example too:
We are not limited to selecting just the pervious sibling, we can target all the previous siblings as well. Consider a scenario with breadcrumb separators. In such an example, we don't want any separator after the last item in the list, so we can do this using
:has() and a saubsequent-sibling combinator (~) to do the magic:
By the way, I learned this example from Eric Meyer.
.breadcrumb-item elements having a
.current element as their next siblings, not necessarily the immediate one.
But, what if we didn't use the
.current class and wanted to find elements based on HTML tags only? You can achieve this using
We're selecting list items that have a list item without an
<a> tag as their next sibling.
For the final example in this category, let's break down how to achieve the following effect using the same technique we've employed, using
:has() as a selector for previous sibling.
I first saw this technique on Chris Coyier's CodePen.
The idea is to select two elements after and two elements before the one that's being hovered and add the transform effect to them.
If you want see another cool and practical example for this use case, check out Stephanie's star rating component on CodePen.
3. Quantity queries
:has() selector introduces the capability to perform quantity queries in CSS, allowing you to style a parent element based on the number of its children.
Here are some examples I swiped from Bramus' awesome blog post on the subject.
I saw a practical example for this on Twitter. Suppose you've set a
max-height for a table but want to remove that limitation when the table has more data, specifically when it reaches 5 rows:
Here is the demo if you want to play around:
This example shows how you can change styles dynamically depending on how much content is inside an element.
4. Anywhere selector
:has() selector, when used with top-level elements such as
body, or component roots, opens up a wide range of possibilities by allowing you to apply styles based on specific conditions within those elements.
For instance, you can switch themes based on a
<select> element that exists somewhere in the document:
In simple terms, we check if our body element has an
option element checked with the value "dark." Then, we update our color variables. This can also be done for light and high contrast modes. You can see it in action in the following demo:
You can use the same technique to alter a layout as well. For instance, suppose you're looking for an radio
input with a checked "list" value within the body and applying a specific style to a
card-list somewhere in the DOM:
Feel free to give it a try yourself:
Also, I recently came across another practical example in Rob Bowen's article. It involved locking the scroll when a modal is open. By checking if the body contains a modal as a child, you can change the body's overflow property.
5. All-but-me selector
The all-but-me selector, made possible by
:has(), helps choose all elements except the one being interacted with. For instance, when hovering over a child, you can select all its siblings except the one being hovered on. Check out this demo to see what we're dealing with here:
Here's the selector:
This selector reads as follows: when the container has a child card hovered, select all the cards that aren’t hovered.
And that wraps up our last use case and example here. I've added all these examples to a collection on my CodePen account. I plan to include more examples in the future and keep the collection updated.
Check out the YouTube video if you want to see all of this in action:
It's important to be careful not to overly depend on selectors like
:has. Although powerful, excessive use might cause you to overlook simpler selector options. Striking a balance is crucial for crafting easy-to-maintain code!