How to Style a Parent Element with :has() in CSS
: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
<element>:has(<inner selector>) {
<declarations>
}
| Part | What 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:

<!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:

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:

What Happened
- Buy milk is grey and struck through. Once its checkbox was ticked, its
limatchedli:has(input:checked)and got the colorrgb(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
| Mistake | What happens | Do 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 black | Write :has straight after the element name |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| The text changes but the bullet doesn't | There's a space before :has, so the rule styles an element inside the item | Remove the space |
| Nothing changes when the box is ticked | The inner selector doesn't match the checkbox | Check 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.
