How to Add a Hover Effect in CSS
Use the :hover pseudo-class when you want elements to react as the pointer moves over them. Hover effects help users understand what is clickable and add visual feedback to buttons, cards, links, images, and menus.
What you’ll build or solve
You’ll learn how to add a hover effect in CSS using the :hover state. You’ll also know how to create clear feedback without making the UI feel distracting.
Learn CSS on Mimo
When this approach works best
This approach is the right choice when users interact with desktop or laptop interfaces using a mouse or trackpad.
Common real-world scenarios include:
- Button hover states
- Link feedback
- Card elevation
- Image zoom previews
- Navigation menus
This is a bad idea when hover is the only interaction signal, because touch devices do not support hover in the same way.
Prerequisites
You only need:
- A basic HTML element
- A text editor
- Basic HTML and CSS knowledge
Step-by-step instructions
Step 1: Add a :hover state to the target element
Start by styling the default state.
HTML
<button class="cta">USA pricing</button>
<style>
.cta {
background: #0057ff;
color: white;
}
</style>
Then add the hover effect.
HTML
<style>
.cta:hover {
background: #003bb3;
}
</style>
You can also use hover for cards or other UI blocks.
HTML
<div class="card">France plan</div>
<style>
.card:hover {
box-shadow: 0 6px 18px rgba(0, 0, 0, 0.15);
}
</style>
What to look for:
:hoveronly changes the hovered element- Color, shadow, scale, and underline are common hover effects
- Keep the hover difference noticeable
- Avoid overly aggressive movement
- Always keep the default state usable on touch devices
Examples you can copy
Button hover
HTML
<button class="cta">Start lesson</button>
<style>
.cta:hover {
background: #0040cc;
}
</style>
Link hover
HTML
<a href="/docs" class="nav-link">UK docs</a>
<style>
.nav-link:hover {
color: #0057ff;
}
</style>
Card hover shadow
HTML
<div class="card">USA cloud plan</div>
<style>
.card:hover {
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.12);
}
</style>
Common mistakes and how to fix them
Mistake 1: No visible difference on hover
What the reader might do:
HTML
.button:hover {
background: #0057ff;
}
Why it breaks: if the default state already uses the same color, users see no feedback.
Corrected approach:
HTML
.button:hover {
background: #003bb3;
}
Mistake 2: Making hover too dramatic
What the reader might do:
HTML
.card:hover {
transform: scale(1.4);
}
Why it breaks: large jumps can disrupt layout and feel distracting.
Corrected approach:
HTML
.card:hover {
transform: scale(1.02);
}
Mistake 3: Using hover as the only interaction cue
What the reader might do:
HTML
a {
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
Why it breaks: touch users may never see the hover state.
Corrected approach:
Keep a visible default cue like color contrast or underline.
Troubleshooting
If hover does not trigger, confirm the selector targets the correct class.
If the effect feels too weak, increase the contrast difference.
If the layout shifts, reduce transform scale values.
If touch users lose context, make the default state more obvious.
Quick recap
- Use
:hoverfor pointer interaction feedback - Change color, shadow, or scale
- Keep the effect noticeable but subtle
- Avoid layout-breaking transforms
- Do not rely only on hover for usability
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot