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.
a:hover { text-decoration: underline; }
Try it
Moving the pointer over the hint highlights the card too, because the card is an ancestor of the hovered element.
Syntax and arguments
: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
:hoverwith: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,:activein 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-withinor 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
:hoverin most browsers, so style it with:not(:disabled):hoverif you want no effect. pointer-events: noneelements are never hovered.- Performance. Animate cheap properties such as
transformandopacityin hover effects.
Browser support
Supported in every browser. The hover media feature is also supported in all current browsers.