Fix Hover Not Working on Mobile in CSS: Simple Solutions
:hover because they use touch, not a mouse. To fix this, use touchstart events in JavaScript or apply styles on :active or :focus states instead of :hover for better mobile support.Why This Happens
Mobile devices rely on touch input, not a mouse pointer. The :hover CSS selector works when a mouse pointer is over an element, but on touchscreens, there is no pointer to hover. This means hover styles often don't trigger on mobile.
Here is an example of CSS that works on desktop but fails on mobile:
button:hover {
background-color: blue;
color: white;
}The Fix
To fix hover not working on mobile, use :active or :focus selectors which respond to taps and focus events. Alternatively, use JavaScript to add a class on touchstart to simulate hover styles.
This CSS example uses :active to apply styles on tap:
button:active, button:focus {
background-color: blue;
color: white;
}Prevention
To avoid hover issues on mobile, design interactions that do not rely solely on hover. Use clear tap targets and consider using JavaScript to detect touch devices and adjust styles or behaviors accordingly.
- Test your site on real mobile devices.
- Use
:focusand:activefor interactive elements. - Use JavaScript event listeners like
touchstartto add hover-like effects.
Related Errors
Other common issues include:
- Hover styles not triggering on keyboard navigation: Use
:focus-visibleto style keyboard focus. - Click events not firing on touch devices: Use
touchstartorpointerdownevents. - Hover styles causing layout shifts: Avoid large layout changes on hover to improve user experience.