:hover
Example
.card:hover {
transform: translateY(-4px);
}@media (hover: hover) {
.card:hover {
transform: translateY(-4px);
}
}
.card:focus-visible {
outline: 2px solid;
}The media query limits the lift to real pointers, and :focus-visible gives keyboard users a visible state too.
Common mistakes
- A button stays highlighted after a tap on a phone because of a sticky
:hoverstate. - Hiding important actions until hover, so touch and keyboard users never find them.
Key points
- Hover-only effects sit inside
@media (hover: hover)so taps don't leave them stuck. - Every
:hoverstyle has a matching:focus-visiblestyle for keyboard users. - Content shown on hover, like menus, can also be opened by click or keyboard.
Interview questions
Does :hover work on mobile?
Partly. A tap often triggers :hover, and it can stay until the user taps elsewhere.
How do I make a hover effect also work for keyboard users?
Group the selectors: .btn:hover, .btn:focus-visible { ... }.
Can :hover be used on any element?
Yes. :hover works on any element, not just links and buttons.