DEV Community

Ankit Verma
Ankit Verma

Posted on • Edited on

Is there a CSS parent selector?

li:has(> a.active) → Selects any li that directly contains an a element with the class "active".
The parent li will turn red if it has an active link inside it.

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>CSS :has() Example</title> <style> /* Change text color of <li> if it contains an <a> with class "active" */ li:has(> a.active) { color: red; font-weight: bold; } </style> </head> <body> <h1>CSS :has() Parent Selector Example</h1> <p>The list item containing an active link will turn red.</p> <ul> <li>List Item 1</li> <li> List Item 2 <a href="#" class="active">Active Link</a> </li> <li> List Item 3 <a href="#">Normal Link</a> </li> </ul> </body> </html> 
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

you should enable syntax highlighting on your code snippets so they're easier to read:

Image description

will turn into

<b>bold text</b> 
Enter fullscreen mode Exit fullscreen mode
Collapse
 
ankitvermaonline profile image
Ankit Verma

Thank you