The box-shadow property is one of the most versatile CSS properties for creating depth, elevation, and visual effects. This comprehensive guide will teach you everything you need to know about box-shadow, from basic syntax to advanced techniques.
The complete syntax for box-shadow is:
box-shadow: [inset] offset-x offset-y blur-radius spread-radius color;
All values except the color are optional. Let's break down each component:
The inset keyword creates an inner shadow instead of an outer shadow. When used, the shadow appears inside the element's border.
Outer Shadow (default):
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
Inner Shadow:
box-shadow: inset 0 4px 6px rgba(0, 0, 0, 0.1);
These values determine the horizontal and vertical position of the shadow relative to the element.
Shadow to the right and down:
box-shadow: 10px 10px rgba(0, 0, 0, 0.3);
Shadow to the left and up:
box-shadow: -10px -10px rgba(0, 0, 0, 0.3);
The blur radius controls how blurry the shadow is. A value of 0 creates a sharp shadow, while larger values create softer, more diffused shadows.
Sharp shadow (no blur):
box-shadow: 0 4px 0 rgba(0, 0, 0, 0.3);
Soft shadow (high blur):
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
The spread radius makes the shadow larger (positive values) or smaller (negative values) than the element itself.
Larger shadow (positive spread):
box-shadow: 0 4px 6px 10px rgba(0, 0, 0, 0.3);
Smaller shadow (negative spread):
box-shadow: 0 4px 6px -2px rgba(0, 0, 0, 0.3);
The color can be specified in any CSS color format. Using RGBA or HSLA allows you to control opacity, which is essential for creating natural-looking shadows.
Solid color:
box-shadow: 0 4px 6px black;
Color with opacity (recommended):
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.3);
Perfect for cards and buttons that need a gentle elevation:
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
Creates a floating effect similar to Material Design:
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1),
0 2px 4px rgba(0, 0, 0, 0.06);
For elements that need to stand out significantly:
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
Creates a glowing effect around the element:
box-shadow: 0 0 20px rgba(0, 255, 255, 0.6);
Multiple colored shadows for a neon effect:
box-shadow:
0 0 5px rgba(0, 255, 255, 0.8),
0 0 10px rgba(0, 255, 255, 0.6),
0 0 20px rgba(0, 255, 255, 0.4);
Using inset shadow to create a pressed appearance:
box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.2);
You can apply multiple shadows to a single element by separating them with commas. This is incredibly powerful for creating complex effects:
Layered depth effect:
box-shadow:
0 1px 3px rgba(0, 0, 0, 0.12),
0 1px 2px rgba(0, 0, 0, 0.24),
0 3px 6px rgba(0, 0, 0, 0.16);
Combined outer and inner shadow:
box-shadow:
0 4px 6px rgba(0, 0, 0, 0.1),
inset 0 1px 0 rgba(255, 255, 255, 0.1);
Unlike borders, shadows don't affect the element's dimensions or layout. They're purely visual effects that don't take up space in the document flow.
Outlines are always rectangular and can't be blurred. Box shadows offer much more flexibility for creating custom shapes and effects.
Layer multiple shadows with increasing blur and offset to create realistic depth:
box-shadow:
0 1px 3px rgba(0, 0, 0, 0.12),
0 1px 2px rgba(0, 0, 0, 0.24),
0 3px 6px rgba(0, 0, 0, 0.16),
0 10px 20px rgba(0, 0, 0, 0.1);
Use your brand colors in shadows for cohesive design:
box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
Adjust shadow intensity based on screen size using media queries:
/* Mobile */
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
/* Desktop */
@media (min-width: 768px) {
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
}
The box-shadow property has excellent browser support:
Mastering the box-shadow property opens up endless possibilities for creating depth, hierarchy, and visual interest in your designs. Start with simple shadows and gradually experiment with multiple shadows, different colors, and various blur values to find what works best for your project.
Remember: the best shadows are often the ones you barely notice. They should enhance your design without drawing attention to themselves. And when you need to experiment, our Box Shadow Generator makes it easy to visualize and perfect your shadow effects!
Don't want to write CSS manually? Use our free Box Shadow Generator to create beautiful shadows with a visual interface!
Try Box Shadow Generator Now