CSS :hover Pseudo-Class

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The :hover pseudo-class matches an element while a pointing device is over it — usually when the mouse cursor rests on the element without pressing.

Use it for feedback on links, buttons, and cards. For links, place :hover after :link / :visited and before :active (LVHA order).

Baseline Widely available.

Long-supported CSS pseudo-class across modern browsers.

Warning — touchscreens.

On touch devices, :hover may never match, match briefly, or stick after a tap. Do not hide essential content behind hover alone. Prefer @media (hover: hover) for hover-only extras.

CSS Syntax

syntax.css
:hover { /* styles */ }

Often combined with a type or class: a:hover, .btn:hover.

Selector Patterns

PatternDescriptionTry it
a:hoverLink while the pointer is over itTry it
.btn:hoverButton hover feedbackTry it
LVHA order:link, :visited, :hover, :activeTry it
child on parent hoverStyle a child when the parent is hoveredTry it
with transitionSmooth color change on hoverTry it
@media (hover: hover)Apply hover styles only when hover is availableTry it

How to Use :hover

Add a default style, then override it under :hover.

  1. Style the element’s normal state.
  2. Add selector:hover { … } for pointer-over feedback.
  3. For links, keep LVHA order so hover and active still show.
  4. Keep critical UI usable without hover (keyboard and touch).
PieceRequired?Role
Base selectorUsually yesElement that can be hovered
:hoverYesMatches while the pointer is over it
LVHA orderFor linksPrevents later link states from hiding hover
Hover media queryOptionalLimits hover-only styles to capable devices

Live Preview

Move the pointer over the samples. Switch the hover style with the controls.

Hover the button and link to see the selected :hover effect.

Hover preview

Rule: background changes on :hover

CSS Examples

Each snippet focuses on :hover. Use Try It Yourself for a live editor.

Example 2 — Button Hover

Give a button clear pointer feedback.

CSS
.btn {
  background: #2563eb;
  color: #fff;
}

.btn:hover {
  background: #1d4ed8;
}
Try It Yourself

How It Works

  1. Set the base look. .btn defines the resting style.
  2. Darken on hover. .btn:hover shows the control is interactive.
  3. Pair with focus. Also style :focus for keyboard users.
  4. Keep contrast. Hover colors must stay readable.

Example 3 — Style a Child on Parent Hover

Hover the card to highlight text inside it.

CSS
.card:hover .label {
  color: #1d4ed8;
  font-weight: bold;
}
Try It Yourself

How It Works

  1. Hover the parent. .card:hover matches the card under the pointer.
  2. Target a child. .label inside that card gets the new styles.
  3. Leave other cards. Labels elsewhere stay unchanged.
  4. Mind touch. Parent-hover menus need a non-hover path on phones.

Edge Cases & Pitfalls

Most hover bugs are touch behavior, link order, or inaccessible UI.

touch

Unreliable on touchscreens

Do not require hover to reveal navigation or critical content. Offer click/tap and keyboard paths too.

lvha

Wrong link order hides hover

If :link or :visited comes after :hover with equal specificity, hover styles may not show. Use LVHA.

a11y

Keyboard users need focus styles

:hover does not help keyboard navigation. Pair with :focus (and ideally :focus-visible).

media

Gate decorative hover effects

Wrap hover-only polish in @media (hover: hover) when it is not essential.

vs active

Hover is not press

Use :active for the pressed state. It should come after :hover for links.

Browser Support

:hover is a browser CSS pseudo-class. Node.js, Deno, and Bun do not evaluate it.

✓ Baseline · Widely available

:hover

Supported in all major browsers for decades (Baseline Widely available since about July 2015).

99% All modern browsers
Google Chrome All
Full
Mozilla Firefox All
Full
Apple Safari All
Full
Microsoft Edge All
Full
Opera All
Full
Samsung Internet All
Full
Internet Explorer All
Full
:hover Universally supported

Bottom line: Production-ready. Plan for touch and keyboard separately from hover. Not a server runtime API.

Key Takeaways

  • Role: style an element while the pointer is over it.
  • Links: LVHA — :link, :visited, :hover, :active.
  • Touch: do not hide essential UI behind hover alone.
  • Tip: pair with :focus and optional @media (hover: hover).

One line: selector:hover styles the element while the pointer is over it.

Frequently Asked Questions

It matches an element while a pointing device (usually a mouse pointer) is over that element, without requiring a click.
:hover applies while the pointer is over the element. :active applies only while the element is being pressed.
For links, write :link, then :visited, then :hover, then :active. That order keeps hover and active styles from being overridden incorrectly.
It is unreliable. Some browsers never match :hover on touch, others match briefly or stick until another tap. Do not rely on hover alone for essential UI.
Yes. Use a media feature such as @media (hover: hover) so hover-only styles apply on devices that can hover.
No. It is a browser CSS pseudo-class, not a Node.js feature.

Next: :in-range

Style form controls whose values fall inside their allowed range.

:in-range tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful