In July 2008 Michael Ventnor implemented the CSS property box-shadow in Mozilla (as -moz-box-shadow, until the spec has stabilized). In this post I’d like to give a quick summary of how box shadows work. I’ve created a little demo page; all the shadows you see in this post are screenshots from that page. Use a recent nightly Firefox build to view it.
Overview
The most basic way of setting a box shadow on a box is using -moz-box-shadow: [x offset] [y offset] [color]:
![]()
-moz-box-shadow: 5px 3px black;
Adding another number lets you specify a blur radius:
![]()
-moz-box-shadow: 3px 3px 5px black;
You can even set a spread radius (this extends the shadow rect):

-moz-box-shadow: 0 0 5px 5px black;
Negative spread values are possible, too:
![]()
-moz-box-shadow: 0 5px 5px -3px black;
Another interesting feature is the ability to set multiple box shadows, separated by comma, starting with the topmost shadow:
-moz-box-shadow:
0 0 20px black,
20px 15px 30px yellow,
-20px 15px 30px lime,
-20px -15px 30px blue,
20px -15px 30px red;
Shadow Opacity
What if you’d like the shadow to be more transparent? Just use the rgba syntax when setting the color and specify an appropriate alpha value:
![]()
-moz-box-shadow: 3px 3px 5px rgba(0, 0, 0, 0.5);
Increasing the opacity of a shadow is slightly more tricky. First you should try playing with the spread radius, but if that doesn’t give the right results, duplicating the shadow layer works, too:
![]()
-moz-box-shadow: 3px 3px 5px black, 3px 3px 5px black;
Inset Box Shadows
Finally I’m getting to the reason I’m writing this blog post in the first place. Just over a week ago, Michael Ventnor added support for inset box shadows.
Using inset box shadows is as simple as adding the inset keyword to a box shadow layer.
![]()
-moz-box-shadow: inset 2px 2px 5px black;
Inset and outset shadows can be combined:
![]()
-moz-box-shadow: inset 2px 2px 5px black, 2px 2px 5px black;
Now the usefulness of multiple shadow layers becomes really apparent. You can use inset box shadows to create gradients, highlights, 3D borders etc. and freely stack them on top of each other. It’s just like using an unlimited number of “inner shadow” layer styles in Photoshop!
In combination with border-radius (which can even be elliptical in Firefox 3.5), the possibilities for creating shiny, image-less buttons are endless. For example, this is a recreated version of the button on getmiro.com:

The CSS code for buttons like this one can quickly become complicated, though… it’s all on the demo page, in case you’re interested.
More Examples
Recreating Cocoa controls with box shadow (and without any images) is fun, too:
| Searchfields: | |
| Aqua buttons: | |
| “Recessed” Cocoa buttons: | |
| “Round textured” Cocoa buttons: |
Adapting To The Background Color
Inset box shadows are painted on top of the background layer. Black inset shadows make the background darker, white shadows make it lighter. If you keep that in mind, you can build box shadow constructs that can adapt to arbitrary backgrounds. I experimented a little and came up with super glossy boxes that look a little like Cocoa select boxes:
(See how the saturation is higher in the lower half of each box?
)
Browser Support
As far as I know, Webkit has been supporting box shadow (as -webkit-box-shadow) since the version that Safari 3 ships with. However, it apparently supports neither the spread radius feature nor inset box shadows.
Firefox will support box shadows starting with version 3.5.
It looks like Opera doesn’t have any box shadow support yet.
Notes
You should keep in mind that box shadows are rendered on the fly whenever your boxes are repainted. Some performance improvements have already been made, but drawing box shadows is still expensive. So if you value rendering speed and need shadows for large boxes (or shadows with large blur values), you should definitely consider using images of “pre-computed” shadows instead (for example as border-images).
Finally, I’d like to thank Michael for implementing this stuff, and David Baron and Robert O’Callahan for reviewing his patches – thanks!
Update 2009-04-08: dbaron approved the inset box shadow patch for 1.9.1 after all, and today I landed it – that means we’ll get inset box shadows in Firefox 3.5! I’ve updated the article accordingly.

scott said
good stuff
smsmith said
I hope you don’t mind, but I used one of your example buttons in a Stylish style for the bookmarks toolbar.
http://userstyles.org/styles/17859
Great stuff! Thanks for all your hard work!
markusstange said
Of course I don’t mind – glad that it was helpful to you!
shadow boxing with -moz-box-shadow at hacks.mozilla.org said
[...] you can also define multiple shadows by defining several shadows, separated by commas (courtesy of Markus Stange): -moz-box-shadow: 0 0 20px black, 20px 15px 30px yellow, -20px 15px 30px lime, -20px -15px 30px [...]
shadow boxing with -moz-box-shadow | fredericiana said
[...] If you want, you can also define multiple shadows by defining several shadows, separated by commas (courtesy of Markus Stange): [...]
谋智社区 » Blog Archives » 颠覆网络35天 ─ 使用-moz-box-shadow渲染阴影 said
[...] 如果你需要,你还可以定义多个阴影,使用逗号隔开(例子来自Markus Stange): [...]
Stefan said
Firebug shows quite a strange behaviour when dealing with your glossy buttons
Just try hovering over the code of one of the buttons in the Firebug window and watch how the margin is displayed. Strange.
Anyway, great stuff!
Add More Sparkle with CSS3 said
[...] CSS text-gradient in Safari while Markus Stange has some fun with box shadows in his post “Fun with Box Shadows.” Markus produces some impressive effects for buttons in Firefox 3.5 and there is also some [...]
Justen said
The bars look amazing in Firefox 3.5. I can’t wait to see this working in webkit, and I’m sad to know I’ll probably be senile before it shows up in IE.
CSS 3 « Mark Scerri – Eye on the web said
[...] a pure CSS text-gradient in Safari and Markus Stange experiments with box shadows in “Fun with Box Shadows“. He manages to produce impressive effects in Firefox 3.5 and there is also some support of [...]
Markus Stange said
@Stefan: Indeed, that looks funny. Maybe Firebug is using DIVs to create those overlay boxes, which get styled by the box-shadow rules, too?
jt said
Tried duplication the same effects with -webkit-box-shadow. Looks like webkit’s box shadow does not support multiple shadow properties.