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 Tailwind utilities such as colors, padding, and rounded corners in a project where Tailwind is already set up. Here you learn how the hover: variant works, how to use it on common elements, and where it breaks down.
What `hover:` does
A variant prefix is a word followed by a colon, placed in front of a utility class. The utility then applies only when a condition is true. For hover:, the condition is that the pointer is over the element.
In plain CSS you write a rule with a pseudo-class:
.save-button:hover {
background-color: #4338ca;
}In Tailwind you put the condition in the class name:
<button class="bg-indigo-600 hover:bg-indigo-700 text-white px-4 py-2 rounded">
Save
</button>Here bg-indigo-600 is the default background. hover:bg-indigo-700 replaces it while the pointer is over the button. When the pointer leaves, the default returns.
Tailwind generates a separate CSS rule for each variant class. The rule for hover:bg-indigo-700 looks roughly like this:
.hover\:bg-indigo-700:hover {
background-color: var(--color-indigo-700);
}The :hover is attached to the generated class selector. The base rule and the hover rule both exist in the stylesheet. Tailwind orders them so that variant rules come after the base utilities, so the hover rule wins when both match.
Any utility can take `hover:`
You can put hover: in front of almost any utility, not only colors. Common uses include:
- Color changes:
hover:bg-slate-100,hover:text-indigo-600 - Shadows:
hover:shadow-md - Underlines:
hover:underline - Opacity:
hover:opacity-80 - Transforms:
hover:-translate-y-0.5,hover:scale-105 - Borders:
hover:border-indigo-500
You can also use several hover utilities on one element. They all apply at the same time:
<a href="#" class="rounded-lg border border-slate-200 p-4 shadow-sm hover:border-indigo-400 hover:bg-slate-50 hover:shadow-md">
Pricing
</a>Each hover utility needs its own prefix. Writing hover:border-indigo-400 bg-slate-50 would apply the border color on hover but the background all the time.
A complete button
The first button below combines several states. This chapter focuses on hover:. The other variants appear in later chapters, so treat them as a preview.
<button class="rounded bg-indigo-600 px-4 py-2 text-white transition-colors hover:bg-indigo-700 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600 active:bg-indigo-800 disabled:cursor-not-allowed disabled:opacity-50">
Save
</button>Read it in layers:
bg-indigo-600 text-white px-4 py-2 roundedis the resting appearance.hover:bg-indigo-700darkens the background under the pointer.transition-colorsmakes the change animate instead of snapping.focus-visible:,active:, anddisabled:handle keyboard focus, pressing, and the disabled state.
Notice that a utility without a prefix always applies. A prefixed utility applies only in its state. Because of this, you describe the default look once and then list only what changes.
Smooth the change with transitions
By default, a hover change happens instantly. The transition-colors utility tells the browser to animate color-related properties (background, text, border, and similar) over a short duration.
<a href="#" class="text-slate-700 transition-colors duration-200 hover:text-indigo-600">
Documentation
</a>Put the transition on the base element, not behind hover:. If you write hover:transition-colors, the animation exists only while hovered. The element then animates in, but snaps back instantly when the pointer leaves, because the transition rule no longer applies at that point.
Pick the transition utility that matches what you change:
| Utility | Animates | Use when |
|---|---|---|
| `transition-colors` | Color, background, border, fill, stroke | Only colors change |
| `transition-opacity` | Opacity | You fade something |
| `transition-shadow` | Box shadow | A shadow grows or shrinks |
| `transition-transform` | Translate, scale, rotate | An element moves or resizes |
| `transition` | A common set of properties | Several kinds of change at once |
| `transition-all` | Every property | Rarely. It can animate things you did not intend |
You can tune timing with duration-150, duration-300, and easing with ease-in-out.
Hovering a parent to change a child
hover: only reacts to the element that carries the class. Sometimes you want the whole card to react and a child to change. For that you mark the parent with group and use group-hover: on the child.
<a href="#" class="group block rounded-lg border border-slate-200 p-4 hover:bg-slate-50">
<h3 class="font-semibold text-slate-900 group-hover:text-indigo-600">Read the guide</h3>
<span class="inline-block transition-transform group-hover:translate-x-1">→</span>
</a>When the pointer is anywhere over the link, the heading turns indigo and the arrow slides right by a small step. The link itself also gets a light background through its own hover:bg-slate-50.
Compare the two behaviors:
hover:text-indigo-600on the heading changes color only when the pointer is over the heading text.group-hover:text-indigo-600on the heading changes color when the pointer is over the whole group.
The transition-transform on the arrow sits on the arrow, since that is the element whose transform changes.
The group class has no styles of its own. It is a marker that other utilities look for. A later chapter covers group with focus and other states, and named groups for nested components.
Stacking `hover:` with other variants
Variants can be chained. Each prefix adds a condition, and all conditions must be true for the utility to apply. You read them left to right.
<button class="bg-white text-slate-900 hover:bg-slate-100 dark:bg-slate-800 dark:text-white dark:hover:bg-slate-700 md:hover:shadow-md">
Settings
</button>dark:hover:bg-slate-700applies in dark mode and while hovered.md:hover:shadow-mdapplies at themdbreakpoint and wider, and while hovered.
In light mode, hovering gives hover:bg-slate-100. In dark mode, both hover:bg-slate-100 and dark:hover:bg-slate-700 match during hover. The dark one wins because Tailwind emits dark: rules after plain variant rules. Without the dark:hover: class, the dark button would turn light gray when hovered, which would clash with its white text.
This is a common error: you define a dark base color but forget the dark hover color. Whenever you add dark:bg-*, check whether the element also has a hover:bg-* that needs a dark partner.
Hover on touch devices
Touchscreens have no pointer that rests over an element. Browsers handle this in different ways, and one common result is that a tap leaves the element in a "hovered" state until you tap elsewhere. This is sometimes called a sticky hover.
Tailwind helps with this. In current versions, hover: is wrapped in the @media (hover: hover) media query. That query is true only when the primary input can hover, such as a mouse or trackpad. On a typical phone, the hover styles do not apply at all.
The practical consequences:
- Never hide essential information or controls behind hover alone. Touch users may never see them.
- Do not use hover as the only signal that something is clickable. Make buttons and links look interactive by default.
- Treat hover as an enhancement on top of a design that works without it.
For a control that must work on all devices, pair hover: with a state that touch and keyboard users can trigger, such as focus-visible: or active:.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Hover style never shows | The class name is misspelled, or built dynamically such as `` `hover:bg-${color}-700` `` | Write complete class names so Tailwind can detect them |
| Hover works but snaps back instantly | The transition is behind `hover:` | Move the `transition-*` class to the base element |
| Hover does nothing on a `<div>` | The element is covered by another element, or has `pointer-events-none` | Check overlapping elements and `pointer-events` |
| Disabled button still changes on hover | `hover:` applies to disabled elements too | Add `disabled:` styles, or `enabled:hover:bg-indigo-700` |
| Hover color looks wrong in dark mode | No `dark:hover:` partner | Add one |
The first row deserves attention. Tailwind finds classes by scanning your source files for complete strings. It does not run your code. A class assembled from pieces at runtime never appears as a whole string, so no CSS is generated for it. Use a lookup object that contains full class names instead.
Hover is not the whole picture
Mouse users get hover styling, but keyboard users never trigger it. Anything you style with hover: on an interactive element should usually have a matching focus-visible: style. The next chapters cover focus in detail. For now, remember that hover is one input type among several.
Exercise
A card has the classes group rounded border p-4. Inside it, a heading has text-slate-900 hover:text-indigo-600. A user moves the pointer over the card's padding, not over the heading text. Does the heading change color? How do you change it so the heading reacts whenever the pointer is anywhere over the card?
Show answer
No. hover: on the heading reacts only when the pointer is over the heading itself. Change the heading's class to group-hover:text-indigo-600. The group class on the card is already in place, so the heading then changes color when the pointer is anywhere over the card. Add transition-colors to the heading so the change is smooth.
Exercise
This link fades in on hover but jumps back instantly when the pointer leaves: class="text-slate-700 hover:text-indigo-600 hover:transition-colors". Why does this happen, and what is the fix?
Show answer
The transition-colors utility exists only while the element is hovered, so on mouse leave no transition rule applies and the color snaps back. Move the transition to the base element: class="text-slate-700 transition-colors hover:text-indigo-600".
Exercise
A button has bg-white text-slate-900 dark:bg-slate-800 dark:text-white hover:bg-slate-100. In dark mode, hovering turns it light gray with white text. Fix it, and say which order the variants must be written in.
Show answer
Add a dark hover color: dark:hover:bg-slate-700. The variants are read left to right, with dark: first and hover: second, so the class applies only in dark mode while hovered.