Using pointer and hover Media Queries
Detect input device precision with the pointer and hover CSS media queries. Differentiate between mouse, touch, and stylus inputs for adaptive UI design.
Detailed Explanation
pointer and hover Media Features
These media features let you adapt your UI based on the user's primary input device capabilities.
The pointer Feature
| Value | Meaning | Typical Devices |
|---|---|---|
| fine | Accurate pointing (mouse, trackpad, stylus) | Desktop, laptop |
| coarse | Imprecise pointing (finger on touchscreen) | Phone, tablet |
| none | No pointing device | Keyboard-only, screen reader |
@media (pointer: coarse) {
/* Larger touch targets for touch devices */
.btn { min-height: 44px; min-width: 44px; }
}
@media (pointer: fine) {
/* Compact UI for mouse users */
.btn { min-height: 32px; }
}
The hover Feature
| Value | Meaning |
|---|---|
| hover | Primary input can hover (mouse) |
| none | Primary input cannot hover (touch) |
@media (hover: hover) {
.card:hover {
transform: translateY(-4px);
box-shadow: 0 12px 24px rgba(0,0,0,0.15);
}
}
@media (hover: none) {
/* On touch, use active state instead */
.card:active {
background-color: var(--accent);
}
}
any-pointer and any-hover
While pointer and hover check the primary input, any-pointer and any-hover check all available inputs:
@media (any-pointer: coarse) {
/* At least one coarse input exists (e.g., laptop with touchscreen) */
}
JavaScript Detection
const hasCoarsePointer = window.matchMedia('(pointer: coarse)').matches;
const canHover = window.matchMedia('(hover: hover)').matches;
const hasTouchAndMouse =
window.matchMedia('(any-pointer: fine)').matches &&
window.matchMedia('(any-pointer: coarse)').matches;
Practical Guidelines
- Use
pointer: coarseto enlarge touch targets to at least 44px (WCAG recommendation) - Use
hover: hoverto gate hover-dependent interactions (tooltips, dropdown menus) - Use
any-pointerfor hybrid devices like convertible laptops - Never assume touch-only = mobile; some desktops have touchscreens
Use Case
UI developers building adaptive interfaces use pointer and hover queries to create appropriately-sized touch targets for mobile while maintaining compact desktop UIs with hover interactions.
Try It — Screen Info Display
Related Topics
How to Detect Touch Devices in JavaScript
Touch & Input
Detecting Dark Mode with prefers-color-scheme
Media Queries
Respecting prefers-reduced-motion for Accessibility
Media Queries
Tailwind CSS Responsive Breakpoints Reference
Responsive Breakpoints
CSS resolution Media Query for High-DPI
Device Pixel Ratio