Educational guide to box-shadow
From box to shadow
Read a shadow’s values, combine several layers, and decide whether the effect should follow the box or an image’s silhouette.
Mental model
box-shadow paints one or more shadows around or inside a box. Each shadow needs two offsets. Blur, spread, color, and the inset keyword are optional.
- X and Y offsets
The first two lengths move the shadow along the physical axes. Positive values move it right and down. Negative values move it left and up.
- Blur
The third length softens the edge. Zero gives a sharp edge. Negative values are invalid.
- Spread
The fourth length grows the shaded area if positive and shrinks it if negative. Write the blur value, even when zero, before specifying spread.
Outer and inner geometry
An outer shadow starts from the border box and follows its rounded corners. With inset, the padding box is the reference and the shadow is painted inside that inner edge.
The same lengths, outside and inside
The shadow leaves the box dimensions and spacing intact. To make room for it, use gap, padding, or margin where appropriate. An ancestor that clips overflow can hide part of the outer shadow.
Layers and painting order
Separate shadows with commas in one declaration. Among outer shadows, the first is in front, with the rest layered behind it. Inner shadows are drawn above the background and outer shadows beneath it.
Two shadows in reverse order
Blue first
Amber first
To give a card depth, try a close shadow with a little blur as the first layer and a broader, softer shadow as the second. Adjust both on the actual surface.
Box versus silhouette
Use box-shadow to follow the geometry of a box. Use filter: drop-shadow() to cast the silhouette of the painted pixels, such as an image with transparency.
Two triangles on transparent backgrounds
box-shadow
drop-shadow()
The third length in drop-shadow() is the standard deviation of the Gaussian blur. The function accepts up to three lengths. For an inner shadow or spread control, use box-shadow.
Visible focus with or without a shadow
Use the shadow for depth and keep the browser’s focus indicator. When customizing focus, apply a contrasting outline with :focus-visible.
A disclosure with a focus outline
View details
The native disclosure keeps its outline while the summary has focus.
Common pitfalls
The second declaration replaces the first
- Expected result
- Both shadows appear together.
- Cause
- Two box-shadow declarations on the same box compete in the cascade.
- Solution
- Combine the layers with commas and put the frontmost layer first.
The shadow overlaps a neighboring box
- Expected result
- There is enough room to see the entire shadow.
- Cause
- The shadow adds paint while the layout keeps the same space between boxes.
- Solution
- Increase the layout spacing or reduce the extent of the shadow.
Support and compatibility
box-shadow and the drop-shadow() filter function are widely available. Their appearance depends on the geometry and what is painted behind them.
In forced colors, the browser removes box-shadow. When a control’s boundary or state matters, also use a border or outline that stays visible.