Sub topic 13
Hover on touch devices
This chapter assumes you know hover:, group-hover: and focus-within: from earlier chapters. It also assumes you know basic CSS media queries.
The problem with hover on a touchscreen
A mouse can point at an element without clicking it. A finger cannot. When a user taps an element on a phone, the browser often emulates a hover and leaves the element in the :hover state until the user taps somewhere else. This is called a sticky hover. A button can stay highlighted after it was pressed, or a menu can stay open when the user expected it to close.
A second problem is that some touch users can never trigger hover at all. If a control exists only inside a hover: or group-hover: style, those users may not be able to reach it.
How Tailwind handles it
In Tailwind CSS v4, the hover: variant only applies on devices that support hover. It compiles to roughly this CSS:
@media (hover: hover) {
.hover\:bg-indigo-700:hover {
background-color: var(--color-indigo-700);
}
}On a phone, (hover: hover) is false, so the rule never applies and sticky hover colors disappear. In Tailwind v3, hover: applied everywhere by default. You could opt in to the media query with future: { hoverOnlyWhenSupported: true } in tailwind.config.js. Check which version your project uses before you rely on this behavior.
Never hide essential controls behind hover
Even with the media query, a hover-only design fails on touch. Reveal controls on hover for pointer users, and keep them visible for everyone else. Tailwind v4 includes pointer-fine: and pointer-coarse: variants. They match the precision of the primary input, so you can write the pointer-specific behavior explicitly.
<div class="group relative rounded-lg border border-slate-200 p-4">
<p class="text-slate-900">Quarterly report</p>
<button
class="mt-2 rounded bg-indigo-600 px-3 py-1 text-white
pointer-fine:opacity-0
pointer-fine:group-hover:opacity-100
group-focus-within:opacity-100
focus-visible:opacity-100"
>
Edit
</button>
</div>The button is fully visible by default, which is what touch users get. Only on devices with a fine pointer (a mouse) does it start hidden. It then appears on card hover, or when focus moves inside the card, so keyboard users can still find it.
<br>
Restoring hover everywhere
Sometimes you want the old behavior, for example on hybrid laptops where touch and mouse are both available. In v4 you can redefine the variant in your CSS:
@import "tailwindcss";
@custom-variant hover (&:hover);This makes hover: apply on every device again. Doing so brings back sticky hover on touch, so prefer the default unless you have a reason.
Targeting touch directly
For other conditions, use an arbitrary variant that wraps any media query:
<a
href="#"
class="block p-3 [@media(hover:none)]:bg-slate-100"
>
Always shaded on touch-only devices
</a>For pressed feedback that works on touch, use active:. It applies while the finger is down, so it gives immediate feedback without sticky hover.
| Goal | Utility to use |
|---|---|
| Pointer-only highlight | `hover:` (v4 default) |
| Touch press feedback | `active:` |
| Reveal for keyboard users | `focus-visible:`, `group-focus-within:` |
| Touch-specific style | `pointer-coarse:` or `[@media(hover:none)]:` |
Exercise
On a phone, a card's "Delete" button uses invisible group-hover:visible. Why is this a problem, and how would you fix it using the variants from this chapter?
Show answer
A touchscreen cannot hover, and with Tailwind v4 the group-hover: rule is skipped on such devices, so the button stays invisible and unreachable. Keep it visible by default and hide it only for fine pointers: pointer-fine:invisible pointer-fine:group-hover:visible group-focus-within:visible. Touch users see the button, mouse users see it on hover, and keyboard users see it when focus enters the card.