mobile safari links retains focus after touch
Hover is a CSS pseudo class designed to work with a pointer not so much with touch events. You would normally want to avoid the use of hover altogether since it makes no sense on mobile. One of the easiest ways to deal with this is by placing your hover CSS inside a media query. You can do this by targeting tablets or desktop screens, here are the two solutions:
1- Targeting iPads:
@media only screen
and (min-device-width : 768px)
and (max-device-width : 1024px) {
RE.no-touch .my-element:hover {
/* in here you cancel all hover styles you applied for desktop */
}
}
2- Targeting desktops:
@media only screen
and (min-width : 1224px) {
RE.no-touch .my-element:hover {
/* in here you apply hover styles that will only work on desktop */
}
}
Either way is valid. You can change the values of min and max width if you notice that the CSS is being triggered incorrectly in certain devices.
If you're using Modernizr, the no-touch class will be added to the root html element for non-touch devices. Then you can do this:
a.myclass {
color:#999;
}
.no-touch a.myclass:hover,
a.myclass:active {
color:#ccc;
}
This is an old post but I was struggling with this today in 2019, and I find this clean solution and I want to share with all of you.
<a href="#">Try hovering over me!</a>
@media (any-hover: hover) {
a:hover {
background: yellow;
}
}
Basically what are we doing here is using hover in any device with pointer mechanism. Nowadays there is softwares with html readers for a better accessibility of the site and should not be a good idea, deactivate :hover for all mobile devices. Just to be clear, any device without pointing mechanism the hover will not take effect, otherwise we applied the :hover style.