Sub topic 8
Hover and focus states
This chapter covers how to change colors, borders, and shadows when a user interacts with an element. It assumes you can apply the color, border, and shadow utilities from the earlier chapters, and that you know what the :hover and :focus pseudo-classes do in plain CSS.
How state variants work
A variant is a prefix that limits a utility to a condition. Writing hover:bg-indigo-500 produces a rule that applies bg-indigo-500 only while the pointer is over the element. The base class, such as bg-indigo-600, stays as the default and the variant overrides it in that state.
Any color, border, or shadow utility can take a variant prefix. Common examples:
hover:text-indigo-600changes text color.hover:border-slate-400changes border color.hover:shadow-lgincreases shadow size.focus:border-indigo-500changes the border of a focused input.
Tailwind has no special "state" system. Each variant maps to a CSS pseudo-class, so what you already know about pseudo-classes carries over.
Hover
The simplest use is a lighter or darker shade on hover. Moving one or two steps along the shade scale is usually enough.
<button class="rounded-md bg-indigo-600 px-4 py-2 text-white hover:bg-indigo-500">
Save changes
</button>Hover works on borders and shadows too. A common pattern for clickable cards is to darken the border and raise the shadow together:
<a
href="#"
class="block rounded-xl border border-slate-200 bg-white p-6 shadow-sm transition hover:border-indigo-300 hover:shadow-lg"
>
Open the report
</a>The transition class tells the browser to animate the change over a short default duration. Without it the border and shadow snap instantly. Use transition-colors if only colors change, and transition-shadow if only the shadow changes. Narrower transitions avoid animating properties you did not mean to animate.
Focus, focus-visible, and focus-within
Three variants cover focus, and they behave differently.
| Variant | Applies when | Typical use |
|---|---|---|
| `focus:` | The element has focus, by any means | Text inputs and textareas |
| `focus-visible:` | The browser decides a focus indicator should show, usually keyboard focus | Buttons and links |
| `focus-within:` | The element or any descendant has focus | Wrapper around an input and its icon |
The difference between focus: and focus-visible: matters for buttons. A mouse click focuses a button, so focus: styles would appear after every click. Browsers apply :focus-visible to a button only for keyboard navigation, so the indicator shows when it helps and stays hidden when it does not. Text inputs match :focus-visible in all cases, so either variant works there.
<button
class="rounded-md bg-indigo-600 px-4 py-2 text-white hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
>
Save changes
</button>Here the outline utilities draw an indigo outline separated from the button by 2 pixels. The outline class sets the outline style, outline-2 sets the width, outline-offset-2 adds the gap, and outline-indigo-600 sets the color. Rings and outlines get their own chapter, so this one only uses them as examples of variant-prefixed utilities.
For an input, change the border color on focus and add a visible indicator:
<input
type="text"
class="rounded-md border border-slate-300 px-3 py-2 focus:border-indigo-500 focus:outline-none focus:ring-2 focus:ring-indigo-500"
/>focus:outline-none removes the default outline, so you must supply a replacement. The ring does that here. Removing the outline without a replacement leaves keyboard users with no indication of where they are.
focus-within: styles a container when something inside it is focused:
<label
class="flex items-center gap-2 rounded-md border border-slate-300 px-3 py-2 focus-within:border-indigo-500 focus-within:shadow-md"
>
<span class="text-slate-500">Search</span>
<input type="text" class="flex-1 outline-none" />
</label>The border and shadow belong to the label, but the input's focus triggers them. In this example the input has its own outline-none, which is acceptable because the wrapper provides the visible indicator.
Active and disabled
Two more states pair naturally with hover and focus.
active:applies while the element is being pressed. A darker shade such asactive:bg-indigo-700gives immediate feedback.disabled:applies to elements with thedisabledattribute. Typical styles are reduced opacity and anot-allowedcursor.
<button
class="rounded-md bg-indigo-600 px-4 py-2 text-white transition-colors hover:bg-indigo-500 active:bg-indigo-700 disabled:cursor-not-allowed disabled:opacity-50"
disabled
>
Submit
</button>This button still has the hover rule in the stylesheet. A disabled button can match :hover, so the pointer may lighten it even though it cannot be used. Two fixes work. You can add disabled:hover:bg-indigo-600 to cancel the change, or you can use enabled:hover:bg-indigo-500, so the hover style applies only to enabled buttons. The enabled: variant needs Tailwind v3.1 or later.
Stacking variants
Variants can be chained. Each prefix adds a condition, and all of them must be true:
disabled:hover:bg-indigo-600applies when disabled and hovered.dark:hover:bg-slate-700applies in dark mode and on hover. Dark mode is covered in its own chapter.
Write the variant prefixes in front of the utility, in any order that reads well. Tailwind generates the CSS in a fixed internal order, so you do not control precedence by moving classes around in the class attribute.
Styling children from a parent state
Sometimes the element the pointer touches is not the element you want to change. Mark the parent with group, then use group-hover: on the children:
<a
href="#"
class="group block rounded-xl border border-slate-200 bg-white p-6 transition hover:shadow-lg"
>
<h3 class="font-semibold text-slate-900 group-hover:text-indigo-600">Billing</h3>
<p class="mt-1 text-slate-600 group-hover:text-slate-900">Invoices and payment methods</p>
</a>Hovering anywhere on the card changes the title and description colors. There is a matching group-focus-visible: for keyboard users. If your hover effect carries information, give keyboard users the same effect.
Common errors
- Hover styles with no transition. The change works but looks abrupt. Add
transition-colorsortransition. focus:outline-nonewith no replacement. Keyboard users lose their focus indicator. Always pair it with a ring, border change, or outline.- Using
focus:on buttons and links. The style appears after every mouse click. Preferfocus-visible:. - Building class names dynamically. A string like `
hover:bg-${color}-500` is invisible to Tailwind's scanner, so the CSS is never generated. Write the full class names in your source, for example by mapping each option to a complete string. - Hover as the only cue. Touch devices have no hover. Tailwind v4 applies
hover:only on devices that support hover. In v3 it applies on tap and may stay stuck after a tap unless you enable thehoverOnlyWhenSupportedoption. Do not hide essential information behind hover. - Border width shifting on hover. Changing
bordertoborder-2on hover resizes the element by a pixel and moves its neighbors. Change the border color instead, or keep the width constant and add a ring.
Exercise
A button has the classes bg-emerald-600 text-white hover:bg-emerald-500 disabled:opacity-50 and the disabled attribute, yet it still lightens when you hover it. Why does this happen, and how do you change the classes so a disabled button keeps its base color? Also say which variant you would use for its keyboard focus indicator, and why.
Show answer
A disabled button still matches the :hover pseudo-class, so hover:bg-emerald-500 applies. Fix it by adding disabled:hover:bg-emerald-600, which restores the base color when the button is both disabled and hovered, or by replacing hover:bg-emerald-500 with enabled:hover:bg-emerald-500 so the hover rule never applies to disabled buttons. For focus, use focus-visible: (for example focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-emerald-600). It shows the indicator for keyboard navigation but not after a mouse click, unlike focus:.