Shadows: box-shadow, text-shadow and Designing Depth
box-shadow offsets, blur, spread, color and inset, multiple shadows and layered elevation, shadows as rings and fake borders, text-shadow for legibility and effects, performance when animating shadows, forced colors, and when to use the drop-shadow() filter instead.
kvi5hnuCode Sprout team member8 min readOn this page
Shadows suggest depth: that a card floats above the page, a menu sits over the content, or a button can be pressed. CSS has two shadow properties: box-shadow draws a shadow of an element's box, and text-shadow a shadow of its text. Neither affects layout, so a shadow never moves anything. Used well, shadows make a hierarchy clear; overused, they make a page look heavy.
box-shadow
.card { box-shadow: 0 4px 12px rgb(0 0 0 / 0.15); }
/* x y blur color */
A shadow is a copy of the element's border box, filled with a color and painted behind it. Its values, in order:
| Value | Effect |
|---|---|
| horizontal offset | positive moves the shadow right; required |
| vertical offset | positive moves it down; required |
| blur radius | how soft the edge is; 0 (default) is a hard edge, larger values spread the blur further; can't be negative |
| spread radius | grows (positive) or shrinks (negative) the shadow before blurring; default 0 |
| color | default currentColor |
inset | draws the shadow inside the box instead of outside |
- The shadow follows
border-radius. - An outer shadow is painted only outside the border box, so it doesn't show through a semi-transparent background. An inset shadow is painted inside, over the background but under the content.
- Shadows don't take space, so they can overflow neighbors, and an ancestor with
overflow: hiddenclips them. Give such a container a little padding. - The default color is the text color, which is rarely what you want for depth. Always set a color, usually black or a dark tint at low opacity.
The blur value isn't a hard size: the shadow fades over a band about as wide as the blur radius, centered on the shadow's edge, half inside and half outside.

In the half-transparent box, the area inside the dashed line stays clear: an outer shadow is painted only outside the border box.
Multiple shadows
A comma-separated list adds several shadows. The first is on top, as with backgrounds (article 49). Realistic depth usually needs two or more: a tight, darker shadow for the contact with the surface, and a wide, faint one for ambient light:
.card {
box-shadow:
0 1px 2px rgb(0 0 0 / 0.12), /* close, crisp */
0 6px 16px rgb(0 0 0 / 0.10); /* far, soft */
}
Elevation scales
Design systems usually define a few elevation levels as custom properties (article 27), so depth is used consistently: the higher a surface, the larger the offset and blur, so the shadow is softer and more spread out:
:root {
--shadow-1: 0 1px 2px rgb(0 0 0 / 0.12), 0 1px 3px rgb(0 0 0 / 0.08);
--shadow-2: 0 2px 4px rgb(0 0 0 / 0.10), 0 6px 16px rgb(0 0 0 / 0.10);
--shadow-3: 0 4px 8px rgb(0 0 0 / 0.10), 0 16px 40px rgb(0 0 0 / 0.14);
}
.card { box-shadow: var(--shadow-1); }
.menu { box-shadow: var(--shadow-2); }
.dialog { box-shadow: var(--shadow-3); }
Light comes from above in most interfaces, so shadows have a vertical offset and little or no horizontal one. Use the same direction everywhere.
Pure black shadows can look dirty on a colored background. A shadow tinted toward the background's hue, such as rgb(80 40 10 / 0.18) on a warm cream page, looks more natural.

