All articles

A practical guide to CSS box-shadow

· 2 min read

box-shadow draws one or more shadows around an element's border box. It doesn't affect layout, so adding a shadow never moves other elements. Realistic depth usually comes from combining several soft shadows rather than one heavy one.

The syntax

box-shadow: [inset] <offset-x> <offset-y> [blur] [spread] <color>;
box-shadow: 0 4px 12px 0 rgb(0 0 0 / 0.15);
Part Meaning
offset-x Positive moves right, negative left
offset-y Positive moves down, negative up
blur Softness; 0 is a sharp edge. Can't be negative
spread Grows (positive) or shrinks (negative) the shadow before blurring
color Usually black or a dark tint with transparency
inset Draws the shadow inside the box

Shadows follow border-radius automatically.

Use transparent colors

Solid gray shadows look flat and dirty on colored backgrounds. Use black, or a dark version of the background's hue, with low alpha:

box-shadow: 0 4px 12px rgb(0 0 0 / 0.12);
box-shadow: 0 4px 12px hsl(220 60% 20% / 0.15);   /* tinted to match a blue UI */

Layered shadows

Real shadows have a sharp, dark part close to the object and a soft, faint part further away. Stacking two or three shadows reproduces that:

.card {
  box-shadow:
    0 1px 2px rgb(0 0 0 / 0.06),
    0 4px 8px rgb(0 0 0 / 0.06),
    0 12px 24px rgb(0 0 0 / 0.08);
}

The first shadow in the list is drawn on top.

An elevation scale

Define a few levels as custom properties and reuse them, rather than inventing a new shadow for every component:

:root {
  --shadow-1: 0 1px 2px rgb(0 0 0 / 0.08);
  --shadow-2: 0 1px 2px rgb(0 0 0 / 0.06), 0 2px 6px rgb(0 0 0 / 0.08);
  --shadow-3: 0 2px 4px rgb(0 0 0 / 0.06), 0 8px 16px rgb(0 0 0 / 0.1);
  --shadow-4: 0 4px 8px rgb(0 0 0 / 0.08), 0 16px 32px rgb(0 0 0 / 0.12);
}

.button { box-shadow: var(--shadow-1); }
.card { box-shadow: var(--shadow-2); }
.dropdown { box-shadow: var(--shadow-3); }
.modal { box-shadow: var(--shadow-4); }

Larger offsets and blur suggest an element is higher above the page.

Recipes

/* Bottom-only shadow: negative spread hides the sides */
box-shadow: 0 8px 6px -6px rgb(0 0 0 / 0.4);

/* Inner shadow for an inset input */
box-shadow: inset 0 1px 2px rgb(0 0 0 / 0.12);

/* Ring without changing layout (focus or selection) */
box-shadow: 0 0 0 3px rgb(59 130 246 / 0.5);

/* Glow */
box-shadow: 0 0 20px rgb(59 130 246 / 0.6);

/* Multiple outlines */
box-shadow: 0 0 0 2px #fff, 0 0 0 4px #3b82f6;

box-shadow vs filter: drop-shadow()

box-shadow follows the element's rectangle (with its rounded corners). filter: drop-shadow() follows the actual visible pixels, including transparent PNG and SVG shapes:

.icon { filter: drop-shadow(0 2px 4px rgb(0 0 0 / 0.3)); }

Differences:

  • drop-shadow has no spread or inset, and takes one shadow per function (you can chain several).
  • drop-shadow applies to the whole rendered element, including its children and text.
  • box-shadow is the better fit for cards, buttons and panels; drop-shadow for icons, cut-out images and irregular shapes.

Animating shadows efficiently

Animating box-shadow directly triggers a repaint on every frame, which can stutter on large elements or slower devices. A common technique is to put the larger shadow on a pseudo-element and animate its opacity, which browsers can composite cheaply:

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

The keyframe animations guide explains which properties animate cheaply.

Common mistakes

  • Very dark, very sharp shadows, like 5px 5px 0 #000, unless that's the deliberate style.
  • Gray instead of transparent colors, which look muddy on anything but white.
  • Shadows clipped by overflow: hidden on a parent.
  • Relying on shadows alone to separate elements in dark mode or for low-vision users; add a border or a change of surface color.

Shadows and gradients work well together: a subtle gradient on the surface and a layered shadow underneath give depth without heavy effects. The color formats guide explains the rgb(0 0 0 / 0.1) syntax used here.

Frequently asked questions

Why is my shadow cut off?

A parent with overflow: hidden (or auto) clips shadows that extend outside it. Add padding to the parent or move the shadow to an element that isn't clipped.

How do I put a shadow on only one side?

Use an offset toward that side and a negative spread equal to the blur, e.g. 0 8px 6px -6px rgb(0 0 0 / 0.4) for a bottom-only shadow.

Can box-shadow replace borders?

Yes: box-shadow: 0 0 0 1px #d1d5db draws a 1px ring that doesn't affect layout. It's handy for focus rings, but it won't show in Windows high contrast mode, so keep an outline for focus.

Do shadows work in dark mode?

Dark shadows are barely visible on dark backgrounds. Use lighter surfaces for elevated elements, a subtle border, or a stronger shadow in dark themes.

Further reading