The active variant in Tailwind CSS applies styles when an element is being clicked or pressed by the user, like a button being held down.
Click to reveal answer
beginner
How do you write a Tailwind class to change background color when a button is active?
Use the active: prefix before the class. For example, active:bg-blue-700 changes the background to blue when the button is pressed.
Click to reveal answer
beginner
Why is the active variant useful in web design?
It gives users feedback that their click is recognized by changing the element's style while pressing, improving user experience and accessibility.
Click to reveal answer
intermediate
Can the active variant be combined with other variants in Tailwind CSS?
Yes! You can combine active with others like hover or focus to style elements differently for each interaction state.
Click to reveal answer
beginner
Example: What does this class do? <code>active:scale-95</code>
It makes the element slightly smaller (scaled to 95%) while it is being pressed, giving a subtle 'pressed' effect.
Click to reveal answer
What does the Tailwind class active:bg-red-500 do?
AChanges background to red when the element is focused
BChanges background to red when the mouse hovers
CChanges background to red when the element is clicked
DChanges background to red all the time
✗ Incorrect
The active: prefix applies styles only while the element is being clicked or pressed.
Which user action triggers the active variant styles?
AHovering over the element
BDouble-clicking the element
CFocusing the element with keyboard
DClicking and holding the element
✗ Incorrect
The active variant applies styles when the user clicks and holds the element.
Can you combine active: with hover: in Tailwind CSS?
AYes, you can combine them for different states
BNo, only one variant can be used at a time
COnly if you use custom CSS
DOnly on buttons
✗ Incorrect
Tailwind allows combining variants like hover: and active: to style different interaction states.
What visual effect does active:scale-95 create?
AMakes the element bigger when active
BMakes the element smaller when active
CChanges the element's color when active
DRotates the element when active
✗ Incorrect
Scaling to 95% shrinks the element slightly while it is active (pressed).
Why should you use the active variant on buttons?
ATo improve user feedback during clicks
BTo make buttons bigger permanently
CTo disable buttons
DTo change button text
✗ Incorrect
Using active gives users visual feedback that their click is recognized.
Explain how the active variant works in Tailwind CSS and why it is important for user experience.
Think about how buttons feel when you press them.
You got /4 concepts.
Describe how you would combine active with other variants like hover and focus in Tailwind CSS.
Consider how buttons change on hover, focus, and active states.
You got /4 concepts.
Practice
(1/5)
1. What does the active: variant in Tailwind CSS do?
easy
A. Applies styles only while the element is being clicked or pressed
B. Applies styles when the element is hovered over
C. Applies styles when the element is focused via keyboard
D. Applies styles permanently after the element is clicked once
Solution
Step 1: Understand the active: variant purpose
The active: 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 to active:.
Final Answer:
Applies styles only while the element is being clicked or pressed -> Option A
Quick Check:
active: = clicked/pressed state [OK]
Hint: Remember active means 'pressed down' state only [OK]
Common Mistakes:
Confusing active with hover or focus states
Thinking active styles stay after click
Using active for keyboard focus feedback
2. Which of these is the correct way to apply a red background only while a button is pressed using Tailwind's active variant?
easy
A. <button class='hover:bg-red-700 active:bg-red-500'>Click me</button>
B. <button class='bg-red-500 active:bg-red-700'>Click me</button>
C. <button class='active:bg-red-500 bg-red-700'>Click me</button>
D. <button class='bg-red-700 active:bg-red-700'>Click me</button>
Solution
Step 1: Identify base and active styles
The base background is bg-red-500, and the active state changes it to bg-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 B
Quick Check:
Base color then active variant = <button class='bg-red-500 active:bg-red-700'>Click me</button> [OK]
Hint: Base style first, then active:variant for pressed effect [OK]
C. The text color text-white conflicts with active variant
D. The active variant syntax is incorrect; it should be active:bg-green-700
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 be active:bg-green-700 -> Option D
Quick Check:
Use colon for variants like active: [OK]
Hint: Variants use colon, not dash (active:bg-color) [OK]
Common Mistakes:
Using dash instead of colon for variants
Assuming text color affects active variant
Thinking button can't have active styles
5. You want a link that changes text color to purple only while pressed, and smoothly transitions back after release. Which Tailwind classes achieve this?
hard
A. <a href='#' class='text-gray-800 active:text-purple-600 transition-colors duration-300'>Link</a>
B. <a href='#' class='text-gray-800 hover:text-purple-600 transition duration-300'>Link</a>
C. <a href='#' class='text-purple-600 active:text-gray-800 transition-colors duration-300'>Link</a>
D. <a href='#' class='text-gray-800 active:text-purple-600 duration-300'>Link</a>
Solution
Step 1: Identify active text color change
The link should have base text color text-gray-800 and change to text-purple-600 only when pressed, so active:text-purple-600 is correct.
Step 2: Add smooth transition for color changes
Use transition-colors and duration-300 for 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 A