Sub topic 8
Hover and focus states
This chapter assumes you know basic HTML and CSS, you have a Tailwind CSS project or playground, and you have met the idea of utility classes. Earlier chapters covered color utilities and borders. Here you change those styles when a user hovers over or focuses an element.
How state variants work
A variant is a prefix that limits a utility to a condition. hover:bg-indigo-500 applies bg-indigo-500 only while the pointer is over the element. focus:border-indigo-500 applies only while the element has focus.
Variants work with any color, border, or shadow utility. You write the base style first and then add the state version.
<button class="rounded-md bg-indigo-600 px-4 py-2 text-white hover:bg-indigo-500">
Save changes
</button>The button uses bg-indigo-600 normally. On hover, the lighter bg-indigo-500 replaces it. Picking a neighboring shade gives a clear but subtle change.
Common state variants
| Variant | Applies when |
|---|---|
| `hover:` | The pointer is over the element |
| `focus:` | The element has focus, by click or keyboard |
| `focus-visible:` | The browser decides focus should be shown, usually for keyboard users |
| `active:` | The element is being pressed |
Use focus-visible: on buttons and links when you don't want a ring to appear after a mouse click. Use focus: on text inputs, where a visible focus style is useful for every kind of user.
Changing borders and shadows
Variants are not limited to background color. You can change a border color, a shadow size, or a shadow color.
<div class="rounded-xl border border-slate-200 bg-white p-6 shadow-md hover:border-indigo-400 hover:shadow-lg">
Hover over this card
</div>The border shifts to indigo and the shadow grows from shadow-md to shadow-lg. To make the change smooth, add transition, which animates the changed properties.
Focus styles on form fields
Browsers draw a default outline on focused fields. If you remove it, you must replace it with something visible.
<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. focus:ring-2 adds a two-pixel ring, and focus:ring-indigo-500 sets its color. The border color also changes, so the focused state is clear. Rings are covered in detail in the next chapter.
Stacking variants
You can chain variants by joining them with colons. Each prefix must be true for the utility to apply.
<button class="bg-white text-slate-900 hover:bg-slate-100 focus-visible:ring-2 focus-visible:ring-indigo-500 active:bg-slate-200">
Menu
</button>Here the button reacts to hover, keyboard focus, and pressing, each with its own style. Combining a state with dark mode follows the same pattern, as in dark:hover:bg-slate-700. The dark mode chapter covers this.
Common mistakes
- Setting only the hover style and forgetting the base style, so the element has no color to return to.
- Removing the outline without a replacement focus style.
- Using shades too far apart, such as
bg-indigo-300tohover:bg-indigo-900, which feels jarring. - Using
hover:as the only way to reveal information. Touch screens have no hover.
Exercise
A text input uses focus:outline-none and nothing else for focus. What is wrong, and how do you fix it by adding two utilities?
Show answer
Keyboard users get no visible focus indicator. Add focus:ring-2 and focus:ring-indigo-500 (or focus:border-indigo-500 for a border change) so the focused field is clearly marked.
Exercise
Write the classes for a card that has a border-slate-200 border and shadow-md normally, but gets an indigo border and a large shadow on hover.
Show answer
border border-slate-200 shadow-md hover:border-indigo-400 hover:shadow-lg. You can add transition for a smooth change.