box-shadow

Avatar of Sara Cope
Sara Cope on (Updated on )

UGURUS offers elite coaching and mentorship for agency owners looking to grow. Start with the free Agency Accelerator today.

The box-shadow property in CSS is for putting shadows on elements (sometimes referred to as “drop shadows”, ala Photoshop/Figma).

.card {
  box-shadow: 0 3px 10px rgb(0 0 0 / 0.2);
}

That syntax is:

box-shadow: [horizontal offset] [vertical offset] [blur radius] [optional spread radius] [color];
  1. The horizontal offset (required) of the shadow, positive means the shadow will be on the right of the box, a negative offset will put the shadow on the left of the box.
  2. The vertical offset (required) of the shadow, a negative one means the box-shadow will be above the box, a positive one means the shadow will be below the box.
  3. The blur radius (required), if set to 0 the shadow will be sharp, the higher the number, the more blurred it will be, and the further out the shadow will extend. For instance a shadow with 5px of horizontal offset that also has a 5px blur radius will be 10px of total shadow.
  4. The spread radius (optional), positive values increase the size of the shadow, negative values decrease the size. Default is 0 (the shadow is same size as blur).
  5. Color (required) – takes any color value, like hex, named, rgba or hsla. If the color value is omitted, box shadows are drawn in the foreground color (text color). But be aware, older WebKit browsers (pre Chrome 20 and Safari 6) ignore the rule when color is omitted.

Using a semi-transparent color like rgba(0, 0, 0, 0.4) is most common, and a nice effect, as it doesn’t completely/opaquely cover what it’s over, but allows what’s underneath to show through a bit, like a real shadow.

Examples

Inner Shadow

You use the inset keyword:

.shadow {
   box-shadow: inset 0 0 10px #000000;
}

Ancient History

Here’s a snippet with vendor prefixes giving as deep of browser support as easily possible:

.shadow {
  -webkit-box-shadow: 3px 3px 5px 6px #ccc;  /* Safari 3-4, iOS 4.0.2 - 4.2, Android 2.3+ */
  -moz-box-shadow:    3px 3px 5px 6px #ccc;  /* Firefox 3.5 - 3.6 */
  box-shadow:         3px 3px 5px 6px #ccc;  /* Opera 10.5, IE 9, Firefox 4+, Chrome 6+, iOS 5 */
}

You can even get a box shadow to work in IE 8. You need an extra element, but it’s do-able with filter.

<div class="shadow1">
  <div class="content">Box-shadowed element</div>
</div>
.shadow1 {
  margin: 40px;
  background-color: rgb(68,68,68); /* Needed for IEs */

  -moz-box-shadow: 5px 5px 5px rgba(68, 68, 68, 0.6);
  -webkit-box-shadow: 5px 5px 5px rgba(68, 68, 68, 0.6);
  box-shadow: 5px 5px 5px rgba(68, 68, 68, 0.6);

  filter: progid:DXImageTransform.Microsoft.Blur(PixelRadius=3,MakeShadow=true,ShadowOpacity=0.30);
  -ms-filter: "progid:DXImageTransform.Microsoft.Blur(PixelRadius=3,MakeShadow=true,ShadowOpacity=0.30)";
  zoom: 1;
}
.shadow1 .content {
  position: relative; /* This protects the inner element from being blurred */
  padding: 100px;
  background-color: #DDD;
}

One-Side Only

Using a negative spread radius, you can get squeeze in a box shadow and only push it off one edge of a box.

.one-edge-shadow {
  box-shadow: 0 8px 6px -6px black;
}

Multiple Borders & More

You can comma separate box-shadow any many times as you like. For instance, this shows two shadows with different positions and different colors on the same element:

box-shadow: 
  inset 5px 5px 10px #000000, 
  inset -5px -5px 10px blue;

The example shows how you can use that to create multiple borders:

By applying the shadows to pseudo elements which you then manipulate, you can get some pretty fancy 3D looking box shadows:

Super smooth shadows via multiple comma-separated values:

Comparison with filter: drop-shadow()

They can do similar things! But also, wait for it, different things. This is a deep dive article on this. Perhaps the biggest thing to know is that a filter will hug the edges of an element moreso than the rectangular box-shadow will do (even though box-shadow does hug rounded corners).

Browser support

This browser support data is from Caniuse, which has more detail. A number indicates that browser supports the feature at that version and up.

Desktop

ChromeFirefoxIEEdgeSafari
4*3.5*9125*

Mobile / Tablet

Android ChromeAndroid FirefoxAndroidiOS Safari
11511544.0-4.1*

More information