How to Build an Elevation & Shadow System in CSS: 5-Level Light Source Scale
To build a realistic elevation system in CSS, simulate a single overhead light source using two stacked shadows per level: a tight, sharp "key shadow" that creates immediate contact grounding, and a diffuse, soft "ambient shadow" that conveys height. Scale both shadows proportionally across 5 standardized elevation tokens (--elevation-1 for cards, up to --elevation-5 for modals and floating toasts) while matching corresponding z-index layers.
Step 1: Physics of Natural Shadows: Key Light vs Ambient Occlusion
Real-world objects are illuminated by direct key lighting (e.g. overhead ceiling lights or the sun) and indirect ambient bounce light. A single flat box-shadow looks amateurish. Stacking two comma-separated shadow layers creates photorealistic three-dimensional depth.
:root {
/* Level 1: Resting Card Elevation */
--elevation-1:
0 1px 2px rgba(0, 0, 0, 0.08), /* Sharp Key Contact Shadow */
0 4px 12px rgba(0, 0, 0, 0.12); /* Soft Ambient Bounce Shadow */
/* Level 3: Dropdown / Hover Elevation */
--elevation-3:
0 4px 8px rgba(0, 0, 0, 0.12),
0 16px 32px -4px rgba(0, 0, 0, 0.2);
/* Level 5: Floating Modal / Dialog */
--elevation-5:
0 8px 16px rgba(0, 0, 0, 0.15),
0 32px 64px -8px rgba(0, 0, 0, 0.35);
}
Step 2: Color-Tinted Shadows for Brand Harmony
Pure black shadows on colored backgrounds look dingy and synthetic. By tinting shadow colors with your primary brand hue or surface background tone, shadows feel luminous and integrated.
:root {
--shadow-color: 243 45% 12%; /* Deep Indigo HSL */
--elevation-brand-hover:
0 4px 10px hsla(var(--shadow-color), 0.18),
0 20px 36px -6px hsla(var(--shadow-color), 0.35);
}
Step 3: Synchronizing Z-Index Stacking with Elevation Scales
Visual elevation must correspond directly to stacking order. If a modal has Level 5 shadow depth, its z-index must be systematically higher than Level 1 cards to prevent stacking context bugs.
:root {
/* Z-Index Design Tokens */
--z-base: 0;
--z-card: 10;
--z-sticky: 100;
--z-dropdown: 1000;
--z-modal: 2000;
--z-toast: 3000;
}
Step 4: Smooth 60 FPS Elevation Transitions on Hover
Transitioning box-shadow directly can trigger GPU paint overhead. For buttery smooth hover physics, transition transform: translateY() alongside the shadow using an authentic spring easing curve.
.interactive-card {
box-shadow: var(--elevation-1);
transform: translateY(0);
transition: transform 0.25s cubic-bezier(0.16, 1, 0.3, 1),
box-shadow 0.25s cubic-bezier(0.16, 1, 0.3, 1);
}
.interactive-card:hover {
transform: translateY(-4px);
box-shadow: var(--elevation-3);
}
Next-Level Addons: Elevating Your Implementation
Take this technique further by combining it with complementary design token studios and animation tools:
A trick I use in almost every project: don't fight specificity battles with !important. Leverage modern CSS cascade layers (@layer) and CSS variables as design tokens—it makes dark/light theme switching and responsive tuning effortless.
Frequently Asked Questions
Practical answers to common implementation questions, browser quirks, and performance tips.
Why should I stack two box-shadow declarations instead of one?
Real-world illumination features direct light and ambient bounce. Two stacked shadows (one tight with low blur for contact definition, and one wide with high blur for ambient diffusion) look significantly more natural than any single shadow.
How do I prevent dark mode box-shadows from disappearing?
In dark mode, black shadows blend invisibly into dark gray or black backgrounds. Add a subtle 1px semi-transparent white border (e.g. border: 1px solid rgba(255, 255, 255, 0.08)) or use slightly tinted blue/purple shadow hues with higher opacity.
What is the performance cost of animating box-shadow on hover?
Animating box-shadow can trigger CPU repainting on every frame. For optimal 60 FPS performance, pair a subtle shadow transition with GPU-accelerated transform: translateY(-4px), or cross-fade between two pseudo-elements with opacity.
How many elevation levels does a standard design system need?
A standard system utilizes 5 levels: Level 0 (flat/inline), Level 1 (resting cards/buttons), Level 2 (hovered items), Level 3 (menus/popovers), Level 4 (navigation bars/drawers), and Level 5 (modals/dialogs).
What is shadow spread and when should it be negative?
The fourth length value in box-shadow is spread. Negative spread (e.g. 0 16px 32px -8px rgba(0,0,0,0.3)) shrinks the shadow footprint, preventing shadows from bleeding out awkwardly from the left and right sides of narrow containers.