CSS :hover Selector

Beginner
⏱️ 6 min read
📚 Updated: Jul 2026
🎯 4 Examples
Interaction

What You’ll Learn

The :hover pseudo-class styles elements when the user moves a mouse or pointer over them. It is one of the most common tools for interactive web design.

01

Mouse-over

Pointer on element.

02

Buttons

Color changes.

03

Links

Underline & color.

04

transition

Smooth effects.

05

:active

Press vs hover.

06

Touch tips

Mobile awareness.

Introduction

The :hover selector in CSS applies styles to an element when the user hovers over it with a pointing device, such as a mouse.

It is commonly used to enhance the interactivity of buttons, links, navigation items, and cards by changing their appearance on mouse-over, giving users immediate visual feedback.

Definition and Usage

Append :hover to a selector with no space before the colon: a:hover, button:hover, or .card:hover. Styles apply only while the pointer is over the element and revert when it moves away.

💡
Beginner Tip

Put transition on the base element (e.g. .button), not just on :hover, so the color or size change animates smoothly both when entering and leaving hover.

📝 Syntax

The syntax for the :hover pseudo-class is:

syntax.css
element:hover {
  /* CSS properties */
}

The :hover pseudo-class can be used on any HTML element, though it is most commonly applied to interactive elements like <a>, <button>, and navigation items.

Basic Example

hover-button.css
.button {
  background-color: #2563eb;
  color: #fff;
  padding: 0.65rem 1.25rem;
  border-radius: 0.5rem;
  transition: background-color 0.25s ease;
}

.button:hover {
  background-color: #1d4ed8;
}
a:hover button:hover .card:hover li:hover

Common Properties on Hover

PropertyTypical hover effect
background-colorDarken or lighten button backgrounds
colorChange link text color
text-decorationAdd underline to links
transformSlight lift or scale on cards
box-shadowElevate cards on hover
opacityDim or reveal overlay content

Syntax Rules

  • Hover applies only while the pointer is physically over the element.
  • Chain with other pseudo-classes: button:hover:focus.
  • Place :active after :hover when both change the same property.
  • Use cursor: pointer on clickable elements for better UX.
  • Combine with transition for smooth, professional effects.

Related Selectors

  • :active — styles while the element is being pressed
  • :focus — styles when an element has keyboard focus
  • :link — styles unvisited anchor tags
  • :visited — styles visited anchor tags
  • :focus-visible — keyboard-only focus rings

⚡ Quick Reference

QuestionAnswer
Selector typePseudo-class
When it appliesPointer is over the element
Triggered byMouse, trackpad, or stylus hover
Common ona, button, nav items, cards
Smooth effectsAdd transition on the base selector
Browser supportAll modern browsers

When to Use :hover

:hover improves interactivity and gives users clear feedback:

  • Buttons — Darken or lighten the background so users know the button is interactive.
  • Links — Change color or add an underline to show the text is clickable.
  • Navigation menus — Highlight the menu item under the pointer.
  • Cards and images — Add shadow, scale, or overlay effects on product or gallery tiles.
  • Toolbars and icons — Subtle background changes on icon buttons.

👀 Live Preview

Move your mouse over the button below to see the :hover background change and slight lift:

Examples Gallery

Practice :hover with buttons, links, image cards, and navigation menus.

📜 Core Patterns

Change appearance when the pointer rests on an element.

Example 1 — Button with smooth color transition

Create a button-like link that darkens on hover with a smooth transition.

hover-button.html
<style>
  .button {
    background-color: #2563eb;
    color: #fff;
    padding: 0.65rem 1.25rem;
    text-decoration: none;
    border-radius: 0.5rem;
    display: inline-block;
    transition: background-color 0.3s ease;
  }
  .button:hover {
    background-color: #1d4ed8;
  }
</style>

<a href="#" class="button">Hover over me</a>
Try It Yourself

How It Works

The base .button sets the default blue background and a transition. When the pointer enters, .button:hover applies the darker shade smoothly.

📄 Cards & Navigation

Apply hover effects to layout components and menus.

Example 3 — Image card lift on hover

Elevate a card with shadow and a slight upward shift when hovered.

hover-card.css
.card {
  border: 1px solid #e2e8f0;
  border-radius: 0.65rem;
  padding: 1rem;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.12);
}
Try It Yourself

How It Works

transform: translateY(-4px) lifts the card while box-shadow adds depth. Both animate smoothly thanks to transition on the base .card.

Example 4 — Navigation menu item highlight

Highlight menu links with a background when the pointer is over them.

hover-nav.css
nav a {
  color: #334155;
  text-decoration: none;
  padding: 0.5rem 0.75rem;
  border-radius: 0.4rem;
  transition: background 0.15s ease;
}

