Sub topic 1
Triggering changes with state variants
A CSS transition needs two things: a change in state, and a rule that says how to animate that change. Tailwind's state variants supply the first. This chapter covers the variants that trigger style changes (hover:, focus-visible:, active:, disabled: and a few others) and how they work with transition utilities. The later chapters on properties, duration and easing build on this.
Prerequisites
This chapter assumes you already know:
- Basic HTML and CSS, including what a pseudo-class such as
:hoveris. - Tailwind utility classes: you can read
px-4 py-2 bg-indigo-600without looking it up. - The idea of a CSS transition: a browser can interpolate between two values of a property over time instead of switching instantly.
You do not need to know keyframes yet. They come in later chapters.
What a state variant does
A variant is a prefix that makes a utility apply only under a condition. hover:bg-indigo-700 means "set the background to indigo-700 while the pointer is over this element". Tailwind generates a CSS rule that uses the matching pseudo-class, so the browser tracks the state for you and you write no JavaScript.
A variant by itself does not animate anything. The browser switches styles the moment the state changes. Smoothness comes from a transition utility on the same element. The variant decides when the style changes, and the transition decides how the change plays out.
<button class="rounded bg-indigo-600 px-4 py-2 text-white transition-colors duration-200 ease-out hover:bg-indigo-700">
Save
</button>Here hover:bg-indigo-700 changes the color. transition-colors says "animate color-related properties". duration-200 sets 200 milliseconds, and ease-out sets the easing. Remove the three transition classes and the button still changes color on hover, but it snaps.
The diagram shows the states a button typically moves through.
Each arrow is a moment when different variants start or stop matching. The transition runs on every one of those arrows.
Hover, focus and active
Three variants cover most pointer and keyboard interaction.
| Variant | Applies when | Typical use |
|---|---|---|
| `hover:` | The pointer is over the element | Highlight a button, link or card |
| `focus:` | The element has focus by any means | Text inputs |
| `focus-visible:` | The element has focus and the browser decides a focus indicator should show | Buttons and links |
| `active:` | The element is being pressed | Press feedback |
A button that handles all of them:
<button class="rounded bg-indigo-600 px-4 py-2 text-white
transition-colors duration-200 ease-out
hover:bg-indigo-700
active:bg-indigo-800
focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600">
Save
</button>Variants that set the same property follow CSS order. Tailwind emits active: after hover:, so when the pointer is over the button and pressed, the active: color wins.
focus versus focus-visible
focus: matches whenever the element has focus, including after a mouse click. On a button, that often leaves a ring behind after a click, which many designers dislike. focus-visible: matches only when the browser thinks the user needs to see where focus is, usually during keyboard navigation. Use focus-visible: for buttons and links. Use focus: for text inputs, where the user needs to see the active field however they got there.
Never remove the focus indicator without replacing it. outline-none with no focus-visible: style makes the page hard to use by keyboard.
focus-within
focus-within: styles a parent when any descendant has focus. It suits a search box wrapper that should glow while the inner input is focused:
<div class="rounded border border-slate-300 px-3 py-2 transition-colors duration-150 focus-within:border-indigo-600">
<input type="text" class="w-full outline-none" placeholder="Search">
</div>The input removes its own outline, but the wrapper's border change provides the focus indicator.
Put the transition on the base state
The most common mistake is attaching the transition classes to the variant:
<!-- Animates in, snaps back out -->
<button class="bg-indigo-600 hover:bg-indigo-700 hover:transition-colors hover:duration-200">
Save
</button>A transition is read from the element's style at the time the change happens. When the pointer leaves, the hover: rules stop matching, so the transition declaration disappears too. The return trip has no transition and snaps. Put the transition utilities on the element without any variant, as in the earlier examples, and both directions animate.
You can still set different timing for each direction on purpose. Base values apply when leaving the state, and variant values apply when entering it:
<button class="bg-indigo-600 transition-colors duration-500 hover:bg-indigo-700 hover:duration-100">
Save
</button>This button reacts quickly on hover and fades back slowly. Use this sparingly. Most interfaces feel more consistent with one duration.
Disabled and attribute-based states
disabled: matches form controls that have the disabled attribute. Pair it with a pointer cue so users understand the control is inactive:
<button class="rounded bg-indigo-600 px-4 py-2 text-white
transition-colors duration-200
hover:bg-indigo-700
disabled:cursor-not-allowed disabled:opacity-50 disabled:hover:bg-indigo-600"
disabled>
Save
</button>Two details are worth noting:
disabled:hover:bg-indigo-600stops the hover color from applying to a disabled button. Without it, the disabled button still darkens when hovered, because both variants match and the later one in the generated CSS wins.transition-colorsdoes not includeopacity. The disabled fade therefore happens instantly. To animate it too, usetransition(the default set, which includes opacity) ortransition-[color,background-color,opacity]. The next chapter covers choosing properties.
Tailwind also exposes ARIA and data attributes as variants, which lets your markup state drive the style:
<button aria-expanded="false"
class="rounded px-3 py-2 transition-colors duration-150 aria-expanded:bg-slate-200 aria-expanded:font-semibold">
Menu
</button>When script sets aria-expanded="true", the variant matches and the background transitions. For custom states, data-[state=open]:bg-slate-200 matches data-state="open". Driving styles from attributes keeps visual state and accessibility state in sync, because one attribute serves both.
Stacking variants
Variants combine by chaining. They apply from left to right, and the style applies only when every condition holds:
md:hover:bg-indigo-700applies on screens at themdbreakpoint or wider, while hovered.dark:hover:bg-indigo-400applies in dark mode, while hovered.focus-visible:hover:ring-2applies only when the element is both keyboard-focused and hovered.
Stacking does not change how transitions work. The transition still belongs on the base element.
Hover on touch devices
Touch screens have no real hover. In older browsers a tap can leave the element stuck in its hover style until you tap elsewhere. In Tailwind v4, hover: is wrapped in a @media (hover: hover) query, so it applies only on devices whose primary input can hover. In Tailwind v3 the variant applies on every device unless you configure it otherwise.
In either version, do not rely on hover to reveal essential information. A touch user may never see it. Use hover for polish and put the real content or action where every input can reach it, for example behind focus-visible: or a click.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| Color changes instantly | No transition utility on the element | Add `transition-colors` (or `transition`) and a `duration-*` |
| Animates in, snaps out | Transition classes only on the `hover:` variant | Move them to the base element |
| Ring remains after a mouse click | `focus:` used on a button | Use `focus-visible:` |
| Disabled button still reacts to hover | `hover:` also matches disabled controls | Add `disabled:hover:*` to reset it |
| Opacity or shadow change is abrupt | `transition-colors` does not cover that property | Use `transition` or an arbitrary property list |
| No animation at all | A required duration is missing in setups where you set `transition-[...]` without `duration-*` | Check the computed `transition-duration` in DevTools |
When a transition misbehaves, open DevTools, inspect the element, and read the computed transition-property and transition-duration. If the property you are changing is not listed, or the duration is 0s, the browser has nothing to animate.
Exercise
A button has class="bg-indigo-600 hover:bg-indigo-700 hover:transition-colors hover:duration-300". It darkens smoothly when you hover, but snaps back to the original color when the pointer leaves. Why does this happen, and how do you fix it?
Show answer
The transition declaration exists only while the hover: rules match. When the pointer leaves, those rules stop applying, so the return change has no transition. Move the utilities to the base element: class="bg-indigo-600 transition-colors duration-300 hover:bg-indigo-700". Both directions then animate.
Exercise
Write the classes for a submit button that fades its background over 150 ms on hover, shows a 2-pixel indigo outline only for keyboard focus, and, when disabled, has 50% opacity, a not-allowed cursor and no hover color change. Which transition utility must you choose if you also want the opacity change to animate?
Show answer
One answer is bg-indigo-600 transition duration-150 hover:bg-indigo-700 focus-visible:outline-2 focus-visible:outline-indigo-600 disabled:cursor-not-allowed disabled:opacity-50 disabled:hover:bg-indigo-600. Use transition (the default property set, which includes opacity) rather than transition-colors, because transition-colors does not cover opacity.