Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Media Queries and Features

The hover and any-hover media features

Reference for the CSS hover and any-hover media features: detect whether a device can hover, with examples for touch-friendly styles and common pitfalls.

The hover media feature reports whether the user's primary input mechanism can hover over elements. A mouse can; a finger on a touchscreen cannot. any-hover asks the same question about any available input: it is true if at least one connected input device can hover.

Use these features to apply hover effects only where hovering exists, and to avoid hiding important controls behind a hover that touch users can never trigger.

CSS
@media (hover: hover) {
  .card:hover {
    transform: translateY(-4px);
  }
}

Try it

The button highlights on hover only when the device can hover. A label shows what your device reports.

Hover only where it works

Syntax

CSS
@media (hover: none) { /* ... */ }
@media (hover: hover) { /* ... */ }
@media (any-hover: none) { /* ... */ }
@media (any-hover: hover) { /* ... */ }

Both are discrete media features.

Values

Value Description
none The input cannot hover, or hovering is awkward (for example, a tap-based touchscreen).
hover The input can conveniently hover over elements.

Usage notes

  • hover versus any-hover. hover describes the primary input only. A laptop with a touchscreen usually reports hover: hover, yet the user may also tap. any-hover: hover is true whenever a mouse or trackpad exists anywhere.
  • Never hide essentials behind hover. Menus, labels and actions that only appear on hover are unusable on touch devices. Provide a tap or focus alternative.
  • Keep focus styles. Pair :hover with :focus-visible so keyboard users see the same feedback.
  • Sticky hover. Some touch browsers apply :hover after a tap and keep it until you tap elsewhere. Wrapping hover effects in (hover: hover) avoids that.
  • Combine with pointer. Use pointer to also check how precise the input is.
  • Do not detect devices. These are capabilities of the input, not device types. A user might connect a mouse to a tablet at any time.

Browser support

Supported in current versions of all major browsers; check a compatibility table for older mobile browsers.