skip to content

In Tailwind CSS v4, why does a card's `hover:` lift never show when tapped on a phone, and what should touch and keyboard users get?

level: middleimportance: nice to knowfreq 10%

answer

  1. hover as an enhancement
  2. a media query around :hover
  3. primary input must support hover
  4. focus-visible for keyboards
  5. active for a press

basics

~20 s

Tailwind CSS v4 wraps every hover: rule in @media (hover: hover), so it applies only when the primary input device can hover; a phone's touchscreen cannot. Give keyboard users focus-visible: styles and touch users active: feedback.

solid answer

~50 s

In Tailwind CSS v4, `hover:-translate-y-1` compiles to a `:hover` rule nested inside `@media (hover: hover)`, so it only applies when the **primary input device supports hover**. On a phone the media query is false and the lift never appears. v3 emitted a plain `:hover`, which touch browsers could trigger on tap, so sites could come to depend on that. The upgrade guide shows how to restore the old behaviour by overriding the `hover` variant with a plain `&:hover`, but recommends treating hover as an enhancement instead. Give keyboard users the same cue with `focus-visible:`, which matches when the browser shows focus (typically from the keyboard) rather than on every mouse click as `focus:` does; give touch users `active:` feedback for the press; and make sure nothing important is reachable only by hovering. `group-hover:` is gated the same way.

go deeper

for a junior

Recall that in v4 hover: only applies on devices that can hover, and that keyboards need focus-visible: rather than hover:.

for a middle

Explain the compiled rule, a :hover nested in @media (hover: hover), how v3 differed, and the difference between focus:, focus-visible: and active:.

for a senior

Spot interactions that only hover can reach, fix them for touch and keyboard, and decide whether overriding the hover variant is justified in a migration.

for a principal

Set the team rule that hover is an enhancement, and weigh restoring v3 hover behaviour against fixing the designs that depended on it.

## What `hover:` compiles to in v4 A **variant** makes a utility conditional, and `hover:` is the most used one. In Tailwind CSS v4 it does not compile to a bare `:hover` selector. The upgrade guide shows its output: ```css @media (hover: hover) { .hover\:underline:hover { text-decoration: underline; } } ``` The `(hover: hover)` media feature is true when the **primary input device** can hover, such as a mouse or trackpad, and false when it cannot, such as a phone's touchscreen. The rule therefore applies only where hovering is possible. The same gate wraps `group-hover:`, because the group form is built from the same hover condition. How the hover and pointer media features behave as CSS is a separate topic; here what matters is that Tailwind adds the query for you. ## Why the recipe card stopped lifting on phones In v3, `hover:` was a plain `:hover`. Touch browsers can apply `:hover` after a tap, so a card could appear to lift when tapped, and some sites came to rely on that. After an upgrade to v4, those taps do nothing, which is correct for a device that cannot hover but surprises anyone who designed around it. The upgrade guide covers this case and offers two paths: - **restore the old behaviour** by overriding the `hover` variant with one that uses a plain `&:hover`, which is a custom-variant definition in your CSS; - **treat hover as an enhancement**, which is what Tailwind recommends: touch devices do not truly hover, so nothing should depend on it. ## Feedback for every input The card should still respond to every kind of input. Each one has its own variant: | Input | Variant | Compiles to | Use it for | |---|---|---|---| | Mouse or trackpad | `hover:` | `&:hover` inside `@media (hover: hover)` | the lift and shadow | | Keyboard | `focus-visible:` | `&:focus-visible` | a visible focus cue on tab | | Any focus | `focus:` | `&:focus` | also fires on mouse click | | Press or tap | `active:` | `&:active` | momentary press feedback | | Focus inside the card | `focus-within:` | `&:focus-within` | when a link inside the card is focused | **`focus-visible:` versus `focus:`** is the distinction to get right. `focus:` applies whenever the element has focus, including right after a mouse click, which is why designers ask to remove "ugly outlines". `focus-visible:` applies when the browser decides focus should be shown, typically for keyboard navigation, so you can give keyboard users a strong ring without changing what mouse users see. For input-specific layout, Tailwind CSS 4.1 added `pointer-fine:`, `pointer-coarse:` and the `any-pointer-*` variants, which let a card show a larger tap target when the primary pointer is coarse. ## A working pattern ```html <a href="/recipes/lemon-risotto" class="block rounded-lg shadow-sm transition hover:-translate-y-1 hover:shadow-lg focus-visible:-translate-y-1 focus-visible:outline-2 active:translate-y-0"> ... </a> ``` 1. The lift is the hover enhancement for mouse users. 2. Keyboard users get the same lift and an outline through `focus-visible:`. 3. A tap gets press feedback from `active:` instead of a lift that would never appear. 4. Anything a user must reach, such as the save button, is visible without hovering. ## Controls revealed only on hover The most damaging pattern is not a missing lift but a **control that only hover reveals**: a save button with `opacity-0 group-hover:opacity-100`. On a touchscreen `group-hover:` never matches, so the button stays invisible, and keyboard users fare no better unless focus reveals it too. Safer options: - show the control by default and use hover only to emphasise it; - reveal it for every input, adding `group-focus-within:opacity-100` beside `group-hover:opacity-100`; - keep it visible when the primary pointer is coarse, with `pointer-coarse:opacity-100`. Restoring v3's plain `:hover` would let some taps reveal it again, but it only papers over the real problem: the design depends on an input that some users do not have. ## What to check in review - Menus or actions revealed only with `hover:` or `group-hover:` are unreachable on touch devices. - `focus:` used where `focus-visible:` was meant puts rings on every click. - Stacking `hover:focus-visible:` asks for both at once; when the cue should appear for either input, list `hover:` and `focus-visible:` utilities side by side.

  • Why prefer `focus-visible:` over `focus:` for a card's focus ring?
    `focus:` applies whenever the element has focus, including after a mouse click, so mouse users see the ring too. `focus-visible:` applies when the browser decides focus should be shown, typically during keyboard navigation, so keyboard users get a clear cue without changing the mouse experience.
  • A recipe card reveals its save button only with `group-hover:opacity-100`. What is wrong on a phone, and how do you fix it?
    `group-hover:` is gated on `(hover: hover)` like `hover:`, so on a touchscreen the button stays invisible and cannot be reached. Show the button by default, or reveal it for every input: add `group-focus-within:opacity-100` for keyboards and make it visible on coarse pointers, so the action never depends on hovering.

  • hover: in Tailwind CSS v4 compiles to a plain :hover selector, as in v3
  • focus: and focus-visible: are interchangeable for keyboard focus rings
  • on a phone, tapping a card still triggers its v4 hover: styles
  • group-hover: is not affected by the hover capability check
  • the only fix for touch devices is a JavaScript touch handler

Assumes Tailwind CSS v4 (checked at 4.3.3); since v4 the hover: variant only applies inside @media (hover: hover), where v3 emitted a plain :hover.