Sub topic 1
Triggering changes with state variants
Prerequisites
This chapter assumes you know basic HTML and CSS, and that you have used Tailwind utility classes such as bg-indigo-600 and px-4. You should also know what a CSS transition is: a rule that animates a property from one value to another instead of switching instantly.
How a state variant works
A state variant is a prefix that applies a utility only while an element is in a given state. hover:bg-indigo-700 sets the background color only while the pointer is over the element. Tailwind compiles it to a rule with a pseudo-class selector, so the browser does the work and no JavaScript is involved.
A state variant changes a value, but it does not animate the change. Without a transition, the browser switches between the two values at once. To animate, you pair the variant with a transition utility on the element's base state.
<button class="rounded bg-indigo-600 px-4 py-2 text-white transition-colors duration-200 ease-out hover:bg-indigo-700">
Save
</button>This button has three parts:
bg-indigo-600is the resting color.hover:bg-indigo-700is the color while hovered.transition-colors duration-200 ease-outanimates the change over 200 milliseconds.
The transition utilities sit on the base element, not behind hover:. Because of that, the animation runs in both directions: into the hover state and back out of it.
The common state variants
Three variants cover most interactive controls.
| Variant | Applies when | Typical use |
|---|---|---|
| `hover:` | The pointer is over the element | Color or shadow feedback on buttons and cards |
| `focus:` | The element has keyboard or pointer focus | Highlighting a text input |
| `active:` | The element is being pressed | A small "pressed" effect on buttons |
Other variants, such as focus-visible:, disabled: and checked:, follow the same pattern.
You can stack several variants on one element. The next example gives a button all three states.
<button class="rounded bg-indigo-600 px-4 py-2 text-white transition-colors duration-200 ease-out hover:bg-indigo-700 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-400 active:bg-indigo-800">
Save
</button>The active: color is darker than the hover: color. While a user presses the button, both states are true. Tailwind orders active: after hover: in the generated CSS, so active: wins.
Combining variants
You can chain variants, and they apply only when every condition is true. Read them from left to right.
<input class="rounded border border-slate-300 px-3 py-2 transition-colors duration-150 hover:border-slate-400 focus:border-indigo-600 focus:outline-none disabled:opacity-50 md:hover:border-indigo-400">md:hover:border-indigo-400 applies only on screens at the md breakpoint or wider, and only while hovered. This is useful because touch devices have no real hover. Many teams limit hover effects to larger screens for that reason.
Common errors
Most problems with state variants come from a small set of mistakes.
- No animation, only a jump. You added
hover:but notransition-*utility. The change happens, but instantly. - The transition only runs one way. You put the transition utility inside the variant, as in
hover:transition-colors. Moving into the state animates, but leaving it snaps back, because the base element has no transition. - The wrong property is transitioned.
transition-colorscovers color properties only. If the hover state also changesbox-shadowortransform, usetransition(the default set) ortransition-all, as the next chapter explains. - Variant order surprises. If
hover:andactive:set the same property, the one generated later wins. Check that the pressed state looks different from the hover state. - Focus styles removed.
focus:outline-nonehides the default focus ring. Always replace it with another visible indicator, such as a border color orfocus-visible:outline.
How the pieces fit together
The variant decides what changes. The transition utilities decide how it changes. Keeping those two jobs separate makes each easier to debug.
Exercise
A card has the classes rounded bg-white p-4 hover:bg-slate-100. The background changes instantly on hover. What do you add so it fades over 300 milliseconds in both directions, and why must it not go behind the hover: prefix?
Show answer
Add transition-colors duration-300 to the base classes. The transition properties must be on the base element so they apply both when entering and when leaving the hover state. If they sat behind hover:, they would exist only while hovered, and the return to the resting color would snap instantly.
Exercise
You want a button to turn bg-indigo-800 while pressed, and you want a visible outline for keyboard users only. Which two variants do you use?
Show answer
Use active:bg-indigo-800 for the pressed state and focus-visible:outline (with outline color and offset utilities as needed) for keyboard focus.