# :hover

> The `:hover` CSS pseudo-class selects an element when the user moves the mouse pointer over it, allowing for hover effects to be applied.

## How do I stop :hover styles sticking on touch devices?

The pseudo-class that applies styles to an element when the user moves the mouse pointer over it. It's commonly used for interactive elements like buttons or links, changing their appearance to indicate they are clickable or to highlight them.

- Topic: Pseudo Classes
- Type: pseudo-class
- Browser support: Chrome 1+, Chrome Android 18+, Edge 12+, Firefox 1+, Firefox Android 4+, Safari 2+, Safari iOS 1+

## Example

Wrong:

```css
.card:hover {
  transform: translateY(-4px);
}
```

Right:

```css
@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 `:hover` state.
- 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 `:hover` style has a matching `:focus-visible` style 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.

## Related cards

- [:focus-visible](https://wonderium.dev/css/focus-visible.md)
- [:active](https://wonderium.dev/css/active.md)
- [@media](https://wonderium.dev/css/media.md)
- [transition](https://wonderium.dev/css/transition.md)
- [tooltip](https://wonderium.dev/a11y/tooltip.md)

---

- Level: [CSS](https://wonderium.dev/css.md)
- HTML version: https://wonderium.dev/css/hover
- Last updated: 2026-11-25
