Card hover lift effect in CSS
9 A card that lifts on hover
easyGive a card a shadow that deepens and a slight lift when hovered. Keep it smooth and keep it accessible.
Stuck? Three hints, smallest first
- 1. Animate
transformandbox-shadow, nevertopormargin. - 2.
translateY(-4px)is the lift. Small is better than dramatic. - 3. Add
:focus-withinso a keyboard user tabbing to a link inside the card gets the same feedback.
<article class="card">
<h3>Lagos to Abuja</h3>
<p>Departs 06:30 · ₦18,000</p>
<a href="#">Book a seat</a>
</article>
.card {
max-width: 260px;
padding: 16px;
border: 1px solid #e4e5e9;
border-radius: 10px;
background: #fff;
box-shadow: 0 1px 2px rgb(0 0 0 / 6%);
/* Only transform and box-shadow animate: both are cheap for the browser. */
transition: transform 180ms ease, box-shadow 180ms ease;
}
.card:hover,
.card:focus-within {
transform: translateY(-4px);
box-shadow: 0 8px 20px rgb(0 0 0 / 12%);
}
.card h3 { margin: 0 0 6px; font-size: 1rem; }
.card p { margin: 0 0 10px; color: #5b5c63; }
@media (prefers-reduced-motion: reduce) {
.card { transition: none; }
}
8 checks that prove this answer
Assertions, run in the browser. The markup and styles above are handed to them
as html and css, so press Run and each check prints
ok. The build fails if a single one fails.
test('the lift uses transform', /translateY\(-4px\)/.test(css), true);
test('the shadow deepens', (css.match(/box-shadow\s*:/g) || []).length >= 2, true);
test('only cheap properties animate', /transition\s*:\s*transform[^;]*box-shadow/.test(css), true);
test('top is not animated', /transition[^;]*\btop\b/.test(css), false);
test('keyboard focus inside the card counts too', /:focus-within/.test(css), true);
test('reduced motion is honoured', /prefers-reduced-motion/.test(css), true);
test('a semantic article is used', hasTag(html, 'article'), true);
test('and it contains a heading and a link', hasTag(html, 'h3') && hasTag(html, 'a'), true);
Output
:focus-within is the part worth stealing. A hover-only card gives a mouse user feedback and a keyboard user none, even though tabbing to the link inside is the same intent. :focus-within matches the card whenever anything inside it has focus, so one extra selector covers both.