What is the CSS box-shadow Property?
The CSS box-shadow property adds one or more shadow effects to an element's bounding box. Unlike a real shadow cast by light, a box shadow is drawn as a copy of the element's rectangular border box, offset in any direction, blurred, and colored. It is one of the most widely used visual effects in modern web design, used for everything from subtle card depth to dramatic glow effects.
Box shadows were introduced in CSS3 and are now supported by every modern browser without any vendor prefix. You can apply them to any block-level or inline-block element, including div, button, input, and img.
box-shadow: 0 4px 24px -4px rgba(0,0,0,0.12);Understanding the Syntax
The full syntax for a single box-shadow layer is:
box-shadow: [inset] <offset-x> <offset-y> [blur] [spread] <color>;- offset-x — How far to push the shadow horizontally. Positive values move it right, negative values move it left.
- offset-y — How far to push the shadow vertically. Positive pushes it down, negative pushes it up.
- blur-radius — Optional. Controls how blurry the shadow edges are. Zero gives a hard edge. Larger values create softer, more diffused shadows.
- spread-radius — Optional. Expands or shrinks the shadow. Positive values make it larger than the element, negative values shrink it. Useful for tricks like creating an outline effect.
- color — The shadow color. Using
rgba()lets you control opacity without a separate property. - inset — Optional keyword that flips the shadow inside the element, creating an inner shadow effect.
Layering Multiple Shadows
One of the most powerful features of box-shadow is that you can stack as many layers as you need, separated by commas. Professional designs rarely use a single flat shadow. Instead, they combine two or three layers at different distances and opacities to mimic how real light behaves — close light creates a sharper, darker shadow while ambient light creates a wider, lighter one.
box-shadow:
0 2px 8px rgba(0,0,0,0.08),
0 8px 24px -4px rgba(0,0,0,0.16);The first layer handles the tight, immediate shadow close to the surface. The second layer provides the broader ambient shadow that gives the element its sense of elevation. This two-layer technique is the foundation of Google's Material Design elevation system and most modern design systems.
Inset Shadows for Depth and Pressed States
Adding the inset keyword draws the shadow on the inside of the element. This creates the impression that the element is recessed into the page rather than elevated above it. Inset shadows are commonly used for:
- Input fields to indicate a sunken, editable area
- Button "pressed" states when combined with
:activeselector - Well components that show content sitting below the page surface
- Neumorphic UI elements that combine both outer and inner shadows
box-shadow:
-4px -4px 8px rgba(255,255,255,0.7),
4px 4px 8px rgba(0,0,0,0.12);Performance Considerations
Box shadows are painted by the browser's compositor on a separate layer. This means they are generally performant and do not trigger layout recalculations. However, there are a few things to keep in mind:
- Avoid animating blur-radius. Changing the blur-radius during an animation forces the browser to repaint the shadow every frame, which is expensive on mobile. Animate
opacityinstead. - Limit layers. Five or more shadow layers on dozens of elements can add up. Profile your page if you notice performance issues.
- Use spread-radius for outlines. A box shadow with zero offset and zero blur but a positive spread is a performant alternative to
outline, and it respectsborder-radius.
Frequently Asked Questions
Does box-shadow affect layout?
No. A box shadow takes up no space in the document flow. It is drawn on top of surrounding content without pushing adjacent elements away. If you need the shadow space to be reserved, you would need to add padding or margin manually.
Can I animate box-shadow?
Yes, box-shadow is animatable using CSS transitions or animations. Browsers interpolate between shadow values smoothly. For the best performance, avoid animating the blur-radius; instead animate the whole property on hover from a smaller to a larger shadow using a transition on the element itself.
Why use rgba for shadow color?
Using rgba() colors lets your shadow blend naturally over any background. A shadow defined as rgba(0,0,0,0.15) looks correct whether the card underneath is white, blue, or patterned, whereas a solid #aaa gray would look wrong on dark backgrounds.
What is the difference between box-shadow and drop-shadow?
The box-shadow property always follows the rectangular bounding box of an element, even if the element has a transparent background or irregular shape. The filter: drop-shadow() filter follows the actual visible pixels of the element, including transparency. For a PNG icon with a transparent background, only drop-shadow will hug the icon's shape correctly.