CSS Box Shadow Guide: Design Beautiful Depth Effects
Master CSS box-shadow with this complete guide. Syntax, examples, and visual generator.
# CSS Box Shadow Guide: Design Beautiful Depth Effects
Shadows are one of the most effective tools for creating visual depth and hierarchy in web design. A well-placed box-shadow can elevate a flat interface into a polished, professional-looking layout. Whether you're building a card component, a floating navigation bar, or a subtle UI accent, understanding CSS box-shadow is essential for any modern web designer.
In this comprehensive guide, we'll cover the full syntax of box-shadow, walk through real-world examples, explore advanced techniques like multiple shadows and inset effects, discuss performance considerations, and introduce a visual generator that makes crafting shadows effortless.
What Is CSS Box-Shadow?
The box-shadow CSS property adds shadow effects around an element's frame. You can control the shadow's color, size, blur, spread, and position with a single line of CSS. Unlike image-based shadows, box-shadow is resolution-independent, requires no extra HTTP requests, and integrates seamlessly with responsive layouts.
A basic box-shadow declaration looks like this:
box-shadow: h-offset v-offset blur spread color;Each value plays a specific role:
Simple Shadow Example
.card {
box-shadow: 2px 4px 12px rgba(0, 0, 0, 0.15);
}This creates a subtle shadow that is offset 2 pixels to the right and 4 pixels downward, with a 12-pixel blur and 15% opacity black color. It's a classic card shadow used across countless websites.
No Shadow vs. Shadow Comparison
Without a shadow, elements can appear flat and blend into the background. Adding a box-shadow creates an immediate sense of elevation. The shadow implies that the element is "floating" above the surface, which is a core principle of Material Design and modern UI systems.
Box-Shadow Syntax Breakdown
Let's examine each parameter in greater depth.
Horizontal and Vertical Offset
The offsets determine where the shadow falls relative to the element. These are the only two required values.
/* Shadow to the bottom-right */
box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.2);
/* Shadow directly below (no horizontal offset) */
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
/* Shadow to the top-left */
box-shadow: -4px -4px 10px rgba(0, 0, 0, 0.2);Setting both offsets to zero places the shadow directly behind the element, which is useful for creating a glowing effect when combined with a larger blur radius.
Blur Radius
The blur radius controls how soft the shadow edges appear. A value of 0 produces a hard, crisp shadow. As the value increases, the shadow becomes increasingly diffused.
/* Sharp shadow (no blur) */
box-shadow: 4px 4px 0px rgba(0, 0, 0, 0.3);
/* Soft shadow (moderate blur) */
box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.3);
/* Very soft shadow (high blur) */
box-shadow: 4px 4px 30px rgba(0, 0, 0, 0.3);The blur radius cannot be negative. For most UI applications, a blur between 8 and 20 pixels produces natural-looking shadows.
Spread Radius
The spread radius expands or contracts the shadow's footprint. Unlike blur, spread affects the overall size of the shadow uniformly.
/* Normal shadow */
box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.2);
/* Expanded shadow (spread of 5px) */
box-shadow: 4px 4px 10px 5px rgba(0, 0, 0, 0.2);
/* Shrunken shadow (spread of -3px) */
box-shadow: 4px 4px 10px -3px rgba(0, 0, 0, 0.2);A positive spread makes the shadow appear larger than the element, while a negative spread makes it smaller. This is particularly useful for creating subtle outlines or tightly bound shadows.
Color and Transparency
The shadow color is critical for realism. Most real-world shadows are not pure black — they have some transparency and sometimes a hint of color.
/* Using rgba for transparency */
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
/* Using a colored shadow for a glow effect */
box-shadow: 0 0 20px rgba(59, 130, 246, 0.5);
/* Using the currentColor keyword */
box-shadow: 0 4px 12px currentColor;For light-themed designs, shadows with 10–20% opacity work well. For dark themes, you may need slightly higher opacity (20–40%) or use lighter shadow colors to ensure visibility.
Inset Shadows
By adding the inset keyword, the shadow appears inside the element's border rather than outside. Inset shadows are perfect for creating pressed buttons, input fields, and recessed containers.
/* Inner shadow for a pressed button effect */
.button:active {
box-shadow: inset 2px 2px 6px rgba(0, 0, 0, 0.3);
}
/* Inset shadow for an input field */
.input {
box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.1);
}Combining Inset and Outer Shadows
You can combine both inset and outer shadows on the same element by separating them with commas:
.card {
box-shadow:
0 4px 12px rgba(0, 0, 0, 0.15),
inset 0 1px 2px rgba(255, 255, 255, 0.3);
}This creates a shadow below the card (outer) and a subtle highlight at the top (inset), which mimics how light naturally falls on a raised surface.
Multiple Shadows
One of the most powerful features of box-shadow is the ability to layer multiple shadows on a single element. Each shadow is separated by a comma, and they are painted in order — the first shadow on top.
.floating-card {
box-shadow:
0 1px 3px rgba(0, 0, 0, 0.12),
0 4px 8px rgba(0, 0, 0, 0.08),
0 12px 24px rgba(0, 0, 0, 0.06);
}This layered approach creates a more realistic and nuanced shadow than a single declaration. The closest shadow is sharper and darker, while the outermost shadow is softer and lighter — just like in the real world.
Elevation System Using Multiple Shadows
Google's Material Design defines an elevation system where elements at different "heights" have different shadow characteristics:
/* Elevation 1 — subtle shadow */
.elevation-1 {
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.08);
}
/* Elevation 2 — medium shadow */
.elevation-2 {
box-shadow: 0 3px 6px rgba(0, 0, 0, 0.16), 0 3px 6px rgba(0, 0, 0, 0.12);
}
/* Elevation 4 — prominent shadow */
.elevation-4 {
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.19), 0 6px 6px rgba(0, 0, 0, 0.10);
}
/* Elevation 8 — high shadow */
.elevation-8 {
box-shadow: 0 14px 28px rgba(0, 0, 0, 0.22), 0 10px 10px rgba(0, 0, 0, 0.14);
}This system provides a consistent visual language for indicating hierarchy and interactivity across your interface.
Advanced Techniques
Glow Effects
By using a colored shadow with high blur and no offset, you can create a glow effect:
.neon-button {
box-shadow: 0 0 15px rgba(59, 130, 246, 0.6),
0 0 45px rgba(59, 130, 246, 0.3);
}Hard Shadows (No Blur)
Flat design often uses hard shadows with zero blur for a bold, graphic look:
.retro-button {
background: #3b82f6;
border: none;
box-shadow: 4px 4px 0px #1e40af;
transition: all 0.1s ease;
}
.retro-button:active {
box-shadow: 2px 2px 0px #1e40af;
transform: translate(2px, 2px);
}Shadow-Only Elements
You can create a "shadow" without the visible element by setting both the background and border to transparent:
.shadow-only {
background: transparent;
border: none;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
}Using `filter: drop-shadow()`
While not the same as box-shadow, CSS drop-shadow is useful for elements with non-rectangular shapes (like PNGs with transparency):
.icon {
filter: drop-shadow(0 4px 6px rgba(0, 0, 0, 0.25));
}Dark Mode Considerations
Shadows behave differently in dark environments. On dark backgrounds, traditional dark shadows are nearly invisible. Here are some strategies:
@media (prefers-color-scheme: dark) {
.card {
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.5),
0 0 0 1px rgba(255, 255, 255, 0.05);
}
}Performance Tips
Accessibility Considerations
Shadows are purely decorative and do not convey essential information, which is generally good for accessibility. However, keep these points in mind:
Creating Shadows Visually
Writing shadow code by hand can be tedious, especially when you're experimenting with multiple layers. A visual shadow generator lets you adjust offset, blur, spread, and color in real-time, seeing the results instantly on a preview element.
Try our CSS Shadow Tool to design and customize box-shadows interactively. Adjust every parameter with sliders, preview the effect on different shapes and colors, and copy the generated CSS code directly into your stylesheet.
Common Box-Shadow Patterns
Here are some tried-and-true shadow patterns you can use right away:
/* Subtle card shadow */
.subtle-card {
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}
/* Elevated card */
.elevated-card {
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
}
/* Button hover effect */
.button:hover {
box-shadow: 0 4px 16px rgba(59, 130, 246, 0.35);
}
/* Modal overlay shadow */
.modal {
box-shadow: 0 24px 48px rgba(0, 0, 0, 0.2);
}
/* Text shadow for headings */
h1 {
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
}Browser Support
box-shadow is supported in all modern browsers, including Chrome, Firefox, Safari, and Edge. The property has been stable since around 2010, and there are no significant cross-browser inconsistencies for standard usage. Vendor prefixes (-webkit-, -moz-) were necessary in the past but are no longer required for current browser versions.
Conclusion
CSS box-shadow is a versatile and performant tool for adding depth, dimension, and visual polish to your web designs. From simple card shadows to complex multi-layered elevation systems, mastering this property gives you precise control over the visual hierarchy of your interfaces. Combine it with inset shadows, glow effects, and dark mode adjustments to create designs that feel both modern and natural.
Start experimenting with shadows today using our CSS Shadow Tool — adjust parameters in real-time and generate production-ready CSS code in seconds.
---
Related Tools
Try it yourself with our free online tool:
Try CSS Box Shadow Guide: Design Beautiful Depth Effects →