In order for the active state to be applied while the user is also hovering over the button, it's necessary for the :hover
selector to come before the :active
selector in the code.
From https://developer.mozilla.org/en-US/docs/Web/CSS/:active:
Styles defined by the :active pseudo-class will be overridden by any
subsequent link-related pseudo-class (:link, :hover, or :visited) that
has at least equal specificity. To style links appropriately, put the
:active rule after all other link-related rules, as defined by the
LVHA-order: :link — :visited — :hover — :active.
While the accepted answer did mention that it's necessary to have :active
come after :link
and :visited
, it doesn't say that it must also come after :hover
(since in the example given in the question this was already the case). However this wasn't immediately clear to me, so I wanted to post this answer for anyone else who was stuck like I was because the :hover
selector was coming after :active
.
Also, I think the LVHA-order is a handy rule to keep in mind and relevant to this question.