Card with border-radius and box-shadow in CSS
27 A card with a radius and a shadow
easyAssemble a small card: rounded corners, a two-layer shadow, and inner spacing that does not fight the border. Keep the heading order sane.
Stuck? Three hints, smallest first
- 1.
border-radiustakes one value for all corners, or four starting at the top left. - 2. Two shadows read better than one: a tight dark layer for the edge, a wide soft one for the lift.
- 3.
box-shadowis drawn outside the border box, so it needs no space reserved for it.
<article class="card">
<h3 class="card__title">Lagos to Abuja</h3>
<p class="card__meta">Departs 06:30 · Bay B</p>
<p class="card__price">₦18,000</p>
<a class="card__cta" href="#book">Reserve a seat</a>
</article>
.card {
box-sizing: border-box;
max-width: 240px;
padding: 16px;
border: 1px solid #e4e5e9;
border-radius: 12px;
background: #ffffff;
/* Two layers: a tight one for the edge, a soft one for the lift. */
box-shadow: 0 1px 2px rgb(23 23 26 / 8%), 0 10px 24px rgb(23 23 26 / 10%);
}
.card__title { margin: 0 0 4px; font-size: 1rem; }
.card__meta { margin: 0 0 10px; color: #5c5c66; font-size: 0.85rem; }
.card__price { margin: 0 0 12px; font-size: 1.25rem; font-weight: 700; }
.card__cta {
display: inline-block;
padding: 8px 14px;
border-radius: 8px;
background: #3055d8;
color: #ffffff;
text-decoration: none;
font-size: 0.9rem;
}
.card__cta:hover { background: #2543b4; }
.card__cta:focus-visible {
outline: 2px solid #17171a;
outline-offset: 2px;
}
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.
// Count the shadow layers by splitting on the commas that separate them.
const shadow = (/box-shadow:\s*([^;]+);/.exec(css) || [, ''])[1];
const layers = shadow.split(/,(?![^(]*\))/).length;
test('the corners are rounded', /border-radius:\s*12px/.test(css), true);
test('the shadow has two layers', layers, 2);
test('both are drawn with an alpha channel', (shadow.match(/rgb\(/g) || []).length, 2);
test('the card is border-box, so padding stays inside the width', /box-sizing:\s*border-box/.test(css), true);
test('the call to action is a link, since it navigates', /<a class="card__cta"/.test(html), true);
test('it has a hover state', /\.card__cta:hover/.test(css), true);
test('and a visible focus state, with the outline replaced not removed', /:focus-visible/.test(css) && /outline:\s*none/.test(css) === false, true);
test('the heading is an h3, following the h2 sections above it', hasTag(html, 'h3'), true);
Output
box-shadow is painted outside the border box and takes part in no layout at all, so it never pushes a neighbour or changes a width — which is why it is safe to animate and why two cards side by side can overlap shadows without moving. Two layers is the trick worth copying: a 1–2px shadow at low alpha defines the edge, and a wide one at low alpha suggests height. One big soft shadow on its own reads as blur. Since the action here navigates rather than acts, it is an <a>, not a <button>.