CSS Box Shadow
Control depth and dimension with css box shadows. Create elevation, focus states, and stunning visual effects that enhance user experience.
How does box-shadow work in CSS?
box-shadow draws a shadow around an element from up to five values: horizontal offset, vertical offset, blur, spread and colour, as in box-shadow: 0 4px 12px rgb(0 0 0 / 0.15);. Add inset for an inner shadow, and separate several shadows with commas for more realistic depth.
.card { box-shadow: 0 1px 2px rgb(0 0 0 / .06), 0 4px 12px rgb(0 0 0 / .08); }
.input:focus { box-shadow: 0 0 0 3px rgb(59 130 246 / .4); } /* focus ring */
.pressed { box-shadow: inset 0 2px 4px rgb(0 0 0 / .2); }Interactive Box Shadow Demo
Code Editor
<div class="card soft">Soft layered shadow</div>
<div class="card hard">Hard offset shadow</div>
<div class="card inset">Inset shadow</div>
<input class="ring" placeholder="Focus me: shadow ring">Preview
Box Shadow Syntax
Box shadows add depth and visual interest to elements. Understanding the syntax is key to creating effective shadow effects that enhance your design.
Complete Box Shadow Syntax
box-shadow: [inset] offset-x offset-y [blur-radius] [spread-radius] color;offset-x: Horizontal position (+ = right, - = left)
offset-y: Vertical position (+ = down, - = up)
blur-radius: Blur amount (0 = sharp edge)
spread-radius: Shadow size (+ = larger, - = smaller)
color: Shadow color (rgba recommended)
inset: Creates inner shadow (optional)
Material Design Elevation
Material Design uses consistent elevation levels to create visual hierarchy. Each level represents a different z-axis position in space.
Low Elevation (1-2)
Cards, buttons, input fields
0 1px 3px rgba(0,0,0,0.12)Medium Elevation (3-4)
Dialogs, menus, floating buttons
0 10px 20px rgba(0,0,0,0.19)High Elevation (5)
Modals, overlays, navigation drawer
0 19px 38px rgba(0,0,0,0.30)Advanced Shadow Techniques
Multiple Shadows
Combine multiple shadows for complex lighting effects and realistic depth.
box-shadow:
0 1px 3px rgba(0,0,0,0.12),
0 1px 2px rgba(0,0,0,0.24);Inset Shadows
Create inner depth for pressed buttons, input fields, and recessed areas.
box-shadow:
inset 0 2px 4px rgba(0,0,0,0.1);Performance & Best Practices
Best Practices
- • Use consistent elevation levels throughout your design
- • Apply shadows purposefully, not decoratively
- • Consider the light source direction
- • Use RGBA colors for better control
- • Test shadows on different backgrounds
Performance Tips
- • Avoid overly complex shadow combinations
- • Use will-change for animated shadows
- • Consider hardware acceleration
- • Simplify shadows on mobile devices
- • Remove shadows when not needed
Modern Shadow Trends
Neumorphism
Soft, extruded appearance that mimics physical buttons and controls.
Glassmorphism
Frosted glass effect with backdrop blur and subtle shadows.
CSS Box Shadow — step by step
box-shadow Syntax
box-shadow: x-offset y-offset blur spread color. Example: 0 4px 6px -1px rgba(0,0,0,0.1) creates a subtle card shadow.
🎯 Quick Check
Which keyword draws the box-shadow inside the element instead of outside?
CSS Box Shadow: summary
Key points
- Soft, natural shadows use low-opacity colours and several layers rather than one dark shadow.
- A spread value with zero blur makes a solid ring that doesn't affect layout, which is useful for focus states.
- Shadows follow
border-radiusbut not transparent image shapes. Usefilter: drop-shadow()for those. - Animating
box-shadowis costly. Animate theopacityof a pseudo-element that carries the shadow instead.
Frequently asked questions
›What is the difference between box-shadow and drop-shadow?
box-shadow follows the element's rectangular box. filter: drop-shadow() follows the visible shape, including transparent areas of PNGs and SVGs.
›How do I add a shadow on one side only?
Use a vertical offset with a negative spread so the shadow doesn't show on the other sides, for example box-shadow: 0 8px 6px -6px rgb(0 0 0 / .3);.