CSS Box Shadow Generator: Designing Shadows That Look Real
A CSS box shadow generator saves you from guessing offsets and blur values. The tool above outputs a working box-shadow line, but good shadows depend on choices the generator can't make for you: light direction, layering and restraint. This guide covers the design rules that make shadows look physical, plus performance and accessibility details most tutorials skip.
A Fast Workflow for Natural-Looking Shadows
- Decide where the light comes from. Most interfaces assume it comes from above, so shadows fall downward with a small or zero horizontal offset.
- Choose elevation: a low card (1-2px offset), a raised card (8px) or a modal (24px or more).
- Make blur about two to three times the vertical offset.
- Use a low-opacity, slightly tinted colour instead of pure black.
- Add a second or third layer for realism (see the next section), then copy the CSS.
Why Layered Shadows Look Better
Real objects cast a tight dark shadow close to the surface and a wider, softer one farther away. A single shadow can only fake one of those. Stacking layers lets you do both:
.card {
box-shadow:
0 1px 1px rgba(16, 24, 40, 0.08),
0 2px 4px rgba(16, 24, 40, 0.08),
0 8px 16px rgba(16, 24, 40, 0.08),
0 16px 32px rgba(16, 24, 40, 0.08);
}
Each layer doubles the offset and blur while keeping low opacity. The result is a smooth ramp without the heavy edge of a single large shadow. Because layers add up, the total opacity can get dark quickly, so keep each one under 0.12.
Elevation Scale Table
| Level | Use | Example value |
|---|
| 0 | Flat surfaces | none |
| 1 | Inputs, list rows | 0 1px 2px rgba(0,0,0,.12) |
| 2 | Cards | 0 4px 8px rgba(0,0,0,.10) |
| 3 | Dropdowns, popovers | 0 8px 24px rgba(0,0,0,.14) |
| 4 | Dialogs | 0 20px 48px rgba(0,0,0,.20) |
Define these as CSS custom properties (--shadow-1 through --shadow-4) so the whole interface shares one consistent depth system. Material Design's elevation guidance is a good reference for how many levels you really need.
Recipes for Specific Effects
Bottom-only shadow
.bottom-only { box-shadow: 0 10px 10px -10px rgba(0,0,0,.4); }
A negative spread pulls the shadow in from the sides so it only shows below the element. This is a spread trick that many people don't know.
Hard offset "brutalist" shadow
.brutal { border: 2px solid #111; box-shadow: 6px 6px 0 #111; }
Focus ring that isn't an outline
button:focus-visible { box-shadow: 0 0 0 3px #fff, 0 0 0 6px #2563eb; }
Two zero-blur layers draw a white gap and then a blue ring. Pair it with outline: 2px solid transparent so forced-colors users still see something.
Inner glow
.glow { box-shadow: inset 0 0 20px rgba(99,102,241,.5); }
Page-curl "paper lift"
.paper { box-shadow: 0 0 0 1px rgba(0,0,0,.05), 0 12px 24px -8px rgba(0,0,0,.25); }
How Shadows Interact With Other Properties
- Border radius: The shadow follows the rounded outline. Adjust the corners in the border radius generator first, then tweak the shadow.
- Layout: Shadows never affect layout, so they can overlap neighbours and get covered by later siblings. Use
position: relative; z-index to bring a card forward. - Overflow: A parent with
overflow: hidden clips children's shadows. Add padding to the parent or remove the clip. - Clip-path: Clipping removes the shadow. Use
filter: drop-shadow() on a wrapper, as explained in the clip-path guide. - Transparent elements: The shadow is drawn only outside the border box, so it does not show through a see-through background. For transparent shapes or PNGs, use
drop-shadow instead.
The specification for rendering is in CSS Backgrounds and Borders Level 3.
Performance
Static shadows are inexpensive. Animation is the issue: changing box-shadow forces a repaint every frame, which is noticeable on lists with many cards. The standard fix is to place the larger shadow on a ::after pseudo-element, set its opacity to 0, and transition only the opacity on hover.
.card { position: relative; box-shadow: 0 2px 6px rgba(0,0,0,.1); }
.card::after {
content: "";
position: absolute; inset: 0;
border-radius: inherit;
box-shadow: 0 16px 32px rgba(0,0,0,.2);
opacity: 0;
transition: opacity .25s;
pointer-events: none;
}
.card:hover::after { opacity: 1; }
This approach is recommended in web.dev's animation guide because opacity can be handled by the compositor.
Accessibility
- Don't use shadow as the only way to show a boundary. WCAG 1.4.11 asks for 3:1 contrast on UI boundaries needed to identify a control.
- Box-shadow is removed in forced-colors mode. Keep a transparent border or outline so the control stays visible.
- Dark mode needs different shadows. Black shadows vanish on dark surfaces; express elevation with lighter surface colours instead, and keep shadows subtle.
Common Mistakes
- One huge, dark shadow (for example
0 0 50px #000) that makes the card look dirty. - Offsetting shadows horizontally on every card, which implies a different light source than the rest of the page.
- Using pure black. Tint toward the background hue for a more natural look.
- Comparing shadows on a white preview but shipping on a grey page. Test on the final colour.
- Placing
inset after other values. The keyword can sit at the start or end, but the lengths must stay together.
Colour-Matched Shadows and Dark Mode
A grey shadow under a coloured card looks pasted on. Real shadows pick up the colour of the surface. A blue button should cast a blue-tinted shadow:
.btn-blue {
background: #2563eb;
box-shadow: 0 6px 16px -4px rgba(37, 99, 235, 0.55);
}
The negative spread keeps the glow tight, so it hugs the button instead of spreading wide. The same idea works with cards on tinted pages: use a very dark version of the page hue, such as rgba(30, 27, 75, .15) on a purple page, rather than black.
In dark mode, a black shadow on a near-black surface is nearly invisible. Designers usually raise the surface lightness with elevation (a modal is slightly lighter than the page) and keep a faint shadow for separation:
@media (prefers-color-scheme: dark) {
.card {
background: #1e2330;
box-shadow: 0 0 0 1px rgba(255,255,255,.06), 0 8px 24px rgba(0,0,0,.5);
}
}
The one-pixel white ring replaces the border you'd otherwise lose. If you want a soft inset look rather than elevation, the neumorphism generator shows the paired light and dark approach.
More Box Shadow Questions
What does the spread value do and when should I use it?
Spread grows or shrinks the shadow's size before blur is applied. Positive values make it larger than the element; negative values shrink it, which is how one-sided shadows are made. Zero is fine for most cases.
Why is my shadow cut off on one side?
A parent with overflow: hidden or overflow: auto is clipping it. Add padding to the parent equal to the blur plus offset.
Can I put a shadow on only one side?
Yes. Use an offset in one direction and a negative spread equal to the blur, like 0 8px 8px -8px for a bottom-only effect.
How can I shadow a transparent PNG or SVG?
Use filter: drop-shadow(0 4px 8px rgba(0,0,0,.3)). It follows the image alpha instead of the rectangular box.
How many shadow layers are too many?
Up to four layers is typical for premium-looking cards. Beyond that, the visual gain is small. Reserve the stacked version for key elements and use single shadows elsewhere.