nav a:hover {
  background: #eff6ff;
  color: #2563eb;
}
Try It Yourself

How It Works

Each nav link gets a subtle background and blue text on hover, making it clear which item the pointer is over.

💬 Usage Tips

  • Any element — Use :hover on div, cards, and images for rich interactive effects, not just links and buttons.
  • Smooth transitions — Combine with transition or animation for polished color, shadow, and transform changes.
  • Layer with :active — Add :active after :hover for a pressed-state color when the user clicks.
  • cursor: pointer — Set on clickable elements so the mouse icon indicates interactivity.
  • Touch devices — Do not hide essential actions behind hover-only UI; provide tap-friendly alternatives on mobile.

⚠️ Common Pitfalls

  • Mobile and touch:hover does not work reliably on touchscreens. Never depend on hover alone for critical functionality.
  • Over-animation — Too many hover animations can distract users and hurt performance on low-end devices.
  • Missing :focus styles — Keyboard users cannot hover; always pair interactive hover effects with :focus styles.
  • Specificity order — If :active is declared before :hover, hover may override the pressed state unexpectedly.
  • Sticky hover on touch — On some mobile browsers, the first tap triggers hover and a second tap activates the link. Test on real devices.

♿ Accessibility

  • Do not rely on hover alone — Essential information and actions must be available without hovering.
  • Provide :focus styles — Mirror or complement hover styles with visible :focus or :focus-visible indicators for keyboard users.
  • Sufficient contrast — Hover color changes must still meet WCAG contrast requirements.
  • Large touch targets — On mobile, ensure buttons and links are easy to tap without needing hover feedback.
  • Reduced motion — Consider @media (prefers-reduced-motion: reduce) to disable large hover animations for sensitive users.

🧠 How :hover Works

1

Pointer enters element

The user moves a mouse, trackpad, or stylus over the target element.

Enter
2

:hover rule matches

The browser applies styles from element:hover declarations.

Match
3

Visual feedback renders

Color, shadow, or transform changes give the user immediate feedback.

Render
=

Interactive experience

Users know which elements respond to their pointer.

🖥 Browser Compatibility

The :hover pseudo-class is supported in all modern browsers on desktop and mobile.

Baseline · Universal support

Hover styling everywhere

:hover works reliably in Chrome, Firefox, Safari, Edge, and Opera.

99% Universal support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions
Full support
Opera All modern versions
Full support
:hover pseudo-class 99% supported

Bottom line: :hover is safe everywhere. Remember touch devices may not trigger hover consistently.

🎉 Conclusion

The :hover selector is a simple yet powerful tool in CSS that helps create dynamic, interactive web pages. Whether it is changing the color of a button or adding a visual effect to cards, :hover enhances user interaction with immediate visual feedback.

Combine it with transition for smooth effects, pair it with :focus and :active for complete interaction coverage, and always consider touch-device users when designing hover-dependent UI.

💡 Best Practices

✅ Do

  • Use transition for smooth hover effects
  • Pair hover with :focus for keyboard users
  • Set cursor: pointer on clickable elements
  • Keep hover changes subtle and purposeful
  • Place :active after :hover in CSS

❌ Don’t

  • Hide essential actions behind hover-only UI
  • Overuse heavy animations on every element
  • Forget mobile and touch device behavior
  • Skip focus styles when styling hover
  • Confuse :hover with :active press states

Key Takeaways

Knowledge Unlocked

Five things to remember about :hover

Use these points when adding mouse-over effects.

5
Core concepts
🎨 02

Visual feedback

Color & shadow.

Effect
03

transition

Smooth changes.

Tip
:active 04

Press state

Hover vs click.

Pair
🌐 05

99% support

All browsers.

Compat

❓ Frequently Asked Questions

The :hover pseudo-class applies styles to an element when the user moves a pointing device (such as a mouse) over it. It provides immediate visual feedback for interactive elements like buttons and links.
:hover is designed for pointer devices like a mouse. On touchscreens, hover may not trigger consistently, or it may stick after a tap. Do not rely on hover alone for essential functionality on mobile.
:hover applies while the pointer rests over an element. :active applies only while the user is pressing or clicking. Use both together for polished button interactions.
Yes. While :hover is most common on links and buttons, it works on any element including div, img, and li. This enables card highlights, image zoom effects, and menu styling.
Yes, when you want smooth color, size, or shadow changes. Add transition on the base selector (not only :hover) so the effect animates both in and out.

Practice in the Live Editor

Open the HTML editor and experiment with :hover, transitions, and interactive button styling.

HTML Editor →

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.

5 people found this page helpful