Sub topic 13
Hover on Touch Devices
This chapter explains why hover: behaves differently on phones and tablets. It also covers how Tailwind CSS handles that difference and how to design interactive components that work without a mouse. It assumes you know the hover:, focus-visible:, active:, and group-hover: variants from earlier chapters, and that you have a working Tailwind project. Version differences matter here, so the chapter names the Tailwind version each time behavior depends on it.
Why hover is a problem on touch screens
A mouse has a pointer that can rest over an element without clicking it. A finger cannot. It is either touching the screen or it is not. Browsers on touch devices still have to run the huge amount of existing code that relies on :hover, so they emulate it. Two behaviors follow:
- Sticky hover. After you tap an element, the browser treats it as hovered. It stays hovered until you tap somewhere else. A button can look highlighted long after the tap, which looks like a bug.
- Double-tap activation. Some mobile browsers, especially older iOS Safari, treat the first tap on an element with hover-dependent content as "hover" and need a second tap to "click". This can happen when a hover rule reveals a menu or changes the element's content.
Neither behavior is a Tailwind bug. Both come from the browser applying the CSS :hover pseudo-class to touch input. Tailwind's hover: utilities compile to :hover rules, so they inherit it.
The CSS feature that fixes it: `@media (hover: hover)`
CSS has a media feature that asks whether the device's primary input can hover:
@media (hover: hover) {
.link:hover {
text-decoration: underline;
}
}hover: hovermatches when the primary input can hover, such as a mouse or trackpad.hover: nonematches when it cannot, such as a touch screen.
Rules inside @media (hover: hover) do not run on a typical phone, so sticky hover never appears. Tailwind uses this feature in different ways depending on the version.
Tailwind v4: hover is wrapped by default
In Tailwind CSS v4, the hover: variant only applies when the primary input device supports hover. This also covers group-hover: and peer-hover:. The generated CSS is roughly:
@media (hover: hover) {
.hover\:bg-indigo-700:hover {
background-color: var(--color-indigo-700);
}
}On a phone, tapping a hover:bg-indigo-700 button does not leave it indigo. On a laptop, nothing changes. You get the safer behavior without writing anything extra.
Tailwind v3: hover applies everywhere by default
In Tailwind v3, hover: compiles to a plain :hover rule. Sticky hover therefore appears on touch devices unless you opt in. The opt-in is the hoverOnlyWhenSupported flag in your config:
// tailwind.config.js
module.exports = {
future: {
hoverOnlyWhenSupported: true,
},
content: ["./src/**/*.{html,js}"],
};With the flag on, v3 wraps every hover: utility in @media (hover: hover), the same as v4 does by default.
| Tailwind version | Default `hover:` output | How to change it |
|---|---|---|
| v3 | Plain `:hover`, active on touch devices | `future.hoverOnlyWhenSupported: true` wraps it in `@media (hover: hover)` |
| v4 | Wrapped in `@media (hover: hover)` | Override the variant with `@custom-variant` |
Restoring plain hover in v4
Sometimes you want the old behavior. An example is a hybrid device where the browser reports hover: none as the primary input but a stylus or mouse is also attached. In v4 you can redefine the variant in your CSS:
@import "tailwindcss";
@custom-variant hover (&:hover);After this, hover: compiles to a plain :hover rule again. Use this only if you have a concrete reason. For most sites the wrapped version is better.
Matching other input conditions with arbitrary variants
hover: hover describes only the primary input. A laptop with a touch screen reports a mouse or trackpad as primary, so hover works there. A tablet with a keyboard dock may report touch as primary even though a trackpad is connected. The related any-hover feature is true if any available input can hover. You can target it with an arbitrary variant, which works in v3 and v4:
<button class="rounded bg-indigo-600 px-4 py-2 text-white [@media(any-hover:hover)]:hover:bg-indigo-700">
Save
</button>The [@media(any-hover:hover)]: prefix wraps the rule in the media query. Then hover: stacks on top of it, following the left-to-right stacking you saw in the previous chapter. In v4 this means two hover conditions apply at once. The element needs any-hover support and a real :hover state.
Pointer variants for coarse and fine input
Tailwind v4.1 and later include variants for the pointer's precision:
pointer-fine:matches precise pointers such as a mouse.pointer-coarse:matches imprecise pointers such as a finger.pointer-none:matches when there is no pointer.any-pointer-fine:andany-pointer-coarse:match if any connected input has that precision.
These are useful for sizing targets, not just for hover. A common pattern makes tap targets larger on touch devices:
<button class="rounded bg-indigo-600 px-3 py-1.5 text-white pointer-coarse:px-5 pointer-coarse:py-3">
Add to cart
</button>Apple and Google guidelines recommend touch targets of roughly 44 by 44 CSS pixels or more. pointer-coarse: lets you meet that on phones without bloating the desktop layout.
Never hide essential content behind hover
The most important rule for touch design is that hover can only enhance. If a control exists only in a hover state, touch users cannot reach it. This is a common mistake with the group-hover: pattern from earlier in this tutorial:
<!-- Problem: the Edit button never appears on a phone -->
<li class="group flex justify-between p-2">
<span>Invoice 1042</span>
<button class="invisible group-hover:visible">Edit</button>
</li>On a v4 phone, group-hover: never fires. The button stays invisible and the feature is lost. Here are three fixes, in order of preference.
Fix 1: show the control by default and hide it only for fine pointers. This is the safest approach, because touch is the default case and only mouse users get the hover-reveal:
<li class="group flex justify-between p-2">
<span>Invoice 1042</span>
<button
class="pointer-fine:invisible pointer-fine:group-hover:visible pointer-fine:group-focus-within:visible"
>
Edit
</button>
</li>group-focus-within: also reveals the button when a keyboard user tabs to it. Without that, a keyboard user on a desktop would hit an invisible control. Elements with visibility: hidden cannot receive focus. That is a second reason to include the focus-within rule. If the button were hidden with opacity-0 instead, it could still be focused, and you would want a focus rule to make it visible then.
Fix 2: always show the control. If the control matters, drop the hover trick. A permanently visible button is simpler and more accessible.
Fix 3: move the action behind an explicit tap, such as a "more" menu button that toggles a list. Handle the toggle in JavaScript or with a native element such as <details>.
Give touch users feedback with `active:`
Because hover: is silent on touch devices, a tap would otherwise give no visual response. The active: variant applies while the element is being pressed, and it works for touch, mouse, and keyboard activation. Pair it with hover: so each input type gets feedback:
<button
class="rounded bg-indigo-600 px-4 py-2 text-white transition-colors
hover:bg-indigo-700 active:bg-indigo-800
focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
>
Save
</button>On iOS Safari, :active styles only apply if the page has a touch event listener on the element or an ancestor, or if you opt in with an empty touchstart listener on the body. Recent iOS versions have improved this, but test on a real device if press feedback seems missing.
Also consider the browser's tap highlight. Mobile browsers draw a translucent box when you tap a link or button. You can control it with the arbitrary property utility [-webkit-tap-highlight-color:transparent], but only remove it when your active: styling replaces the feedback.
A decision flow
Testing
Browser developer tools help, but with limits.
- Open device emulation in Chrome or Firefox developer tools and choose a phone profile. The emulator changes the
hoverandpointermedia features, so v4hover:rules should stop applying. - Tap an element and check that no hover color stays behind.
- Test on a real phone when you can. Emulation does not reproduce every iOS quirk, such as the
:activebehavior above. - Tab through the page with a keyboard to confirm every hidden control is reachable.
Common errors
- Assuming v3 and v4 behave the same. The same
hover:class produces different CSS. If you migrate from v3 to v4, hover effects that used to appear after a tap no longer do. - Putting required controls behind
group-hover:. Fix it with the default-visible pattern shown above. - Using
invisiblefor hover-reveal without a focus rule. Keyboard users cannot focus an element that hasvisibility: hidden. - Forgetting
active:. Touch users get no feedback on tap. - Overriding
hoverwith@custom-variantglobally and then being surprised by sticky hover on phones.
Exercise
A list row uses <button class="invisible group-hover:visible">Delete</button> inside a group element. Using Tailwind v4.1 or later, rewrite the button's classes so that touch users and keyboard users can both reach it, while mouse users still see it only on hover.
Show answer
Use <button class="pointer-fine:invisible pointer-fine:group-hover:visible pointer-fine:group-focus-within:visible">Delete</button>. The button is visible by default, which covers touch devices. Only fine-pointer devices hide it. On those, it appears on hover and also when focus is inside the group, so keyboard users can tab to it. Without the focus-within rule, visibility: hidden would stop it from receiving focus at all.
Exercise
In Tailwind v4, a button has hover:bg-indigo-700. A user taps it on a phone. Does the button turn indigo-700 and stay that way? What would you change to give the user feedback during the tap?
Show answer
No. In v4 the hover: variant is wrapped in @media (hover: hover), and a typical phone reports hover: none, so the rule does not apply. Add active:bg-indigo-800 (optionally with transition-colors) so the button changes color while it is pressed, on touch, mouse, and keyboard.