CSS Box-Shadow: From Flat to Layered in Minutes
Good shadows make UI feel real. Here's how to layer them properly — and a free visual generator that writes the CSS.
# CSS Box Shadow: From Flat to Layered in Minutes
Drop shadows are one of the most effective ways to add depth to web designs. Yet the CSS box-shadow syntax trips up a lot of people because it looks like a random string of numbers.
The Box-Shadow Syntax
box-shadow: [inset] offsetX offsetY blurRadius spreadRadius color;
- offsetX / offsetY — How far the shadow shifts from the element. Positive X moves it right, positive Y moves it down.
- blurRadius — Higher = softer shadow. 0 = hard edge.
- spreadRadius — Positive makes the shadow bigger, negative makes it smaller. Usually 0.
- inset — Puts the shadow inside the element instead of outside.
What Makes a Shadow Look Natural
Bad shadows look like a kid's PowerPoint presentation — hard black edges, 0 blur, way too dark. Good shadows are subtle:
- Low opacity — Use
rgba(0,0,0,0.1)torgba(0,0,0,0.25), not solid black. - Moderate blur — 8px to 24px for most UI elements.
- Small offset — 2px to 8px vertical. Horizontal usually 0 or very small.
- Layer multiple shadows — One tight shadow for definition, one soft shadow for depth:
box-shadow:
0 2px 4px rgba(0,0,0,0.1),
0 8px 16px rgba(0,0,0,0.1);
Common Patterns
- Card hover:
box-shadow: 0 4px 12px rgba(0,0,0,0.15) - Sticky header:
box-shadow: 0 2px 8px rgba(0,0,0,0.1) - Floating button:
box-shadow: 0 8px 24px rgba(0,0,0,0.2) - Inset (pressed):
box-shadow: inset 0 2px 4px rgba(0,0,0,0.1)
Generating Shadows Visually
Memorizing the syntax isn't necessary. Our CSS Box Shadow Generator lets you drag sliders for offset, blur, spread, and opacity, then copy the CSS. Free, no signup.
## Box Shadow Syntax Explained
The CSS box-shadow property accepts multiple values:
box-shadow: <offset-x> <offset-y> <blur-radius> <spread-radius> <color> <inset>;
- offset-x / offset-y: Horizontal and vertical offset. Positive values move the shadow right and down; negative values move it left and up.
- blur-radius: How blurry the shadow is. 0 = sharp edge. Higher values = softer, more diffused shadow.
- spread-radius: How much the shadow grows or shrinks. Positive values make it larger, negative values make it smaller.
- color: Any CSS color value (hex, rgb, hsl, named colors).
- inset: Optional keyword. Makes the shadow appear inside the element instead of outside.
Common Shadow Patterns
- Subtle depth:
box-shadow: 0 1px 3px rgba(0,0,0,0.12)— Material Design elevation - Floating card:
box-shadow: 0 4px 6px rgba(0,0,0,0.1)— Hover state for cards - Sharp drop shadow:
box-shadow: 4px 4px 0 #000— Retro, brutalist design - Inner glow:
box-shadow: inset 0 0 10px rgba(0,0,0,0.3)— Inset effect - Layered shadows:
box-shadow: 0 1px 2px rgba(0,0,0,0.1), 0 2px 4px rgba(0,0,0,0.1)— More realistic depth
Performance Considerations
Box shadows can be expensive to render, especially with large blur radii. Tips for performance:
- Avoid animating shadows: Animating blur-radius causes repaints on every frame. Animate opacity or transform instead.
- Use will-change sparingly:
will-change: box-shadowcan help for animated shadows but consumes memory. - Prefer transforms for hover effects: Instead of changing shadow on hover, layer two elements with different shadows and toggle opacity.
- Limit shadow count: Each shadow adds rendering cost. Keep to 1-2 shadows per element.