The active variant helps you style elements when they are being clicked or pressed. It makes buttons or links show a different look while you hold the mouse or tap on them.
Active variant in Tailwind
active:<utility>
Use active: before any Tailwind utility class to apply styles only when the element is active (clicked or pressed).
This works for buttons, links, and other clickable elements.
<button class="bg-blue-500 active:bg-blue-700 text-white font-bold py-2 px-4 rounded">Click me</button><a href="#" class="text-gray-700 active:text-red-500">Press me</a>
<button class="border border-gray-400 active:border-black">Press border</button>This page shows a green button. When you click and hold it, the green color becomes darker. This uses the active: variant to change the background color only while the button is pressed.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Active Variant Example</title> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="flex items-center justify-center min-h-screen bg-gray-100"> <button class="bg-green-500 active:bg-green-700 text-white font-semibold py-3 px-6 rounded shadow-lg transition-colors duration-200"> Press Me </button> </body> </html>
The active variant only applies while the element is being pressed or clicked, not after.
It works well with transition classes to make the color change smooth.
Remember to use semantic HTML elements like <button> for better accessibility.
The active: variant styles elements only while they are clicked or pressed.
Use it to give users visual feedback on buttons and links.
Combine with transitions for smooth effects and use semantic HTML for accessibility.