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.
Practice
active: variant in Tailwind CSS do?Solution
Step 1: Understand the
Theactive:variant purposeactive:variant targets the moment an element is clicked or pressed, giving immediate feedback.Step 2: Compare with other states
Hover applies on mouse over, focus applies on keyboard focus, and permanent styles are not related toactive:.Final Answer:
Applies styles only while the element is being clicked or pressed -> Option AQuick Check:
active:= clicked/pressed state [OK]
- Confusing active with hover or focus states
- Thinking active styles stay after click
- Using active for keyboard focus feedback
Solution
Step 1: Identify base and active styles
The base background isbg-red-500, and the active state changes it tobg-red-700.Step 2: Check class order and correctness
<button class='bg-red-500 active:bg-red-700'>Click me</button>correctly sets the base and active variant. Other options either reverse colors or misuse classes.Final Answer:
<button class='bg-red-500 active:bg-red-700'>Click me</button> -> Option BQuick Check:
Base color then active variant =<button class='bg-red-500 active:bg-red-700'>Click me</button>[OK]
- Swapping base and active colors
- Using active without base style
- Confusing hover and active variants
<button class='bg-blue-400 active:bg-blue-600 text-white px-4 py-2 rounded'>Press</button>
What background color will the button have while it is being clicked?
Solution
Step 1: Identify base and active background classes
The base background isbg-blue-400. The active variant changes it tobg-blue-600when pressed.Step 2: Determine background during active state
While clicking, theactive:bg-blue-600applies, overriding the base color.Final Answer:
bg-blue-600 -> Option CQuick Check:
Active variant overrides base color when pressed [OK]
- Choosing base color instead of active
- Confusing text color with background
- Ignoring active variant effect
<button class='bg-green-500 active-bg-green-700 text-white'>Click</button>
What is the error?
Solution
Step 1: Check active variant syntax
The correct syntax uses a colon:active:bg-green-700. The code uses a dash instead, which is invalid.Step 2: Verify other classes
Base color and text color are correct. Button supports active variant.Final Answer:
The active variant syntax is incorrect; it should beactive:bg-green-700-> Option DQuick Check:
Use colon for variants like active: [OK]
- Using dash instead of colon for variants
- Assuming text color affects active variant
- Thinking button can't have active styles
Solution
Step 1: Identify active text color change
The link should have base text colortext-gray-800and change totext-purple-600only when pressed, soactive:text-purple-600is correct.Step 2: Add smooth transition for color changes
Usetransition-colorsandduration-300for smooth color transitions on press and release.Final Answer:
<a href='#' class='text-gray-800 active:text-purple-600 transition-colors duration-300'>Link</a> -> Option AQuick Check:
Active variant + transition-colors + duration =<a href='#' class='text-gray-800 active:text-purple-600 transition-colors duration-300'>Link</a>[OK]
- Using hover instead of active for press effect
- Missing transition-colors for smooth change
- Reversing base and active colors
Ready to prove it?
Pass the test and this pattern moves unknown
