Software Design
Photo by UX Store on Unsplash

Handle :hover CSS on mobile touch screen

Arturo Lopez
1 min readFeb 17, 2021

If you are working on CSS and want to be able to handle hover on desktop view, but wan’t to avoid any changes on mobile touch, I got you.

I spent too much time trying to figure this out, so I am writing a quick article for anyone who wants to implement this.

Background

In CSS, you can define :hover, which will trigger any styling changes when the user hovers the mouse on the item which you have tagged.

Problem

The problem for me was that it was also registering a hover when a user would touch the component on their phone. I didn’t want this to happen on the mobile screen.

Solution

This is what I did to fix it:

I changed this:

a:hover {  background-color: red;  color: white;}

to this:

@media (hover: hover) {  a:hover {  background-color: red;  color: white; }}

Conclusion

CSS can be tricky sometimes. Have fun with it!

If this helped you, give this article a clap! Comment with any questions.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Arturo Lopez
Arturo Lopez

Written by Arturo Lopez

Tech analyst at Accenture. Mobile app and web developer. Passionate about tech and education.

Responses (1)

Write a response

I had to add hover state on my mobile app for accessibility purposes and I faced the same issue, that once Hover is added it didn't get removed. But your magical solution helped me, thanks!