Question:
Why doesn't CSS hover work on mobile devices? I have a CSS hover class that functions seamlessly on desktop browsers but fails to trigger on mobile devices.
Answer:
The CSS :hover pseudo-class relies on a pointing device that distinguishes between "pointing" and "selecting/activating." Touch-based mobile devices typically lack dedicated pointing mechanisms, only providing the latter. Additionally, certain pen interfaces also limit functionality to activation rather than pointing.
As the W3C documentation explains, ":hover applies while the user designates an element (with some pointing device), but does not activate it. User agents not supporting interactive media do not have to support this pseudo-class."
Therefore, the compatibility of CSS hover on mobile devices depends on the specific device. However, it is generally expected to not work due to the absence of dedicated pointing devices. It is crucial to avoid relying solely on :hover for event handling on mobile devices. With the increasing adoption of touch-screen devices, the availability of pointer-only events is diminishing.
The above is the detailed content of Why Doesn\'t CSS Hover Work on Mobile Devices?. For more information, please follow other related articles on the PHP Chinese website!