Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Pseudo-classes

The :hover pseudo-class

Reference for the CSS :hover pseudo-class: style elements while a pointer is over them, with touch-device behavior, hover media queries and accessibility tips.

The :hover pseudo-class matches an element while a pointing device is over it, for example when the mouse cursor rests on a button or link. It also matches the element's ancestors, because pointing at a child also points at its parents.

It is used for feedback: color changes on links, raised cards, menu reveals. Because it depends on a pointer, hover must never be the only way to reach content or to understand a control.

CSS
a:hover {
  text-decoration: underline;
}

Try it

Hover feedback

Moving the pointer over the hint highlights the card too, because the card is an ancestor of the hovered element.

Syntax and arguments

CSS
:hover { }
a:hover { }
.card:hover .title { }
@media (hover: hover) {
  .btn:hover { background: #1864ab; }
}
Part Description
:hover Matches while a pointing device designates the element (or a descendant) without necessarily activating it.
@media (hover: hover) Applies rules only when the primary input can hover (mouse, trackpad).
@media (any-hover: hover) True when any available input can hover.

Specificity is 0,1,0, the same as a class.

Usage notes

  • Touch screens. There is no real hover on touch. Mobile browsers usually emulate it on tap and may keep the hover state "stuck" until the next tap. Wrap decorative hover effects in @media (hover: hover).
  • Keep keyboard users in mind. Pair :hover with :focus-visible, as in .btn:hover, .btn:focus-visible { }, so keyboard users get the same feedback.
  • Link order (LVHA). For links, declare :link, :visited, :hover, :active in that order, because they have equal specificity and later rules win.
  • Do not hide essentials. Content that appears only on hover (tooltips, menus) must also be reachable by keyboard and touch. Use :focus-within or a button that toggles it.
  • Gaps cancel hover. A flyout menu that is separated from its trigger by empty space loses the hover state when the pointer crosses the gap. Close the gap with padding instead of margin, or use a short transition delay.
  • Disabled controls. A disabled button still matches :hover in most browsers, so style it with :not(:disabled):hover if you want no effect.
  • pointer-events: none elements are never hovered.
  • Performance. Animate cheap properties such as transform and opacity in hover effects.

Browser support

Supported in every browser. The hover media feature is also supported in all current browsers.