Skip to content

Instantly share code, notes, and snippets.

@vasilisvg
Created October 10, 2011 16:57
Show Gist options
  • Save vasilisvg/1275792 to your computer and use it in GitHub Desktop.
Save vasilisvg/1275792 to your computer and use it in GitHub Desktop.
Set :hover for non-touch devices only

No :hover for touch devices

Some touch devices trigger the mouseover event on the first touch when something changes visibly. This happens even with simple changes like a new color or removal of an underline. The click-event only fires the second time you touch.

This is easy to solve, if modernizr is included, by adding the class .no-touch to each :hover rule.

.no-touch a:hover { color: #06e; }

This code will also kill hover states in environments where JS has not been executed. To solve this you would have to extend the code a bit and add class=no-js to the head of the document.

.no-touch a:hover,
.no-js a:hover { color: #06e; }
@isaacalves
Copy link

That's clever... is there any cleaner solution for that, though?

@aaron-coding
Copy link

If you don't use Modernizr you can create your own no-touch in document using this code:

if (!("ontouchstart" in document.documentElement)) {
document.documentElement.className += " no-touch";
}

props to: http://www.nczonline.net/blog/2012/07/05/ios-has-a-hover-problem/

@imme-emosol
Copy link

Perhaps useful for someone, vim rewrite/substitute commands:

:%s/^([\t ]*)(.{-}):hover/\1.no-touch \2:hover/gc 

or

:%s/^([\t ]*)(.{-}):hover/\1.no-touch \2:hover ,\r\1.no-js \2:hover/gc 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment