4

My button changes its color on :hover, see:

button {
  background-color: orange;
}

button:hover {
  background-color: darkorange;
}

Example: https://jsfiddle.net/oepb5ks4/

Works great on desktop, but on mobile - where :hover should not exist – the browser* still changes the color, but after touching the button.

  1. Why is that behavior? Am I missing something? What makes somebody think that implementing (parts of) :hover on mobile browsers makes any sense?
  2. Is there a nice and clean solution for this (preferably without Media Queries or JavaScript)?

*Tested in Chrome (in "Device Mode") as well as in iOS Safari.

isherwood
  • 58,414
  • 16
  • 114
  • 157
kraftwer1
  • 5,253
  • 10
  • 36
  • 54
  • 6
    this known hover is activated by a click. Imagine you create a menu with subitems that you show on hover, on mobile it will be click – Temani Afif Jul 29 '19 at 14:13
  • On mobile, when you click, hover is activated, and If you "pass finger" over the button or something else that have state hover, the hover also activated, so hover exists on mobile too. – rafaelfndev Jul 29 '19 at 14:39

1 Answers1

3

In you CSS you could filter for mobile devices with the @media rules and filters (documentation at w3). You could state "if the media has the hover functionality then the hover color should be orange":

@media not all and (hover: none) {
    a:hover{
        color: orange
    }
}

You also could try to detect it with JS and manually adjust the color to always be orange when on mobile.

I believe that your problem occurs because in order to click on a button on PC you must have hovered over it before, so the color changes on mobile after you clicked it.

Update I just read the comment under the question by Temani Afif: apparently :hovers are "converted" to something like :onclick on mobile because of the lack of the hover feature on phones. As many websites have on hover menus mobile users couldn't access those if they wouldn't translate it.

Another thing you could try is to force the color of the visited links with this:

a:visited { 
  color: orange!important;
}

Update 2 Included the new media tag and took advice from this answer on a different question and it's comment on how to use not and the @media rules.

creyD
  • 1,972
  • 3
  • 26
  • 55