Styling selected text with Tailwind CSS

Updated: June 5, 2022 By: Pennywise Post a comment

In Tailwind CSS, you can control the background color and text color of selected text by using the selection modifier:

<p class="selection:text-{color} selection:bg-{color}>Some text</p>

Complete Example

Preview:

The full source code:

<!doctype html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://cdn.tailwindcss.com"></script>
    <title>KindaCode.com</title>
</head>

<body class="p-40 bg-purple-700">
    <h2 class="text-4xl text-white selection:bg-yellow-500 selection:text-black">
        Welcome to KindaCode.com
    </h2>
    <p class="mt-10 text-white selection:text-transparent">
        Selected words will become invisible. Selected words will become invisible. Selected words will become
        invisible.
    </p>
</body>

</html>

That’s it. Further reading:

You can also check out our CSS category page for the latest tutorials and examples.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Related Articles