How do you detect whether a device supports hover?

Detect hover capability with the CSS hover and any-hover media features. hover: hover describes the primary input’s ability to hover conveniently. any-hover: hover becomes true when at least one available input can hover. Capability queries are more accurate than inferring input from viewport width.
@media (hover: hover) and (pointer: fine) {
.menu-item:hover .submenu {
display: block;
}
}
@media (any-pointer: coarse) {
.control {
min-height: 3rem;
}
}
Use the primary-input query for optional hover polish such as color changes or previews. Use any-hover when the design needs to know that some connected device can hover, but remember that the user may still be touching the screen. Hybrid laptops and tablets can report both coarse and fine inputs through the any-pointer features.
JavaScript can observe the same condition with matchMedia("(hover: hover)"). Listen for the media query’s change event if input capabilities may change after a mouse or trackpad connects. Do not cache the result permanently at page load and label the device as touch-only or mouse-only.
Hover detection should never decide access to content. The base experience needs visible controls and keyboard behavior. Add hover as an optional path, pair it with focus for keyboard users, and provide tap-based disclosure. Device capabilities do not reveal the user’s preference or guarantee which input they will use next.
Test the rules on a phone, a mouse-driven desktop and a touchscreen laptop. Connect and disconnect a pointer where possible. Confirm that coarse input receives larger targets, mouse input receives hover feedback, and the interface stays complete when neither hover query matches.