Sub topic 3
Styling hover states
This chapter assumes you know basic HTML and CSS, including the :hover pseudo-class. It also assumes you can use common Tailwind utilities such as colors, padding, and rounded corners in a project that already has Tailwind set up.
What hover: does
The hover: prefix is a variant. It makes a utility apply only while the pointer is over the element. Tailwind generates a rule that uses the CSS :hover pseudo-class, so you get the same behavior as hand-written CSS without leaving your markup.
<a href="#" class="text-slate-700 hover:text-indigo-600">Pricing</a>Here text-slate-700 is the normal color. hover:text-indigo-600 replaces it only while the pointer is over the link. When the pointer leaves, the link returns to slate.
Always write the base style too
The variant adds a conditional rule on top of your normal styles. It does not replace them. If you write only hover:bg-indigo-700 and no bg-* class, the element has no background until the pointer arrives, so it appears suddenly.
Pair each hover utility with a base utility for the same property:
<button class="rounded bg-indigo-600 px-4 py-2 text-white hover:bg-indigo-700">
Save
</button>The base class sets the starting color and the hover class sets the darker one. Choosing a neighboring shade, such as 600 to 700, gives a clear but modest change.
Hover is not only for color
Any utility can take the hover: prefix. Common choices are shadows, underlines, borders, opacity, and transforms:
<a href="#" class="rounded-lg border border-slate-200 p-4 shadow-sm hover:border-indigo-300 hover:shadow-md">
Read the guide
</a>You can add several hover utilities to one element. Each one is a separate class, and each applies at the same moment.
A link that only gains an underline on hover looks like this:
<a href="#" class="text-indigo-600 no-underline hover:underline">Docs</a>Common errors
The most frequent mistakes come from expecting hover to reach further than it does.
- Hover on the wrong element.
hover:reacts to the element that carries the class. If you puthover:text-indigo-600on a heading inside a card, the color changes only when the pointer is over the heading, not the card. Styling a child from its parent needsgroup, which a later chapter covers. - Hover on a non-interactive element. A
divwithhover:bg-slate-50does change color, but nothing tells users it is clickable. Keep strong hover effects for links, buttons, and other controls. - Relying on hover alone. Keyboard users never hover. Anything you signal on hover should also be reachable through focus, which the next chapters cover.
- Hover on a disabled control. A disabled button still matches
:hover, so it can look clickable. You will handle that with thedisabled:variant in a later chapter.
Checking your result
Open the page and move the pointer over the element. Then use your browser's developer tools to force the :hover state on it and confirm which class is applying. If nothing changes, check that the base and hover utilities target the same property. Also check that your content paths include the file, so Tailwind generates the class.
Exercise
A card contains a title in an h3. You add hover:text-indigo-600 to the h3, but the title only changes color when the pointer is directly over the title text, not when it is over the rest of the card. Why does this happen, and what does it tell you about where hover utilities apply?
Show answer
The hover: variant uses the :hover state of the element that has the class. The h3 is hovered only when the pointer is over the h3 itself, so hovering the card does not trigger it. To react to the whole card you need a different tool, group on the card and group-hover: on the child.