CSS box-shadow is one of the most versatile properties in modern web design. A well-placed shadow adds depth, hierarchy, and visual interest to any interface. From subtle elevation cues that guide user attention to dramatic glow effects that create focal points, box-shadow gives designers and developers precise control over how elements relate to their surroundings.
Before CSS3 introduced box-shadow, creating shadows meant using sliced background images, extra markup, or JavaScript workarounds. Today, a single line of CSS produces effects that once required hours of asset preparation. Browser support is universal across modern browsers, and performance is excellent when shadows are used thoughtfully.
If you prefer a visual approach to building shadows, our Box Shadow Generator lets you create and customize shadows in real time without writing CSS by hand. You can experiment with every property covered in this guide and see results instantly.
Understanding the Box-Shadow Syntax
The box-shadow property accepts up to six values per shadow layer:
element {
box-shadow: offset-x offset-y blur-radius spread-radius color inset;
}
Each value plays a specific role in how the shadow renders:
- offset-x — Horizontal offset. Positive values move the shadow right, negative values move it left.
- offset-y — Vertical offset. Positive values move the shadow down, negative values move it up.
- blur-radius — Optional. Controls how soft the shadow edges are. Higher values create more diffusion. Default is 0, which produces a sharp edge.
- spread-radius — Optional. Expands or contracts the shadow size. Positive values enlarge the shadow beyond the element bounds, negative values shrink it. Default is 0.
- color — The shadow color. Accepts any valid CSS color value including hex, rgb, rgba, hsl, and named colors.
- inset — Optional keyword. Changes the shadow from an outer drop shadow to an inner shadow that appears inside the element border.
The most basic box-shadow requires only offset values and a color:
.shadow {
box-shadow: 4px 4px 0px 0px rgba(0, 0, 0, 0.3);
}
This produces a hard shadow shifted 4 pixels right and 4 pixels down. Adding a blur radius softens the edges and produces a much more natural appearance. Most production shadows use at least a small blur value to simulate how real light sources create soft shadows rather than hard ones.
Choose shadow colors that complement your design using our Color Picker. The right color choice makes shadows look natural and enhances your overall design system. Shadows tinted with the background color often look more realistic than purely gray or black shadows.
Creating Realistic Elevation Shadows
Physical objects cast shadows that vary based on light source intensity and distance from the surface. Digital interfaces should mimic this behavior to create convincing depth illusions. The key principles follow real-world physics:
- Nearby objects cast small, sharp shadows with minimal blur and offset
- Distant objects cast larger, softer shadows with more blur and offset
- Light from above means the shadow offset-y is typically positive, placing the shadow below the element
A small button or badge might use a subtle shadow:
.button {
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
A modal dialog, dropdown menu, or tooltip that needs to appear significantly elevated above the page:
.modal {
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.15);
}
A floating action button or pinned element that should stand out from surrounding content:
.fab {
box-shadow: 0 6px 16px rgba(0, 0, 0, 0.2);
}
These natural-looking shadows communicate interactivity and hierarchy without distracting from the content. The key is to use just enough shadow to suggest depth while keeping the interface clean. Our Border Radius Generator pairs perfectly with box-shadow to create polished, modern cards and buttons with soft, rounded corners that catch light naturally.
Inner Shadows with Inset
The inset keyword flips the shadow direction to render inside the element, creating a recessed or engraved appearance:
.inset-shadow {
box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.1);
}
Inset shadows are excellent for a variety of practical design patterns:
- Input fields that need to appear sunken into the page surface
- Pressed button states that simulate physical depression when clicked
- Card containers that need subtle inner depth around their edges
- Image frames with inner borders or vignette effects
Combining inset shadows with regular outer shadows creates layered depth that feels sophisticated:
.card {
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1), inset 0 1px 0 rgba(255, 255, 255, 0.1);
}
This two-layer technique produces a card that floats above the page with a subtle highlight at the top edge, mimicking a light source positioned above and slightly in front of the element. The outer shadow creates the elevation while the inner highlight defines the top edge.
Working with Multiple Shadows
Box-shadow supports comma-separated layers, allowing unlimited shadow combinations on a single element. Each additional layer compounds the depth effect:
.multi-shadow {
box-shadow:
0 1px 3px rgba(0, 0, 0, 0.12),
0 1px 2px rgba(0, 0, 0, 0.08),
0 4px 6px rgba(0, 0, 0, 0.05);
}
Multiple shadows create rich, nuanced depth that single shadows cannot achieve. The layers render from front to back, meaning the first shadow listed appears closest to the viewer. Common use cases include:
- Material Design elevation system — Google specification uses multiple shadow layers at increasing offsets to represent different elevation levels from 1 to 24
- Neon glow effects — Combine a bright core shadow with progressively larger, more transparent shadows in the same hue to simulate actual light emission
- Layered depth — Stack shadows at different offsets with varying blur values to simulate complex lighting conditions from multiple sources
Create a neon glow effect with three shadow layers expanding outward:
.glow {
box-shadow:
0 0 5px #00ff00,
0 0 10px #00ff00,
0 0 20px #00ff00;
}
This produces a bright green glow that appears to emit light from the element itself. The first layer creates the bright core, and subsequent layers build the diffusion halo. Experiment with different colors from our Color Palette Generator to create custom glow effects that match your brand identity and color scheme.
Box Shadow and Gradients
Shadows and gradients work together to create some of the most visually compelling effects in modern UI design. A gradient background combined with a well-crafted shadow creates dimensionality that flat colors simply cannot match:
.gradient-card {
background: linear-gradient(135deg, #667eea, #764ba2);
box-shadow: 0 8px 32px rgba(102, 126, 234, 0.4);
}
The shadow picks up a tint from the gradient starting color, creating a cohesive visual effect that feels intentional and polished. This technique appears throughout modern web design in hero sections, feature cards, pricing tables, and call-to-action buttons. The shadow color should typically match one of the gradient endpoint colors to maintain visual harmony.
Use our Gradient Generator to create beautiful gradient backgrounds, then apply matching box-shadow colors for a polished, professional look that elevates your entire design system.
Glassmorphism and Frosted Glass Effects
Glassmorphism became one of the most influential design trends by combining semi-transparency, backdrop blur, and soft shadows. The signature glassmorphism shadow is soft, widely spread, and light in color to match the translucent nature of the glass effect:
.glass-card {
background: rgba(255, 255, 255, 0.15);
backdrop-filter: blur(10px);
border-radius: 16px;
border: 1px solid rgba(255, 255, 255, 0.2);
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}
The key to convincing glassmorphism is a shadow that is both soft and expansive, suggesting the glass panel floats above a distant background. Avoid hard, dark shadows that contradict the transparent, airy nature of the glass effect. The border with low opacity helps define the glass edge without making it look opaque.
Our Glassmorphism Generator automates this entire process, letting you adjust transparency, blur intensity, border styling, and shadow settings visually until you achieve the perfect frosted glass look.
Animated Shadows
Shadows can animate smoothly using CSS transitions or keyframe animations. This adds a layer of interactivity and polish that users notice and appreciate:
.card {
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
transition: box-shadow 0.3s ease;
}
.card:hover {
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.15);
}
On hover, the card appears to physically lift off the page, providing immediate and intuitive visual feedback. This pattern is standard across the modern web for cards, buttons, and any interactive element. For more complex animation sequences, CSS keyframes give you full control:
@keyframes pulse-glow {
0%, 100% {
box-shadow: 0 0 5px rgba(0, 150, 255, 0.3);
}
50% {
box-shadow: 0 0 20px rgba(0, 150, 255, 0.6);
}
}
.pulsing-button {
animation: pulse-glow 2s infinite;
}
This creates a breathing glow effect that draws the eye to call-to-action buttons, notification badges, or loading states. The smooth transition between shadow intensities feels natural and engaging rather than jarring. Combine animated shadows with our CSS Animation Generator to create sophisticated interactive experiences with custom timing and easing functions.
Box Shadow in Layout Contexts
Shadows play an essential role in establishing visual hierarchy within layout systems. In flexbox and CSS grid layouts, shadows differentiate content cards, highlight active elements, and create separation between distinct content sections.
When building a flexbox-based card grid, apply consistent shadow treatment to maintain visual rhythm across all cards:
.card-grid {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.card {
flex: 1 1 300px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
border-radius: 12px;
padding: 24px;
}
Cards in this grid share the same shadow styling, creating a cohesive visual system where every card sits at the same perceived elevation. The subtle shadow gives each card definition without overpowering the content or competing for attention. When users hover over a card, increasing the shadow elevation provides clear interactive feedback.
Our Flexbox Generator helps you build responsive card layouts quickly, and pairing them with consistent shadow styles produces professional, production-ready results.
Combining Shadows with Clip Paths
CSS clip-path creates non-rectangular shapes by clipping elements to custom geometries, and shadows applied to clipped elements follow the clip boundary rather than the original box. This creates interesting visual possibilities that standard rectangular shadows cannot achieve:
.clipped-shadow {
clip-path: polygon(0 0, 100% 0, 100% 85%, 50% 100%, 0 85%);
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}
The shadow follows the clipped shape, producing effects that appear genuinely three-dimensional. This technique is especially powerful for diagonal section dividers, featured image frames, testimonial cards, and decorative UI elements that break out of the standard rectangular grid.
Use our CSS Clip-Path Generator to experiment with polygon shapes, curved paths, and inset clipping. See how shadows interact with each clipping variation to produce unique visual effects that set your design apart.
Performance Best Practices
While box-shadow is GPU-accelerated in modern browsers, careless usage can impact rendering performance, especially on lower-end devices. Follow these guidelines for optimal performance:
- Limit blur radius — Large blur values require significantly more computation. Keep blur radii under 50 pixels whenever possible for good performance.
- Minimize shadow layers — Each additional shadow layer increases the rendering cost. Two to three layers are sufficient for virtually any production effect.
- Avoid animating shadows on large elements — Animating shadows on full-page containers or large hero sections can cause jank. Keep animated shadows on small, focused components.
- Use transparency efficiently — Semi-transparent shadows composite differently than opaque ones. For smooth rendering, use rgba or hsla color values with alpha channels.
- Prefer box-shadow over filter: drop-shadow — The box-shadow property is more performant than the CSS filter alternative because it does not require post-processing of the entire element tree.
For static content, shadows have negligible performance impact. The primary concerns arise with animations and when applying dozens of shadow layers across hundreds of elements on the same page.
Browser Support and Fallbacks
The box-shadow property enjoys universal support across all modern browsers including Chrome, Firefox, Safari, Edge, and Opera. Support extends back to Internet Explorer 9, making it one of the safest CSS properties to use in production without concerns about compatibility.
For the rare cases where you need to support Internet Explorer 8 or earlier, shadows simply disappear without breaking the layout. The underlying element and its content remain fully visible and accessible, meaning no special fallback code is required. This graceful degradation is why box-shadow is considered production-ready for any website.
Conclusion
CSS box-shadow is an essential tool in every web designer and developer's arsenal. From subtle elevation cues that improve usability and guide user attention to dramatic glow effects that capture focus and create atmosphere, mastering box-shadow unlocks a new dimension of design possibilities.
Start with simple offset shadows and gradually explore multiple layers, inset shadows, and animated effects. Use visual tools to accelerate your learning and experiment with combinations you have not tried before. The difference between a flat interface and one with thoughtful shadow treatment is immediately noticeable to users.
Our Box Shadow Generator is the perfect companion for hands-on learning. Generate production-ready CSS code visually, then fine-tune it as your understanding deepens. For related design techniques, explore our collection including the Gradient Generator and Glassmorphism Generator to complete your visual design toolkit.
For further reading, the MDN Web Docs on box-shadow provide the most authoritative technical reference available, and the CSS-Tricks Complete Guide to box-shadow remains one of the best community-written resources with practical examples and code snippets for every use case.