Skip to main content

How to Style a Parent Element with :has() in CSS

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

:has() styles an element because of what's inside it. li:has(input:checked) matches a list item that holds a ticked checkbox, so a to-do item can strike itself through when it's ticked, with no JavaScript.

Syntax​

Syntax
<element>:has(<inner selector>) {
<declarations>
}
PartWhat it does
<element>The element to style, such as li.
:has(<inner selector>)Keeps only the elements that contain an element matching <inner selector>, such as input:checked, a ticked checkbox. It goes straight after <element>, with no space.
<declarations>The styles the matching elements get.

MDN's :has() page describes it as a way of selecting a parent element with respect to the elements inside it.

The Example​

The example is a to-do list of three items, each with a checkbox. When an item's box is ticked, the item turns grey and gets a line through it.

Step 1: Write the Code​

Create a file named index.html in a text editor, such as Visual Studio Code:

index.html, an HTML page with a style element holding a rule for li elements that have a checked input, setting the color 888888 and a line-through text decoration, and a list of three items, each with a checkbox in a label.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>:has()</title>
<style>
li:has(input:checked) {
color: #888888;
text-decoration: line-through;
}
</style>
</head>
<body>
<ul>
<li><label><input type="checkbox"> Buy milk</label></li>
<li><label><input type="checkbox"> Call the bank</label></li>
<li><label><input type="checkbox"> Water the plants</label></li>
</ul>
</body>
</html>

li:has(input:checked) matches an li that contains a ticked checkbox. :checked is the state of a ticked checkbox or a selected radio button.

Step 2: Open It in a Browser​

Double-click index.html. It opens in your browser:

The page in Chromium: a list of Buy milk, Call the bank and Water the plants, each with an empty checkbox, all in black.

No box is ticked, so no item matches the rule yet. The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.

Step 3: Tick an Item​

Click Buy milk:

The page in Chromium: Buy milk ticked, grey and struck through, with a grey bullet, and the other two items unchanged.

What Happened​

  • Buy milk is grey and struck through. Once its checkbox was ticked, its li matched li:has(input:checked) and got the color rgb(136, 136, 136), which is #888888, and a line through the text.
  • Its bullet turned grey too, because the rule styles the whole li.
  • The other items didn't change: their checkboxes aren't ticked.
  • Unticking the box took the line off again.

Common Mistakes​

MistakeWhat happensDo this instead
A space before :has: li :has(input:checked)The space means "an element inside the li that holds a ticked checkbox", so the label was styled instead of the item: the text was struck through, but the bullet stayed blackWrite :has straight after the element name

Troubleshooting​

SymptomLikely causeFix
The text changes but the bullet doesn'tThere's a space before :has, so the rule styles an element inside the itemRemove the space
Nothing changes when the box is tickedThe inner selector doesn't match the checkboxCheck it reads input:checked

FAQ​

Does :has() work in every browser?​

MDN marks it as widely available: it has worked across browsers since December 2023.

Does it need JavaScript?​

No. The browser applies the rule as soon as the checkbox is ticked, and removes it when it's unticked.

Conclusion​

Write the element to style, then :has() with what it must contain. li:has(input:checked) lets a list item react to its own checkbox, with CSS alone.

Additional Resources​