Sub topic 9
Hover, focus, and other state variants
This chapter assumes you know basic CSS pseudo-classes such as :hover and :focus, and that you have read the earlier chapters on color and responsive prefixes. It shows how Tailwind applies utilities only in a given state.
The variant prefix model
A variant is a prefix followed by a colon. It makes a utility apply only under a condition. hover:bg-indigo-700 sets the background color only while the pointer is over the element. Tailwind generates a CSS rule whose selector includes the matching pseudo-class, so the base style stays in place until the state applies.
The card button from earlier chapters uses this model:
<button class="mt-4 rounded-lg bg-indigo-600 px-4 py-2 text-white hover:bg-indigo-700 focus:ring-2 focus:ring-indigo-500">
Read more
</button>Here bg-indigo-600 is the default. hover:bg-indigo-700 darkens it on hover. focus:ring-2 and focus:ring-indigo-500 draw a 2px ring in indigo when the button has focus.
Common state variants
| Variant | Applies when |
|---|---|
| `hover:` | The pointer is over the element |
| `focus:` | The element has focus |
| `focus-visible:` | The element has focus and the browser decides a focus indicator is needed, such as keyboard focus |
| `active:` | The element is being pressed |
| `disabled:` | The element is disabled |
| `first:` and `last:` | The element is the first or last child |
| `odd:` and `even:` | The element is an odd or even child |
Prefer focus-visible: for focus rings on buttons and links. With plain focus:, a mouse click also shows the ring, which many designs avoid.
Stacking variants
You can chain variants. Each one must be true for the utility to apply. Order reads left to right as outer to inner.
<button class="bg-indigo-600 text-white hover:bg-indigo-700 active:bg-indigo-800 disabled:cursor-not-allowed disabled:opacity-50 md:hover:bg-indigo-500 dark:hover:bg-indigo-400" disabled>
Save
</button>md:hover:bg-indigo-500 applies on screens at the md breakpoint and wider, and only on hover. dark:hover:bg-indigo-400 applies only in dark mode and on hover. The next chapter covers the dark: variant in detail.
Styling based on a parent or sibling
Sometimes the state belongs to another element. Mark that element with group, then style children with group-hover:.
<a href="/docs" class="group flex items-center gap-2 rounded-lg p-3 hover:bg-slate-100">
<span class="text-slate-700 group-hover:text-indigo-600">Read the docs</span>
<span class="translate-x-0 transition group-hover:translate-x-1">→</span>
</a>Hovering anywhere on the link changes the text color and nudges the arrow. The peer class works the same way for siblings. Use peer on an earlier sibling, and peer-checked: or peer-focus: on a later one.
Smooth changes with transitions
State changes happen instantly by default. Add transition to animate them, and optionally duration-200 to set the length.
<button class="bg-indigo-600 px-4 py-2 text-white transition duration-200 hover:bg-indigo-700">
Hover me
</button>Put the transition on the base element, not on the hover: utility. Then the change animates both into and out of the state.
Common errors
- Hover has no effect on touch screens. Touch devices have no true hover, so do not hide essential information behind
hover:. - Removing the focus outline.
outline-nonewithout a replacement such asfocus-visible:ring-2makes keyboard navigation hard to follow. - Conflicting utilities. If
hover:bg-indigo-700seems ignored, check that no other utility sets the same property with higher priority, such as another variant that matches at the same time. - Wrong parent.
group-hover:does nothing if no ancestor has thegroupclass.
Exercise
A link should show an underline only on hover or keyboard focus, and the underline should appear smoothly. Which classes would you add to the link?
Show answer
Use no-underline hover:underline focus-visible:underline transition. The variants apply the underline in each state. Note that the underline itself is not animatable by transition alone, so for a smooth effect you could instead transition color or use underline decoration-transparent hover:decoration-current transition.