Dealing with the Mobile Hover State

A common issue web developers run into is how hover effects on desktop transition to a mobile device. Since obviously there is no hover on mobile a discussion needs to be had about whether that hover effect is still vital/functional to the design or not. A good example of this is an effect we use on our About page.

Now, as with anything regarding web development, there are many ways to skin a cat, this is just my approach. If you have done something similar in a different way please share in the comments below.

The Solution (*no cats were harmed in this development)

To start things off, here is what the HTML looks like for one team member:

&nbsp;

<div class="team-member">
<figure>
<img class="member-image" src="<?php echo SITE_URL; ?>assets/images/pages/about/team/team-mike.jpg" alt="" />
<figcaption>
<h2>Mike</h2>
<h3>Principal, Creative Director</h3>
<p>Mike launched King Design out of the spare bedroom in his apartment in 2002, and the rest, as they say, is history. He’s a family man with a taste for beer, grilling, great design, working with his hands, guitar and photography.</p>
</figcaption>
</figure>
</div>

&nbsp;

Now, since on mobile hover does try to initiate the hover when you click on an element, we need to disable that. I used a combination of javascript and CSS for that. The javascript uses a media query for anything below 1024px wide and adds a “disable” class to the figure element:

The important part here is the &:not(.disable):hover section. What it is doing is applying my “hover effect” to the figure elements that do not have the .disable class applied on hover. Now, if you remember in the javascript, we applied the “disable” class for mobile devices. So this creates the effect on desktop sizes but not mobile devices. We now need to add a little more javascript to the existing code:

This code creates a click event on the figure element and applies a “hover” class to it if there isn’t already one on it (if there is one on it, it’s because it has already been clicked). Next, we have to add this new “hover” class into our CSS (for sanity’s sake, I’m just showing the modified section):

Wrapping Up

Hover effects on mobile are fairly common issues that have a pretty straight-forward workaround but still aren’t perfect. This generation of web developers are still trying to wade through the sea of new mobile device development. When I was researching this problem, I was really surprised at the lack of documentation I was finding. Hopefully this will help anyone else that has run into a similar use case.