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

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

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

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay