Sub topic 13
Adapting to Hover and Pointer Capabilities
This chapter assumes you know basic HTML, CSS selectors and the cascade, and how @media rules and min-width queries work. Width tells you how much room a page has. It does not tell you how the user points at things. A 1024px-wide tablet is touch-first. A 1024px-wide laptop window uses a mouse. This chapter covers the media features that describe the input device.
Why width is not enough
Two problems appear when you guess input type from screen size.
- Hover styles stick on touch screens. After a tap, many mobile browsers keep the
:hoverstate on the element until the user taps somewhere else. A menu or button can look stuck in its hover state. - Small targets are hard to hit with a finger. A 24px icon is fine for a mouse and frustrating on a phone.
Interaction media features let you test the input capabilities directly instead of inferring them from the viewport.
The four features
| Feature | Describes | Values |
|---|---|---|
| `hover` | Can the primary input hover over elements? | `none`, `hover` |
| `pointer` | How precise is the primary input? | `none`, `coarse`, `fine` |
| `any-hover` | Can any available input hover? | `none`, `hover` |
| `any-pointer` | Most precise of any available input | `none`, `coarse`, `fine` |
The values mean the following.
hover: hovermeans the primary input can hover easily, as a mouse does.hover: nonemeans it cannot, as with a finger on a touch screen.pointer: finemeans an accurate device such as a mouse, trackpad, or stylus.pointer: coarsemeans a limited-accuracy device such as a finger.pointer: nonemeans no pointing device.- The
any-versions check every input the device has. The plain versions check only the primary input, which the browser chooses.
Applying hover styles only where hover exists
This rule applies hover styling only when the primary input can hover and point precisely.
@media (hover: hover) and (pointer: fine) {
.button:hover {
background: #0a58ca;
}
}The and operator requires both conditions. On a phone, neither is true, so the :hover rule never applies and the sticky hover state never appears. The button's normal background is defined outside the query, so touch users still see a styled button.
A complete example with a base style, a hover style, and a keyboard focus style:
.button {
display: inline-block;
padding: 0.75rem 1rem;
background: #0d6efd;
color: #fff;
border: 0;
border-radius: 0.375rem;
}
.button:focus-visible {
outline: 3px solid #ffbf47;
outline-offset: 2px;
}
@media (hover: hover) and (pointer: fine) {
.button:hover {
background: #0a58ca;
}
}Keep :focus-visible outside the query. Keyboard users need focus styles on every device, including laptops that report a hover-capable mouse and phones with an attached keyboard.
Larger targets for coarse pointers
For pointer: coarse, increase the size of clickable areas. Common guidance recommends targets of roughly 44 by 44 CSS pixels (WCAG 2.5.5 suggests 44px, and WCAG 2.2's minimum in 2.5.8 is 24px).
.icon-button {
min-width: 2rem;
min-height: 2rem;
}
@media (pointer: coarse) {
.icon-button {
min-width: 2.75rem;
min-height: 2.75rem;
}
.nav a {
padding: 0.875rem 1rem;
}
}With rem units, the sizes also scale if the user raises their default font size.
Primary input versus any input
Many devices have more than one input. A touch laptop has a trackpad and a touch screen. A tablet may have a keyboard case with a trackpad. The browser reports one as primary, and the plain hover and pointer features describe only that one.
This creates a decision, shown below.
flowchart TDBecause that block would be invalid as a diagram, here is the decision in the proper form.
A practical rule: use the primary-input features (hover, pointer) for sizing and hover decoration. Use any-hover and any-pointer when you must not exclude a capability. For example, if you hide a menu until hover, show it by default whenever any-hover: none is true, and do not rely on hover alone.
.tooltip-trigger .tooltip {
display: block;
}
@media (any-hover: hover) {
.tooltip-trigger .tooltip {
display: none;
}
.tooltip-trigger:hover .tooltip,
.tooltip-trigger:focus-within .tooltip {
display: block;
}
}The default shows the content. Only devices that can hover get the hidden-until-hover behavior. This follows the same progressive-enhancement idea as mobile-first CSS: the base case works everywhere, and the query adds behavior.
Do not hide essential content behind hover
Hover is not available to touch users, and it is awkward for keyboard and screen reader users. Treat hover as decoration or a shortcut. Anything important needs another route, such as a visible button, a focus state, or a tap target. Pair every :hover effect with :focus-visible or :focus-within where it reveals content.
Common errors
- Putting all
:hoverrules outside the query and then trying to undo them with@media (hover: none). The base should be the safe one. Add hover inside(hover: hover). - Using only
pointer: coarseto enlarge a layout. Coarse pointers need larger targets and spacing, but they do not need a different page structure. - Relying on
any-pointer: coarseto mean "touch is the main input". On a touch laptop it is true even though the user may be using the trackpad. - Using
@media (hover)without a value. This is valid and means "any value exceptnone", but it is easy to misread. Write the value explicitly. - Forgetting that older or unusual browsers may not report these features. If neither
hover: hovernorhover: nonematches, the rule does not apply, so keep the base styles usable on their own.
Combining with other queries
Interaction features combine with width and preference queries using the operators from earlier chapters.
@media (min-width: 48em) and (hover: hover) {
.menu-item:hover > .submenu {
display: block;
}
}
@media (hover: none), (pointer: coarse) {
.menu-item > .submenu {
position: static;
}
}The comma in the second query means "or". The submenu falls back to a static, always-visible list when the primary input cannot hover or is coarse.
Testing
In Chrome or Edge DevTools, device mode emulates touch, so hover: none and pointer: coarse match. Rendering settings in the DevTools command menu can also override some media features. Use matchMedia in the console to check what your current device reports:
const checks = [
"(hover: hover)",
"(hover: none)",
"(pointer: fine)",
"(pointer: coarse)",
"(any-hover: hover)",
"(any-pointer: coarse)"
];
for (const query of checks) {
console.log(query, window.matchMedia(query).matches);
}Emulation is a good first check. A real phone or a touch laptop is still the best test, because it shows sticky hover and target-size problems that emulation can miss.
Exercise
A touch laptop has a trackpad as its primary input and a touch screen as a secondary input. You want a dropdown menu to open on hover for mouse users, but it must stay usable by touch. Which query should gate the hover-to-open behavior, and what should the default (no query) state be?
Show answer
Make the default state show the submenu content, for example as a visible list or a toggle button. Then wrap the hide-until-hover rules in @media (any-hover: hover), and also reveal on :focus-within. Using any-hover keeps the hover behavior available for the trackpad, and because the default state is usable, the touch screen still works. A rule gated only by hover: hover would also match here, but it would leave touch-only use dependent on the primary input being reported correctly.
Exercise
Why is @media (hover: hover) and (pointer: fine) { .button:hover { ... } } safer than putting .button:hover outside any query?
Show answer
On touch devices, the :hover state can stay applied after a tap, so the button looks stuck. Inside the query, the rule matches only when the primary input can hover and is precise, so touch users never receive it.