Introduction
Arbitrary variants let you apply styles based on custom CSS selectors. This helps you style elements in special ways that Tailwind's default options don't cover.
[&<custom-selector>]:<utility>
[&:hover]:bg-blue-500
[&>p]:text-red-600
[&[data-active="true"]]:font-bold[&.custom-class]:underline
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Arbitrary Variants Example</title> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="p-6"> <button class="[&:hover]:bg-green-300 border border-gray-400 px-4 py-2 rounded"> Hover me </button> <div class="mt-4"> <p class="[&:first-letter]:text-4xl [&:hover]:text-purple-700"> This paragraph uses arbitrary variants to style its first letter and text color on hover. </p> </div> </body> </html>
<button class="[&:focus-visible]:ring-2 [&:focus-visible]:ring-blue-600">Click me</button>
<div class="[&hover]:bg-red-500">Alert</div>
span inside a div only when the div is hovered, using Tailwind arbitrary variants. Which class should you add to the div?span child only when the div is hovered, so the hover is on the parent.Pass the test and this pattern moves unknown