The three levels differ mostly in blur and offset, not darkness, and the tinted shadow on the right uses the same opacity as the black one.
body { font: 0.85rem system-ui, sans-serif; margin: 1rem; background: #f4efe6; }
.row { display: grid; grid-template-columns: repeat(auto-fit, minmax(5.5rem, 1fr)); gap: 1rem; margin-bottom: 1.25rem; }
.card { background: white; border-radius: 10px; padding: 1rem 0.5rem; text-align: center; }
.s1 { box-shadow: 0 1px 2px rgb(80 40 10 / 0.14), 0 1px 3px rgb(80 40 10 / 0.08); }
.s2 { box-shadow: 0 2px 4px rgb(80 40 10 / 0.12), 0 6px 16px rgb(80 40 10 / 0.12); }
.s3 { box-shadow: 0 4px 8px rgb(80 40 10 / 0.12), 0 16px 40px rgb(80 40 10 / 0.18); }
.btn {
font: inherit; color: white; border: 0; border-radius: 8px; padding: 0.6rem;
background: #2f7d4f;
box-shadow: 0 2px 0 #1f5636, 0 4px 8px rgb(0 0 0 / 0.15);
}
.btn:active { transform: translateY(2px); box-shadow: 0 0 0 #1f5636, inset 0 2px 4px rgb(0 0 0 / 0.25); }
.ring { box-shadow: 0 0 0 3px #8a3b12; } /* a "border" that takes no space */
.well { background: #ece3d3; box-shadow: inset 0 2px 6px rgb(80 40 10 / 0.25); }
.title { font: 700 2rem Georgia, serif; color: #8a3b12; margin: 0 0 0.75rem; text-shadow: 1px 1px 0 #f4c95d, 2px 2px 0 #2b2118; }
.photo {
block-size: 5rem; border-radius: 10px; display: grid; place-items: center;
background: linear-gradient(90deg, #fff, #7fb4d1 40%, #f4c95d);
}
.photo p { margin: 0; color: white; font-weight: 700; font-size: 1.1rem; text-shadow: 0 1px 3px rgb(0 0 0 / 0.8), 0 0 1px rgb(0 0 0 / 0.9); }
Shadows that aren't shadows
With no offset and no blur, spread alone draws a solid band around the box, following border-radius. It works like a border that takes no space, or an extra outline:
.selected { box-shadow: 0 0 0 3px #8a3b12; } /* a ring */
.stack { box-shadow: 0 0 0 3px white, 0 0 0 6px #8a3b12; } /* two rings */
.bottom { box-shadow: inset 0 -3px 0 #8a3b12; } /* an underline inside the box */
Don't use shadows as the only focus indicator: forced colors mode removes box-shadow (article 42). Pair them with an outline, even a transparent one.
Inset shadows
inset draws the shadow inside the padding box, as if the surface were cut into the page: text fields, wells, pressed buttons. Combine it with outer shadows in the same list; for a pressed state, swap a raised shadow for an inset one.
text-shadow
h1 { text-shadow: 1px 2px 3px rgb(0 0 0 / 0.3); }
/* x y blur color */
text-shadow takes offsets, an optional blur and a color, and multiple comma-separated shadows, first on top. Unlike box-shadow, it has no spread and no inset: text-shadow: 1px 1px 2px 3px red is invalid. It's inherited, unlike box-shadow, so a shadow on a container applies to all the text inside.
Uses:
- Legibility on images: a soft dark shadow, plus a tight one, helps white text on a busy photo. It doesn't replace an overlay where contrast matters: WCAG contrast is measured between the text and its background, and a shadow only helps if it's dense enough to act as a background.
- Effects: layered hard shadows for a 3D or letterpress look, or a light-colored offset for an engraved one.
- Glows: zero offset with a blur,
0 0 8px #f4c95d.
Keep text shadows subtle on body text, where blur makes letters harder to read.
Performance
Shadows are painted, not laid out, so they're cheap to show. Animating box-shadow, such as growing it on hover, makes the browser repaint the element and its shadow on every frame. For a hover lift on many cards, animate transform and the opacity of a pseudo-element that carries the larger shadow:
.card { position: relative; transition: transform 0.2s; }
.card::after {
content: ""; position: absolute; inset: 0; border-radius: inherit;
box-shadow: var(--shadow-3);
opacity: 0; transition: opacity 0.2s;
pointer-events: none;
}
.card:hover { transform: translateY(-2px); }
.card:hover::after { opacity: 1; }
Large blurs on many elements, especially on big boxes, can also slow down scrolling on low-end devices.
box-shadow versus filter: drop-shadow()
box-shadow always has the shape of the box. For an image with transparent areas, such as a PNG logo or an SVG icon, filter: drop-shadow(0 2px 4px rgb(0 0 0 / 0.3)) follows the visible shape instead (article 98). drop-shadow() has no spread or inset and takes one shadow per function, but you can chain several.

The first two panels use the same image: only drop-shadow() sees its transparent pixels. The rest are text-shadow lists, first shadow on top.
Forced colors
In forced colors mode, browsers remove box-shadow and text-shadow. Anything that depends on a shadow to be visible, such as a card's edge on a same-colored page, focus rings or a ring for a selected item, needs a border or outline too, which forced colors keeps.
What's current
box-shadowandtext-shadowwith multiple shadows, spread andinsetwork in all browsers.- Shadow colors can use any color syntax, including
oklch()andcolor-mix(). box-shadowis still removed in forced colors mode, so don't rely on it alone.
Common questions
Why is my shadow cut off?
An ancestor clips overflow (overflow: hidden, auto or clip). Add padding to the container, or move the clipping.
How do I put a shadow on one side only?
Offset the shadow toward that side, and use a negative spread equal to the blur, so it doesn't show on the other sides: box-shadow: 0 8px 6px -6px rgb(0 0 0 / 0.3).
Why does my shadow have the text color?
Without a color, shadows use currentColor.
Can text-shadow have a spread?
No. Use several shadows with small offsets in different directions, or -webkit-text-stroke for an outline.
Should I use box-shadow for borders?
A spread ring is fine where you don't want to change layout, but add a real border or outline for forced colors mode.
Summary
box-shadow: x y blur spread color inset, with multiple shadows separated by commas, first on top. It followsborder-radiusand doesn't affect layout.- Combine a tight and a wide shadow for natural depth; define a few elevation levels and keep the light direction consistent.
- Spread alone makes rings;
insetmakes wells and pressed states. text-shadow: x y blur color, inherited, with no spread orinset.- Animate
transformand a pseudo-element'sopacityrather thanbox-shadow. - Use
drop-shadow()for non-rectangular images. Shadows disappear in forced colors mode.
Practice questions
Write a 2px rust (#8a3b12) ring around a selected card, without changing its size.
Answer
.card.selected { box-shadow: 0 0 0 2px #8a3b12; }
Add outline: 2px solid transparent; too, so the selection still shows in forced colors mode.
What's wrong with this declaration?
.title { text-shadow: 0 2px 4px 2px rgb(0 0 0 / 0.4); }
Answer
text-shadow has no spread value, so four lengths make the declaration invalid. Remove the spread: text-shadow: 0 2px 4px rgb(0 0 0 / 0.4);.
Write a shadow that shows only below a header bar, 8px blurred, with none on the sides.
Answer
header { box-shadow: 0 6px 8px -8px rgb(0 0 0 / 0.4); }
The negative spread shrinks the shadow by as much as the blur grows it, so it only peeks out at the bottom, where it's offset.
A logo PNG with a transparent background gets a rectangular shadow from box-shadow. How do you make the shadow follow the logo's shape?
Answer
.logo { filter: drop-shadow(0 2px 4px rgb(0 0 0 / 0.3)); }
Previous: Radial and Conic Gradients, and Choosing the Interpolation Color Space
Next: Responsive Images: srcset, sizes, picture and image-set()

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.