There are several solutions to this problem.
Quick'n'dirty - delete: hover styles using JS
You can remove all CSS rules containing :hover using Javascript. This has the advantage that you don’t have to touch CSS and be compatible even with older browsers.
function hasTouch() { return 'ontouchstart' in document.documentElement || navigator.maxTouchPoints > 0 || navigator.msMaxTouchPoints > 0; } if (hasTouch()) {
Limitations: style sheets must be in the same domain (this means that there is no CDN). Disables freezes on mixed mouse and touch devices such as Surface, which will damage the UX.
CSS-only - using media queries
Put all your rules: hover in the @media block:
@media (hover: hover) { a:hover { color: blue; } }
or, conversely, override all hover rules (compatible with older browsers):
a:hover { color: blue; } @media (hover: none) { a:hover { color: inherit; } }
Limitations: works only with iOS 9.0+, Chrome for Android or Android 5.0+ when using WebView. hover: hover breaks the effects of hovering on older browsers, hover: none requires overriding all previously defined CSS rules. Both are incompatible with mixed mouse and touch devices .
The most reliable is to use a special CSS class and detect touch through JS
This method requires the addition of all hover rules using body.hasHover . (or the class name of your choice)
body.hasHover a:hover { color: blue; }
The hasHover class can be added using hasTouch() from the first example:
if (!hasTouch()) { document.body.className += ' hasHover'; }
However, this problem has the same problems with mixed touch devices as the previous examples, which leads us to a final solution. Turn on hover effects each time you move the mouse cursor; turn off hover effects when a touch is detected.
function watchForHover() { var hasHoverClass = false; var container = document.body; var lastTouchTime = 0; function enableHover() {
This should work mostly in any browser and enable / disable hover styles as needed. Try it here: https://jsfiddle.net/dkz17jc5/19/