CSS techniques

Layered box shadows: making CSS shadows look real

One box-shadow looks flat. Layering several soft shadows, each larger and fainter than the last, gives depth that looks like real light. Here is how, with an elevation scale you can reuse.

To make a CSS box shadow look realistic, layer several shadows instead of using one. Each layer gets a larger offset and blur and a lower opacity, so the shadow is crisp and dark close to the element and soft and faint further away, the way real shadows fall.

This guide covers the box-shadow syntax, the layering technique, how to build a consistent set of elevations, colour and dark mode, and how to animate shadows without hurting performance.

Key takeaways

  • A single heavy shadow looks flat and smudgy. Three to five lighter layers look natural.
  • Double the offset and blur at each layer and keep every layer’s opacity low.
  • Tint shadows with the background’s hue instead of pure black.
  • Keep the light direction consistent across the whole interface.
  • Don’t animate box-shadow directly. Fade a pseudo-element’s opacity instead.

The box-shadow syntax

Each shadow takes up to five values plus an optional inset keyword:

box-shadow: [inset] offset-x offset-y blur-radius spread-radius color;

/* Example: 0 horizontal, 4px down, 12px blur, no spread */
box-shadow: 0 4px 12px 0 rgb(15 23 42 / 0.15);
  • Offset moves the shadow. Positive y pushes it down, which suggests light from above.
  • Blur softens the edge. Larger values spread the shadow over a wider area.
  • Spread grows or shrinks the shadow before blurring. Negative spread keeps large blurs from spilling out to the sides.
  • Multiple shadows are separated by commas. The first one listed is drawn on top.

Why one shadow looks fake

Real shadows have a dark, sharp core where an object is close to a surface and a wide, faint fringe further out. One box-shadow can only have one blur and one opacity, so you have to choose: tight and dark, which looks like a sticker, or wide and soft, which looks like a smudge.

Layering gives you both. The idea was popularised by Tobias Ahlin’s article on layered box-shadows, and it’s now how most design systems build elevation.

/* One shadow */
.card-single {
  box-shadow: 0 16px 32px rgb(15 23 42 / 0.25);
}

/* The same depth, layered */
.card-layered {
  box-shadow:
    0 1px 1px rgb(15 23 42 / 0.06),
    0 2px 2px rgb(15 23 42 / 0.06),
    0 4px 4px rgb(15 23 42 / 0.06),
    0 8px 8px rgb(15 23 42 / 0.06),
    0 16px 16px rgb(15 23 42 / 0.06);
}

Each layer doubles the offset and blur of the one before. The low opacities add up where the layers overlap near the element, giving a darker core that fades out naturally.

Building an elevation scale

Most interfaces need only three or four levels: resting cards, raised elements such as dropdowns, and floating ones such as modals. Define them once as custom properties and reuse them:

:root {
  --shadow-color: 222 47% 11%; /* hue, saturation, lightness */
  --shadow-sm:
    0 1px 1px hsl(var(--shadow-color) / 0.07),
    0 2px 2px hsl(var(--shadow-color) / 0.07);
  --shadow-md:
    0 1px 1px hsl(var(--shadow-color) / 0.06),
    0 2px 2px hsl(var(--shadow-color) / 0.06),
    0 4px 4px hsl(var(--shadow-color) / 0.06),
    0 8px 8px hsl(var(--shadow-color) / 0.06);
  --shadow-lg:
    0 2px 2px hsl(var(--shadow-color) / 0.05),
    0 4px 4px hsl(var(--shadow-color) / 0.05),
    0 8px 8px hsl(var(--shadow-color) / 0.05),
    0 16px 16px hsl(var(--shadow-color) / 0.05),
    0 32px 32px hsl(var(--shadow-color) / 0.05);
}

.card  { box-shadow: var(--shadow-sm); }
.menu  { box-shadow: var(--shadow-md); }
.modal { box-shadow: var(--shadow-lg); }

Higher elevations use more and larger layers. Keeping the same direction and ratios at every level is what makes the set feel consistent.

Colour, light direction and dark mode

  • Tint the shadow. Pure black shadows look grey and dirty on coloured backgrounds. Use a dark version of the background’s hue, as with --shadow-color above. The OKLCH palette guide shows how to pick dark shades that stay in the same hue.
  • One light source. If cards cast shadows straight down but buttons cast them to the right, the page feels wrong without anyone knowing why. Keep x offsets at 0, or the same ratio, everywhere.
  • Dark mode. Shadows barely show on dark backgrounds. Make raised surfaces slightly lighter than the page instead, and add a faint border or highlight. Keep shadows for separation, not as the main depth cue.

Tip: Use a negative spread on large, soft shadows, such as 0 24px 48px -12px. It pulls the shadow in at the sides, so it reads as depth below the element rather than a glow around it.

Animating shadows without jank

Changing box-shadow on hover forces the browser to repaint the shadow on every frame, which can stutter on slower devices, especially for large blurs. A smoother approach is to put the bigger shadow on a pseudo-element and fade its opacity, which the browser can usually animate on the GPU:

.card {
  position: relative;
  box-shadow: var(--shadow-sm);
}
.card::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  box-shadow: var(--shadow-lg);
  opacity: 0;
  transition: opacity 0.2s ease;
  pointer-events: none;
}
.card:hover::after {
  opacity: 1;
}

box-shadow, drop-shadow() and inset

TechniqueFollowsUse it for
box-shadowThe element’s box and border-radiusCards, buttons, panels
filter: drop-shadow()The visible shape, including transparencyTransparent PNGs, SVG icons, irregular shapes
box-shadow: insetThe inside of the boxPressed states, input fields, soft UI

Inset and outer shadows combined are the basis of soft UI styles. The neumorphism generator pairs light and dark shadows for an extruded look, and the claymorphism generator combines inner and outer shadows for puffy 3D cards.

Build your shadows visually

Shadows are easier to judge than to type. In the layered box shadow generator you choose how many layers you want and set the offset, blur and spread of the outermost one; the smaller layers are calculated for you on an eased curve, and a single strength value keeps the overall darkness consistent as you add layers. Preview it live, then copy the CSS. Four to six layers is a good place to start.