Box-shadow: shadow effect on elements in css3

Thursday 19 February 09 by Onsitus in CSS3

css3 box-shadow

The css3 property box-shadow allows to add without the use of images a shadow effect to the selected elements.

Thanks to the different values of the box-shadow, we can specify that the shadow is positioned to the left/right or top/bottom on the outside of the element, its gradient of blur and its color.

The box-shadow is not part of the so-called ‘box-model’, meaning that the shadow is not considered in the final total measure of the element.

This css3 property can be combined with other properties such as css3 border-radius.

NB: The css3 property box-shadow is at the moment supported by Safari 3.1+ adding the prefix -webkit., Firefox 3.5+ adding the prefix -moz. (updated May 2009)

The box-shadow requests at least 3 values, 4 at the maximum:

  • the horizontal length of the shadow
    • This value can be positive for a right shadow or negative for an effect to the left.
    • Example:
      box-shadow:10px 10px 20px #000000;
      box-shadow:-10px 10px 20px #000000;
  • the vertical length of the shadow
    • This value can be positive for a top shadow or negative for an effect at the bottom.
    • Example:
      box-shadow:10px 10px 20px #000000;
      box-shadow:10px -10px 20px #000000;
  • the blur radius - the degree of the blur shadow
    • This value can only be positive with zero as a minimum
    • The highest is this value, the more blurred will be the shadow
    • This value can be omitted, equal to the minimum value of 0 for no blur and neat edges.
    • Example:
      box-shadow:10px 10px 20px #000000;
      box-shadow:10px 10px 0 #000000; equal to box-shadow:10px 10px #000000;
  • the shadow’s color
    • Example:
      box-shadow:10px 10px 20px #000000;
      box-shadow:10px 10px 20px black;
      box-shadow:10px 10px 20px rgb(0,0,0);

NB: in the working draft of the W3C for the css3 property box-shadow, 2 others values are proposed but not actually supported by any browsers:
- the ’spread radius’ to add the shadow in all the direction of the element
- the ‘inset’ to add an inner shadow instead of an outer one

Example of box-shadow

1. Box-shadow with positive value

Preview done with Safari 3.1:
css3 box-shadow esempio 1

The result:

box-shadow:10px 10px 20px #000;
-webkit-box-shadow:10px 10px 20px #000;
-moz-box-shadow: 10px 10px 20px #000;

2. Box-shadow with negative value

Preview done with Safari 3.1:
css3 box-shadow esempio 2

The result:

box-shadow:-20px -20px 5px #000;
-webkit-box-shadow:-20px -20px 5px #000;
-moz-box-shadow: -20px -20px 5px #000;

3. Box-shadow with no blur specified (equal to zero)

Preview done with Safari 3.1:
css3 box-shadow esempio 3

The result:

box-shadow:-10px 5px #000;
-webkit-box-shadow:-10px 5px #000;
-moz-box-shadow: -10px 5px #000;

4. Box-shadow + border-radius

Preview done with Safari 3.1:
css3 box-shadow esempio 4

The result:

box-shadow:5px -5px 10px #000;
-webkit-box-shadow:5px -5px 10px #000;
-moz-box-shadow:5px -5px 10px #000000;
border-radius:25px; -moz-border-radius:25px; -webkit-border-radius:25px;

5. Box-shadow applied to 2 neighboring elements - proof that the shadow is not part of the box-model

Preview done with Safari 3.1:
css3 box-shadow esempio 5

The result:

Last update May 2009 - compatible with Firefox 3.5+

18 comments

Stay updated with our RSS Feed

{ 14 trackbacks }

CSS3 Exciting Functions and Features: 30+ Useful Tutorials | Noupe
05.21.09 at 21:18
30个CSS3新功能的实用教程 « SonicHtml工作室- PSD转HTML / XHTML,CSS / W3C验证 / 多浏览器支持 / WordPress模板 / Joomla模板
05.29.09 at 11:57
70 Must-Have CSS3 and HTML5 Tutorials and Resources - Iconlandia
08.10.09 at 14:32
70 Must-Have CSS3 and HTML5 Tutorials and Resources - Programming Blog
08.11.09 at 23:55
Adept > 70 Must-Have CSS3 and HTML5 Tutorials and Resources
08.16.09 at 20:00
70 CSS3 and HTML 5 tutorials and resources | Netfire.us - Design tutorials, articles, resources, and creative inspiration.
08.17.09 at 22:12
70 Must-Have CSS3 and HTML5 Tutorials and Resources | huibit05.com
08.20.09 at 07:27
CSS3 y HTML5: Tutoriales y recursos para el nuevo diseño web | Recursos para desarrollo y diseño web - AlmacenPlantillasWeb Blog
08.31.09 at 08:06
CSS3的新功能快速体验和应用 | CssRainBow.cn
09.21.09 at 07:10
CSS3 and HTML5 Tutorials and Resources « Web Developer Blog
11.22.09 at 14:01
Playing with CSS3
12.08.09 at 11:19
Playing with CSS3 | Graphic Design Pro
12.10.09 at 07:54
Playing with CSS3 | iDESIGN
01.06.10 at 13:32
12 Killer Tips for Designing in the Browser | Design Shack
01.08.10 at 04:49

{ 4 comments… read them below or add one }

Jürgen Jeka 05.07.09 at 05:02

-moz-box-shadow is now supported in Gecko 1.9.1 (Firefox 3.5).
See https://developer.mozilla.org/en/CSS/-moz-box-shadow

Onsitus 05.07.09 at 07:03

Ciao,
thanks for the segnalation. Really I should resee most css3 properties as firefox3.5 has introduced lots of new things. Back to the testing stage and updating! :)

Kiran G 01.08.10 at 10:51

I do not see the shadows in IE :(

Onsitus 01.10.10 at 13:34

Box-shadow is not supported by Internet Explorer.

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous post: Css horizontal and vertical menu - css example 5

Next post: Free css template 6 - 2 col fixed/fluid