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.
@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.
Syntax
@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
hoverversusany-hover.hoverdescribes the primary input only. A laptop with a touchscreen usually reportshover: hover, yet the user may also tap.any-hover: hoveris 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
:hoverwith:focus-visibleso keyboard users see the same feedback. - Sticky hover. Some touch browsers apply
:hoverafter a tap and keep it until you tap elsewhere. Wrapping hover effects in(hover: hover)avoids that. - Combine with pointer. Use
pointerto 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.
Related
pointer and any-pointerโ input precision:hoverโ the pseudo-class@mediaโ the at-rule- Lesson: Media Queries