State Variants: Hover, Focus, Active
What you'll learn: How Tailwind's state variant prefixes let you style interactive states inline, no CSS pseudo-classes required.
The Core Idea
In traditional CSS, you write separate rules for :hover, :focus, and :active pseudo-classes. Tailwind brings this logic directly into your className strings using prefix notation: just add hover:, focus:, active:, or disabled: before any utility class.
Think of it like inline conditional formatting: "apply this style, but only when hovering." You're not writing a separate stylesheet rule—you're declaring the condition right where you apply the style.
How It Works
<button className="bg-blue-500 hover:bg-blue-700 focus:ring-2 active:scale-95">
Click Me
</button>
bg-blue-500— default backgroundhover:bg-blue-700— darker blue on hoverfocus:ring-2— add a ring when focused (keyboard navigation)active:scale-95— slightly shrink when pressed
Each prefix automatically applies the corresponding CSS pseudo-class under the hood, but you never leave your JSX.
Common Patterns
// Disabled state
<button
disabled={loading}
className="bg-green-600 hover:bg-green-700 disabled:opacity-50 disabled:cursor-not-allowed"
>
Submit
</button>
// Combined states
<input
className="border-gray-300 focus:border-blue-500 focus:ring-4 focus:ring-blue-200"
/>
Why This Matters
You eliminate context-switching between JSX and CSS files. State styling becomes declarative and co-located with your markup. You can instantly see all interactive states for an element at a glance.
Key Takeaway: Tailwind's hover:, focus:, active:, and disabled: prefixes let you style interactive states directly in className strings, replacing CSS pseudo-classes with inline variant modifiers.