Search code examples
csshovertouch

CSS: How to allow the hover state only on devices which support hover?


I was looking for a way to prevent touch devices from "simulating" the hover event. The reason is that you can't hover on touch devices like you can on a desktop with the cursor and this sometimes interfere with the user experience.

Because I didn't found any solutions online I thought I post my own solution here. I hope this will help someone :)


Solution

  • I achieved to only hover on devices which have a real hover state with two media queries.

    @media (hover: hover), (-ms-high-contrast:none) {
      div:hover {
        background: red;
      }
    }
    <div>Hover me!</div>