Different CSS for touch and mouse input
Screen width does not tell you whether someone is using a mouse, a finger, or a stylus. CSS interaction media features can, with an important limitation: devices often have more than one input method.
The pointer feature describes the primary pointing device. One practical use is giving a coarse pointer larger controls:
@media (pointer: coarse) {
input[type="checkbox"],
input[type="radio"] {
min-width: 30px;
min-height: 40px;
}
}
pointer has three values:
finemeans the primary pointer can accurately target small areas, as a mouse generally can.coarsemeans its accuracy is limited, which is common for touch input.nonemeans there is no primary pointing device.
A device may have several pointers
any-pointer checks all available pointing devices rather than only the primary one:
@media (any-pointer: fine) {
.canvas-controls {
display: block;
}
}
A touchscreen laptop might match both pointer: coarse and any-pointer: fine: touch is primary, but a trackpad or mouse is also available. This is why “touch device” and “small screen” are not interchangeable ideas.
Use these queries to improve an interface, not to remove essential controls based on an assumption about hardware. I would keep tap targets comfortable by default and use interaction features only where the input method genuinely changes the design.
Enjoyed reading this?
If you found this article helpful, consider supporting my writing. Every little bit helps keep this blog going.
Related articles
- Better Line Breaks With CSS balance and pretty
Use text-wrap balance for short headings and text-wrap pretty selectively for longer copy without hard-coded line breaks.
- Why 100vh Doesn’t Always Fit a Mobile Screen
Choose svh, lvh, dvh, or a flexible min-height when mobile browser controls make 100vh an awkward fit.
- Making Links Easy to Recognize
Make links recognisable with underlines, clear focus states, useful text, and behaviour that does not surprise the reader.