CSS Box Shadow Guide: How to Make Realistic, Layered Shadows
Shadows are how flat screens suggest depth. A card that floats above the page, a menu that sits on top of content, a button that looks pressable: all of them depend on shadows. CSS box-shadow is easy to write but surprisingly easy to get wrong. The difference between a cheap-looking shadow and a convincing one usually comes down to blur, opacity and layering.
The syntax
box-shadow: [inset] offset-x offset-y blur-radius spread-radius color;- offset-x / offset-y: how far the shadow moves right and down. Negative values move it left or up.
- blur-radius: how soft the edge is. 0 is a hard edge. It can’t be negative.
- spread-radius: grows (positive) or shrinks (negative) the shadow before blurring.
- color: usually a semi-transparent black or tinted dark color.
- inset: draws the shadow inside the element, as if it were pressed in.
You can list several shadows separated by commas. The first one is drawn on top.
Why most shadows look fake
The default shadow many people write, box-shadow: 0 0 10px #000, looks like a dark halo rather than a shadow. Real shadows have three properties that halo shadows lack:
- Direction. Light usually comes from above, so shadows fall downward. Use a positive Y offset and little or no X offset.
- Softness proportional to distance. An object close to a surface casts a sharp, small shadow; an object higher up casts a larger, softer, fainter one.
- Low opacity. Real ambient shadows are subtle. Opacities of 5–20% look natural; 50%+ looks heavy.
Layered shadows
A single blur can’t reproduce how real shadows fall off: dark and tight near the object, light and wide further away. Stacking several shadows with increasing offset and blur does:
box-shadow:
0 1px 2px rgb(0 0 0 / 0.07),
0 2px 4px rgb(0 0 0 / 0.07),
0 4px 8px rgb(0 0 0 / 0.07),
0 8px 16px rgb(0 0 0 / 0.07);Each layer doubles the previous offset and blur. Together they create a smooth, realistic falloff that no single shadow can match. This is the idea behind the “layered” option in the box shadow generator above.
Single versus layered shadows
| Aspect | Single shadow | Layered shadows |
|---|---|---|
| Realism | Often flat or halo-like | Natural falloff |
| Code length | One line | 3–6 lines |
| Rendering cost | Lowest | Slightly higher, still cheap for static elements |
| Best for | Small elements, subtle borders | Cards, modals, floating panels |
Building an elevation system
Instead of inventing a new shadow for every component, define a small set of elevation levels and reuse them. Material Design popularised this idea: the higher an element is, the larger and softer its shadow.
| Level | Example components | Character |
|---|---|---|
| 0 | Page background, flat cards with borders | No shadow |
| 1 | Cards, list items on hover | Tight, very faint |
| 2 | Raised cards, sticky headers | Small and soft |
| 3 | Dropdowns, popovers | Medium spread |
| 4 | Modals, dialogs | Large, soft, a bit darker |
Store these as tokens (--shadow-1 to --shadow-4) and use the same light direction for all of them. When every shadow in an interface agrees on where the light is, the result feels coherent, even if users never consciously notice it.
Tinted shadows
Shadows in the real world pick up color from the surface and surrounding light. On a warm beige background, a pure black shadow can look grey and dirty. Use a dark version of the background hue instead:
box-shadow: 0 8px 24px rgb(60 40 10 / 0.15);On colored buttons, a shadow tinted with the button color looks like colored light spilling onto the page, an effect used in many modern interfaces.
Inset shadows
inset draws the shadow inside the box. Common uses:
- pressed states for buttons and toggles,
- inner depth for input fields,
- neumorphic soft UI,
- inner borders that don’t affect layout:
box-shadow: inset 0 0 0 1px #ddd.
That last trick is handy because shadows never change an element’s size, unlike borders.
Shadows and accessibility
Shadows alone are not enough to show that something is interactive or separate. Users with low vision, and anyone on a low-contrast screen, may not see a faint shadow. Pair shadows with a subtle border or background difference for important boundaries. WCAG’s non-text contrast rule (3:1) applies to the visual information needed to identify a component, which a 10% shadow rarely provides.
Why animating box-shadow gets janky
Static box shadows are cheap. Problems appear when you animate them: changing box-shadow on hover forces the browser to repaint the shadow on every frame. For smooth hover effects on many cards:
.card { position: relative; }
.card::after {
content: ""; position: absolute; inset: 0; border-radius: inherit;
box-shadow: 0 12px 32px rgb(0 0 0 / 0.18);
opacity: 0; transition: opacity .2s;
}
.card:hover::after { opacity: 1; }Animating opacity is handled by the compositor and stays smooth.
box-shadow versus filter: drop-shadow()
| Feature | box-shadow | filter: drop-shadow() |
|---|---|---|
| Follows | The element’s box (and border-radius) | The actual visible shape, including transparent PNGs and clip-path |
| Spread and inset | Yes | No |
| Multiple shadows | Comma-separated | Chain several drop-shadow() functions |
| Typical use | Cards, buttons, panels | Icons, irregular shapes, clipped elements |
If you use clip-path to cut a shape, box-shadow gets clipped too; apply drop-shadow() to a wrapper element instead.
Shadow values to copy
- Subtle card:
0 1px 3px rgb(0 0 0 / 0.08), 0 4px 12px rgb(0 0 0 / 0.05) - Floating modal:
0 24px 48px -12px rgb(0 0 0 / 0.25) - Focus ring:
0 0 0 3px rgb(138 109 59 / 0.45) - Pressed button:
inset 0 2px 4px rgb(0 0 0 / 0.15)