CSS box-shadow: every value explained

How the box-shadow property works: offsets, blur, spread, color and inset, plus layered shadow examples to copy.

Updated

The box-shadow property draws one or more shadows around an element's box. A shadow is written as two to four lengths and a color: horizontal offset, vertical offset, blur radius and spread radius. Add the keyword inset to draw the shadow inside the box.

.card {
  box-shadow: 0 4px 12px rgb(0 0 0 / 0.15);
}

The values in order

Value Required What it does
Offset x Yes Moves the shadow right. Negative values move it left.
Offset y Yes Moves the shadow down. Negative values move it up.
Blur radius No, defaults to 0 Higher values give a softer, larger edge. Cannot be negative.
Spread radius No, defaults to 0 Positive values grow the shadow, negative values shrink it.
Color No, defaults to the element's text color Usually a semi-transparent black or a dark tint of the background
inset No Draws the shadow inside the border

Common recipes

A soft raised card:

box-shadow: 0 1px 2px rgb(0 0 0 / 0.08), 0 8px 24px rgb(0 0 0 / 0.12);

A crisp outline that does not change layout:

box-shadow: 0 0 0 2px #6d28d9;

A pressed or sunken surface:

box-shadow: inset 0 2px 4px rgb(0 0 0 / 0.2);

A shadow below the element only, using negative spread to pull in the sides:

box-shadow: 0 12px 10px -8px rgb(0 0 0 / 0.3);

Layering shadows

Separate several shadows with commas. The first in the list is drawn on top.

Real shadows have a tight, darker part close to the object and a wide, faint part further out. Two or three layers with growing offset and blur look more natural than one heavy shadow:

.elevated {
  box-shadow:
    0 1px 1px rgb(0 0 0 / 0.08),
    0 4px 8px rgb(0 0 0 / 0.08),
    0 16px 32px rgb(0 0 0 / 0.08);
}

How it behaves

  • It takes up no space. A shadow does not affect layout, and it can overlap neighboring elements.
  • It follows border-radius. Rounded boxes cast rounded shadows.
  • It is clipped by an ancestor with overflow: hidden. If a shadow is cut off, look for that on a parent.
  • It follows the box, not the content. On a transparent PNG or a shape made with clip-path, you still get a rectangle's shadow, or none at all. Use filter: drop-shadow() for shadows that follow the visible shape.

box-shadow or drop-shadow?

box-shadow filter: drop-shadow()
Follows The element's box The visible pixels, including transparency
Spread value Yes No
inset Yes No
Several shadows Comma-separated Chain several drop-shadow() functions

Design tips

  • Keep the light coming from one direction across the whole page. A positive y offset and little or no x offset reads as light from above.
  • Use low opacity. A shadow that is noticed as a shadow is usually too dark.
  • On a colored background, tint the shadow with a darker shade of that background in place of pure black.
  • In dark themes shadows barely show. Use a lighter surface color or a thin border to suggest elevation.

Related: gradient syntax and clip-path shapes.