CSS Shadows
Shadows add depth. They make cards float above the page, buttons feel pressable, and text readable on busy backgrounds. CSS has two shadow properties, box-shadow for boxes and text-shadow for text, plus the drop-shadow() filter for irregular shapes.
box-shadow syntax
box-shadow: [inset] offset-x offset-y [blur] [spread] [color];| Part | Meaning |
|---|---|
offset-x |
Horizontal shift (positive = right) |
offset-y |
Vertical shift (positive = down) |
blur |
Blur radius; 0 gives a sharp edge (optional) |
spread |
Grows (positive) or shrinks (negative) the shadow (optional) |
color |
Shadow colour; semi-transparent black looks most natural |
inset |
Draws the shadow inside the box |
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; display: flex; flex-wrap: wrap; gap: 26px; padding: 20px; background: #f5f5f5; }
div { width: 100px; height: 70px; background: white; border-radius: 10px; display: grid; place-items: center; font-size: 13px; text-align: center; }
.a { box-shadow: 6px 6px #7e57c2; }
.b { box-shadow: 6px 6px 10px rgba(0,0,0,.35); }
.c { box-shadow: 0 0 0 6px #26a69a; }
.d { box-shadow: 0 10px 20px -6px rgba(0,0,0,.5); }
.e { box-shadow: inset 0 4px 10px rgba(0,0,0,.35); }
.f { box-shadow: 0 0 20px 4px #ff4081; }
</style>
</head>
<body>
<div class="a">hard shadow</div>
<div class="b">blurred</div>
<div class="c">spread as ring</div>
<div class="d">negative spread</div>
<div class="e">inset</div>
<div class="f">glow</div>
</body>
</html>Tip:
box-shadow: 0 0 0 4px color(no blur, only spread) draws a solid ring that followsborder-radiusand takes up no layout space. It's a popular way to style focus rings.
Layered shadows
You can list several shadows, separated by commas. The first one is drawn on top. Real objects cast a tight, dark shadow close to them and a soft, faint one further away, so stacking two or three shadows looks far more natural than one.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; display: flex; gap: 30px; padding: 30px; background: #eef1f5; }
.card { width: 140px; height: 100px; background: white; border-radius: 14px; padding: 12px; box-sizing: border-box; transition: box-shadow .25s, transform .25s; }
.flat { box-shadow: 0 2px 6px rgba(0,0,0,.3); }
.layered { box-shadow: 0 1px 2px rgba(0,0,0,.12), 0 4px 8px rgba(0,0,0,.10), 0 16px 32px rgba(0,0,0,.10); }
.layered:hover { transform: translateY(-4px); box-shadow: 0 2px 4px rgba(0,0,0,.12), 0 8px 16px rgba(0,0,0,.12), 0 28px 48px rgba(0,0,0,.14); }
</style>
</head>
<body>
<div class="card flat">One shadow</div>
<div class="card layered">Three layered shadows. Hover me!</div>
</body>
</html>text-shadow
text-shadow takes offset-x offset-y blur color, with no spread and no inset. Several shadows can be layered here too.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; text-align: center; margin: 0; }
.light { padding: 10px; font-size: 34px; font-weight: 800; color: #ffca28; text-shadow: 3px 3px 0 #e65100; }
.dark { background: #1a1a2e; padding: 12px; }
.neon { font-size: 34px; color: #fff; text-shadow: 0 0 4px #fff, 0 0 12px #00e5ff, 0 0 24px #00e5ff; }
.photo { padding: 18px; background: linear-gradient(135deg, #fff59d, #f48fb1, #81d4fa); }
.readable { color: white; font-size: 24px; text-shadow: 0 1px 3px rgba(0,0,0,.8); }
</style>
</head>
<body>
<div class="light">Retro</div>
<div class="dark"><span class="neon">NEON</span></div>
<div class="photo"><span class="readable">White text stays readable</span></div>
</body>
</html>drop-shadow for shapes
box-shadow always follows the rectangular box (rounded by border-radius). For transparent images, SVGs and clipped shapes, use filter: drop-shadow(...). It follows the actual visible pixels.
<!DOCTYPE html>
<html>
<head>
<style>
body { display: flex; gap: 40px; padding: 30px; font-family: system-ui, sans-serif; }
.star { width: 110px; height: 110px; background: #ffc107;
clip-path: polygon(50% 0, 61% 35%, 98% 35%, 68% 57%, 79% 91%, 50% 70%, 21% 91%, 32% 57%, 2% 35%, 39% 35%); }
.wrap-box { box-shadow: 8px 8px 8px rgba(0,0,0,.5); }
.wrap-drop { filter: drop-shadow(8px 8px 6px rgba(0,0,0,.5)); }
p { text-align: center; font-size: 13px; }
</style>
</head>
<body>
<div><div class="wrap-box"><div class="star"></div></div><p>box-shadow: a rectangle</p></div>
<div><div class="wrap-drop"><div class="star"></div></div><p>drop-shadow: the star</p></div>
</body>
</html>Note: Large, blurred shadows on many elements can slow down scrolling and animations. Animating
transformandopacityis cheaper than animatingbox-shadow.
Test yourself
In box-shadow: 4px 8px 12px 2px black;, what is 12px?
- Spread
- Blur radius
- Vertical offset
- Horizontal offset
Which keyword places a box-shadow inside the element?
- inner
- inside
- inset
- within
Which should you use to shadow a star made with clip-path?
- box-shadow
- text-shadow
- filter: drop-shadow()
- outline