PSD to HTML conversion PSD to HTML conversion PSD2HTML.com with over 300 professionals takes the designs to HTML and beyond

Code Snippet

Home » Code Snippets » CSS » CSS Box Shadow

CSS Box Shadow

Used in casting shadows off block-level elements (like divs).

.shadow {
  -moz-box-shadow: 5px 5px 5px #ccc;
  -webkit-box-shadow: 5px 5px 5px #ccc;
  box-shadow: 5px 5px 5px #ccc;
}
  1. The horizontal offset 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 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, if set to 0 the shadow will be sharp, the higher the number, the more blurred it will be.
  4. Color

Example

Inner Shadow

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

Example

Internet Explorer Box Shadow

You need extra elements...

<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;
}

Subscribe to The Thread

  1. Dor says:

    Very cool, but if you could add examples of how it looks (either auto generated or a page with it) could be awesome.

    Also, clipboard function for us Windows users would be very much appreciated :D

  2. vlado says:

    yeah – you can also add a css rounded corners to that – it will bring much more pleasant look

  3. Ian M says:

    Any way to make this work in IE7 or 8?

  4. Adam says:

    I think you’ve got an extra zero in your inset code there–your reference url writes it thusly:

    box-shadow:inset 0 0 10px #000000

    whereas you have

    box-shadow:inset 0 0 10px 0 #000000
  5. Dragonwolf says:

    While -webkit-box-shadow should, in theory, work the same way that -moz-box-shadow does in Firefox, I’ve found that it’s not necessarily the case in practice, especially when using RGBA values for your color. I’ve found that I’ve had to put the RGBA value first for Safari to pick it up (ironically, Chrome doesn’t seem to care).

    It seems, too, that the Webkit engine itself doesn’t support the Inset value yet, either (neither Chrome nor Safari display it). Hopefully, that will change, soon (from what I’ve been able to find, they’re still working the bugs out of the box-shadow support).

  6. looks very nice!
    thx for sharing with us. just waiting for a workaround for IE 7/8 without any images!

  7. dikki says:

    Just found one: http://www.netzgesta.de/tripleb/

    Works great for static content, even with boxes.

    However, it relies on the name of a class. It parses this name through a js file and render the block. I need it for a mouseover event though, and it doesn’t parse it again after this event.

  8. Saifu says:

    not working in ie…..

    • Starr says:

      IE doesn’t support ANY CSS3, not until IE9 comes out.

      Any webby person out there would know this.

  9. Just curious. How do you make the shadow equally go around the box. As opposed to just off to the right?

    • Starr says:

      set the horizontl and vertical offsets to 0px…

      .shadow {
        box-shadow: 0px 0px 5px #ccc;
        -moz-box-shadow: 0px 0px 5px #ccc;
        -webkit-box-shadow: 0px 0px 5px #ccc;
      }
    • Mikkel says:

      Any way to select which sides to get the shadow (remove the bottom), but still having the shadow centered?
      I’m trying to apply it to tabs on my nav, and it look pretty weird when the shadow appears below the tab (which is supposed to look like part of the page below it)

    • Chris Cox says:

      Mikkel: (sorry, hit the limit for nesting comments) Off the top of my head, what about absolute positioning and z-index to keep the bottom shadow lower than the page?

    • Ryan says:

      Perhaps try a negative vertical offset?

    • Brad says:

      Mikkel, you might also try adding multiple shadows with the same color so that the top and both sides are shadowed but the bottom is not. Like this:

      .shadow{box-shadow:5px -5px 5px #ccc, -5px -5px 5px #ccc;}

      Of course, you can get more sophisticated with multiple shadows to achieve just the effect your after, but this gives you the idea.

    • Tom says:

      @Mikkel: Offset your inner-shadow in one direction, and then add a negative spread equal to or greater than your offset in order to make the shadow bigger than your box. Example for FireFox:

      .shadow {
      -moz-box-shadow: 5px 0px 5px -5px #000 inset;
      }

  10. dimas says:

    Any workaround for webkit?

  11. Luke says:

    Inset does not work bro!?

  12. thanks for sharing it with us, an interesting article and I have tried it and the results are amazing ..

  13. blinkdt says:

    Until this works in IE, who cares? Amazingly heady stuff to view the world through an Apple optic, but be careful of the ledge . . . . And, yeah, Win7 and Office 2010 are out and CSS3/9 will be here soon. Thank you, Microsoft.

    • Duncan says:

      Maybe we shouldn’t get too excited about IE 9, or thank Microsoft too soon.
      Could be they have just created another hoop for us to jump through. IE 9 requires Vista as minimum, 64% of the user base for Windows is reported to be still running XP.
      Can we really expect people to possibly replace their entire system simply to get IE 9.
      The biggest hold up with Microsoft is that the browser is bundled so tightly to the Operating System, whereas all the other major browsers are less platform dependant and more easily upgraded to meet newer features.
      We’ve cursed IE 6 for a long time now, sadly we might be heading for a similar scenario with IE 7 and even IE 8.

    • Erik says:

      That statistic is false. Ie9 looks and functions (from what I’ve tested, great). Being that it is, as you say, platform dependent it will forever leverage hardware acceleration far better than anything else provides. It’s not a downside at all.

      On another note… If most of you have been following ie9 you’ll notice that it does not have the CSS 3 hype everyone is anticipating.

      No shadow, no gradient, no transitions, no cool designer tricks…

    • richphitzwell says:

      IE as a whole is below 50% marketshare now. If we remove the lagers such as ie6 that are pretty much there just to support legacy programs and take away ie7 which again is pretty much just there to support legacy programs and some lagers, then the actual market share of users on ie is around 36%. Not sure if we really need to hang onto the must wait for ie bandwagon anymore especially with something that degrades gracefully in all versions of IE. Even my statistics for all my US based websites show IE falling off.

      When we include the incredible growing marketshare of smartphone based browsers except win7phone the dominance of non css3 or html5 browsers (IE) drops even further.

      I can respect creating two stylesheets though, one for IE and one for all the others, but at the same time, my current thoughts are that ie is no longer the absolute dominant browser and that I dont have to design around it anymore. Just my 2c

  14. kirankumar says:

    not working in IE7

  15. not working in IE8 either :(

  16. Jeffrey says:

    Hate to be a nag, but to future proof it the box-shadow property should appear last so when CSS3 is the accepted standard it will be used.

    • Ryan says:

      I was just thinking the same thing.

    • I’m not sure it matters…

      As long as all of them are the same, the browser will either support it or not. If the vendor extension overrides the original, who cares? It’s the same rounded corner. Then if that browser later stops supporting the extension, the it also doesn’t matter because it’ll use the native property and ignore the extension.

  17. Ramm says:

    I’ve been using CSS3 Pie to get the same effect in IE, without braking anything else, it’s been working fine for me. css3pie.com

  18. Ricardo Verhaeg says:

    When I add the box-shadow property on a fieldset with legend, on firefox the display of the shadow takes the width of the fieldset + legend, but on Chrome or Opera it shows without the legend (as I expected)
    Is there any workaround this?

  19. Victor Augusto says:

    Thaks, man.I was looking for this tutorial.

  20. It does work in IE. Gmail does not use PNG images for shadows of its chad box setting popup but shadows do appear same in IE and Firefox.

    What to do?

  21. Aparna Chakraborty says:

    It is not working in IE 7 and 8.

  22. Chris Enloe says:

    Is there any way that I can achieve the same affect when a user just hovers over a box?

    • diglin says:

      Why not just use a pseudo class

      .shadow:hover {
        -moz-box-shadow: 0px 0px 5px #ccc;
        -webkit-box-shadow: 0px 0px 5px #ccc;
        box-shadow: 0px 0px 5px #ccc;
      }

      It depends on which html tag use it this pseudo element but it should work. I use it for rollover input button (I don’t use the shadow effect but a color change effect)

  23. Ah inset is what achieves the inner shadow. I was trying to use negative pixel values wondering if that might work. Thanks Chris your a life saver…;);)

  24. If using the CSS box shadow on a global scale and for whatever reason you need to lose the effect on one particular sub class just use:

    .class {
    -moz-box-shadow: none; -webkit-box-shadow: none; box-shadow: none;
    }

  25. sriram says:

    Using this below code you can achieve the CSS shadow in IE Browser.

    Shadow for two sides(right and bottom)

    .shadow-ie {
    filter: progid:DXImageTransform.Microsoft.Shadow(Strength=4, Direction=135, Color=’#000000′);
    }
    Shadow for all side in IE Browser

    .shadow-ie {
    filter: progid:DXImageTransform.Microsoft.Shadow(color=’#ece8e8′, Direction=45, Strength=3)
    progid:DXImageTransform.Microsoft.Shadow(color=’#ece8e8′, Direction=135, Strength=3)
    progid:DXImageTransform.Microsoft.Shadow(color=’#ece8e8′, Direction=225, Strength=3)
    progid:DXImageTransform.Microsoft.Shadow(color=’#ece8e8′, Direction=315, Strength=3);
    }

    Depends upon your page background choose your shadow color.

  26. Damir says:

    Is there a way to make an inset shadow only for one side of box?
    Have in mind that should work with textured backgrounds too.

  27. Yuval says:

    question: does all browsers that suppors one of the shadow csss also supports rgba notation?
    I saw Google Image use box-shadow with the color part as rgba(0,0,0,0.65) – to add a nice transparency effect to the shadow – and I wanna do it tot – but – do I need to supply a ‘fallback color’ or is it safe to assume that if the browser is “good enough” to support shadow – it will also surely support rgba?
    Thanks.

  28. I tried it out on http://leadtheme.com but it didn’t didn’t quite look how I wanted. Maybe I will try that transparency thing. For now I just had to use a png :( . Thanks for the the code though… I have used it on modals and stuff and it works cool if you use the right colors :)

  29. Cameron Spear says:

    I was really excited when I saw there was a download for a TextMate snippet, but it doesn’t work. It says it’s corrupted (and it seems to say that because the content of the snippet is the snippet in question, but with full HTML markup as it appears on this page).

  30. vipil says:

    Helpful. but what about IE ?

  31. costicanu says:

    Your inset shadow example not working in Safari. After your code, example is empty! Check it, is the last example in this article.
    -webkit-box-shadow:inset 0 0 10px #000000;

  32. Ruann says:

    Just add below to give your border more roundness:

    -moz-border-radius: 15px 15px 15px 15px;

    Hope it’s helpful to someone.

  33. This is great, but will IE ever get with it? Seems like there’s always some sort of drastic work around. So annoying!

  34. Myk says:

    is there any way to get it working on images?

    • Mac says:

      Myk: Just place a DIV in front of the image you want the inset shadow on, absolutely position it with z-index, and you’ll get a nice cutout effect with shadowing. I just did it for a google map where I wanted that cutout effect.

    • Myk says:

      Thanks Mac! That one works for inner shadow on images.. :)

  35. Mohamed says:

    Very helpful, I went to my code directly to perform changes!

  36. time says:

    it works on all browser except the main one :) IE

  37. Làm Web says:

    Thank you very much, this is great!

  38. Artem says:

    Thank you very much, could you just help me out to put the shadow on the right side, of the bottom part of the header? I can place it either on the right or on the left… but not both…

  39. sk8erbryan says:

    A lot of comments and people are concerned about the lack of ie support. Web pages DON’T need to look exactly the same in every browser. I know that may come as a shock but as devices, versions and variations expand it’s time to get out of the idea that webpages are static as print layout.

    So bottom line is if you want the shadow to show up the same in each version of IE cut the images, dust up your padding, margins, background-images, position:absolutes and z-indexes. There’s no easy way around it, otherwise use css3, modern browsers will get a modern experience and everyone else won’t know what they are missing.

  40. Hadley says:

    This looks great! How can I make it on both the left and right side of a box, but not the top and bottom? I’m trying to get one white box from top to bottom of the page, with shadowing on either side. Is it possible?

  41. Discover says:

    Nice post, Thanks to sharing., Css shadow effect in IE with example here http://discoverweb.info/css-shadow-for-ie.html

  42. Josh says:

    This doesn’t work.

    It’s amazing that even IE8 still sucks.

  43. Re. support for IE: I find it more amazing that there are still devs around who look for and find pages like this, yet don’t bother reading the comments.

    • Josh says:

      Oh, I know there is support for it in IE. It’s just still not simple, which is amazing.
      Also, shadows in IE still mess up the alignment of cursors in inputs within elements what have been shadowed on more than one side, so it’s still a pain… and slightly buggy.
      Just seems very behind.

    • Arno says:

      has anyone found a way to fix the cursor alignment issue for IE 6-8 when using box shadow via filters? I’d really appreciate a solution (or a hack) as it would be a pain to recreate this using images.

  44. Randy says:

    Is there any way to use a div with an inset shadow to ‘wrap’ around nested divs? AND keep the shadow translucent on top on the nested divs?

    Like this?
    http://www.integralhealth.ca/aboutUs/index.php

    That site uses JPGs with the shadow already incorporated into the images. Any way to do it using CSS only for the inline shadows?

    In my own noob way I tried the following code. Of course it didn’t work.

    #container	{
    	position: relative;
    	width: 800px;
    	height: 1000px;
    	margin: 30px auto;
    	background-color: lightgrey;
    	-moz-box-shadow:inset 0 0 20px #000000;
       	-webkit-box-shadow:inset 0 0 20px #000000;
       	box-shadow:inset 0 0 20px #000000;
    	z-index: 1;
    }
    
    #headspace	{
    	float: left;
    	position: relative;
    	width: 590px;
    	height: 30px;
    	background-color: lightcoral;
    	z-index: -1;
    }
  45. Ivan says:

    man thanks

  46. kRemtronicz says:

    I knew about this effect but until today I did not know I could use it for tables! My new site is still under development but it is coming along REALLY well. I’m so glad of all the things we can do with CSS. ;O)

  47. bob says:

    box shadow has since been dropped from css3 spec

    • Andre says:

      What do you mean ‘dropped from CSS3 spec’? Could you perhaps provide a link to this news? ‘box-shadow’ was only added in CSS3, surely they wouldn’t drop it so quickly…

    • Andre says:

      If you post comments like that, it’s always best to cite the source of your information.

      After a bit of research, looks like it was dropped around Jan / Feb 2010 (found various blog and article comments about this) and then re-added with the ‘inset’ option sometime in April 2010 (http://ajaxian.com/archives/get-some-box-shadow-going).

  48. sunny says:

    Hey . anyone can help me. ? i want my box with the shadow wrapping around it . not for just two edges .Can css do it ?

    • Andre says:

      Hi Sunny,

      Sounds like you need to use the box-shadow without a X or Y offset, in specifying the box-shadow, the four elements are done as follows:
      box-shadow: ;

      So, try using something like this:
      box-shadow: 0px 0px 5px #666;

      Another option is to use RGBa or HSLa for the colour, such as:
      box-shadow: 0px 0px 5px hsla(0,0%,0%,0.5);

      Obviously for now, remember to duplicate your CSS3 lines with the -webkit and -moz prefixes though…

    • Andre says:

      Eh.. Sorry, some parts of my comment were removed. As seen in Chris’s article, the general format of the box-shadow is:

      box-shadow: (x offset) (y offset) (blur radius) (colour);

    • Toiletduck says:

      Hey Andre, thanks for the info.
      How do I get the same effect in EI? My current code is:

      -ms-filter: “progid:DXImageTransform.Microsoft.Shadow(Strength=4, Direction=135, Color=’#000000′)”;
      -filter: progid:DXImageTransform.Microsoft.Shadow(Strength=4, Direction=135, Color=’#000000′)

      But that’s just for 2 side. Should I set the Direction to 0?

  49. Ajay says:

    what about if i want shadow for whole box… ??
    i tried to change Direction but not… work
    please replay i am waiting

    and i want to tell u i am a big fan of u

  50. Antoinette says:

    So, I was wondering if I could make it so there was no body background color and only the inner shadow. Is this possible at all?

    • Andre says:

      Yes, this is possible.. Just set the background-color to ‘transparent’ and add the word ‘inset’ to the box-shadow. For example:

      body {
      background-color: transparent;
      box-shadow: inset 0px 0px 5px #666;
      }

  51. For IE 6, 7, 8, 9 You can do this:

    HTML:

    hello

    CSS:

    .wrapper {
    position: relative;
    filter: progid:DXImageTransform.Microsoft.blur(pixelradius=2.0, makeshadow=’true’, ShadowOpacity=.50);
    background-color: #fff;
    }

    .wrapper-inner {
    border: 1px solid #ccc;
    background-color: #fff;
    position: relative;
    }

    And for FF, Chrome, Safari:

    HTML:

    hello

    CSS:

    .wrapper {
    position: relative;
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px;
    border-radius: 3px;
    -moz-box-shadow: 0 1px 3px #000000;
    -webkit-box-shadow: 0 1px 3px #000000;
    box-shadow: 0 1px 3px #000000;
    }
    .wrapper:after {
    display: block;
    visibility: collapse;
    content: ” “;
    clear: both;
    font-size: 0px;
    }
    .wrapper-inner {
    border: 1px solid #ccc;
    background-color: #fff;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    border-radius: 3px;
    position: relative;
    }

    Works pretty good, IE 6 need a little tweak, but I thought I would let you’all figure that one out. And, it degrades to just a box with a border. Not to shabby.

  52. Here is a outer glow thingy too:

    The HTML:

    hello

    The CSS:

    .wrapper {
    	position: relative;
    	-moz-border-radius: 4px;
    	-webkit-border-radius: 4px;
    	border-radius: 4px;
    	-moz-box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 0.01);
    	-webkit-box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 0.1);
    	box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 0.1);
    }
    
    .wrapper:after {
    	display: block;
    	visibility: collapse;
    	content: " ";
    	clear: both;
    	font-size: 0px;
    }
    
    .wrapper-inner {
    	border: 1px solid #cdcdcd;
    	background-color: #fff;
    	-moz-border-radius: 4px;
    	-webkit-border-radius: 4px;
    	border-radius: 4px;
    	position: relative;
    
    }
    
    /** This stuff is for IE 8, IE 7 just degrades nicely with only a box with a border **/
    
    * html .wrapper {
    	filter: progid:DXImageTransform.Microsoft.blur(pixelradius=5.0, makeshadow='true', ShadowOpacity=.10);
    	background-color: #fff;
    	left: -5px;
    	top: -5px;
    }
    * html .wrapper-inner {
    	bottom: -5px;
    	left: 5px;
    }
  53. Mooseman says:

    Trying to use the inset shadow for a div around a set of scrolling images. But the images appear over the shadow. Any suggestions?

    • Mooseman says:

      I mean without z-index layering; I have links.

    • WEbCreator.Freelancer says:

      use position:absolute in your css file
      n try 2 use z-index possibly showing your links active.. keeping your content intact

    • Alex says:

      Set your image as the Background of a div, and then apply box-shadow to that same div. Shadows will now be over the image. You can also use border-radius as well. Works great.

  54. ysh says:

    does this apply to tables? i mean.. i won’t put in a div.. just in the table.

  55. johntm says:

    I dont want shadow at the bottom.
    what should i do?
    plz tell.

  56. very interesting post indeed, and yea css3 is so much fun but omg IE is always a huge pain in the ass, even IE9 you have to add some sort of a meta tag so the browser will render the page like whatever
    man, i wish Microsoft didn’t inflict such a disaster as IE into web world!

  57. So Divine Design says:

    Question, I have all of my content in a page wrap that also has a class of group. How do I write the code so that the code will go around my page wrap without wiping it out completely. For example I want the shadow to look how it looks on this page.

  58. Kevlyn says:

    Wow, First of all, the site is really really awesome. Great work.

    Now a days I can see, every site is using font inner shadow, I want to know, how to do it, which can be viewed in IE too.

    Thanks a lot…

  59. Kasia says:

    Great website, now is my favorite:)

    Thank you for the simple and clear description.

    Greetings!

  60. Alberto says:

    Hi dears, I have a problem with the shadow:

    I have the folllowing page:

    
    &ltheader>
    &lt!-- some content -->
    &lt/header>
    &ltdiv id="myID">
    &lt!-- some content -->
    &lt/div>
    

    The header element have a shadow proprety, but, when I apply the background gradient at the div element, it overflow the shadow.

    How to solve it?

    • Alberto says:

      Here the CSS:

      
      header {
      	height: 140px;
      	width: 100%;
      	background: url(/img/css/pattern.png) left top repeat;
      	background-color: #252525;
      	/* Box Shadow */
      	box-shadow: 0 2px 8px rgba(0, 0, 0, .37);
      }
      
      #bread {
      	height: 40px;
      	background: -webkit-gradient(linear, left bottom, left top, from(#0054a6), to(#0072bc));
      }
      
  61. What is all this talk about IE – is that a web browser, or something?

  62. Itsmeagain says:

    Use javascript call modernizr
    takes care of all the browsers’ inconsistency.

  63. Thanks, the code runs smoothly on my blog.

  64. Radhika says:

    for ie 8

    -ms-filter: “progid:DXImageTransform.Microsoft.Shadow(Strength=5, Direction=140, Color=’#cccccc’)”;

    for ie 7 and lower browser

    filter: progid:DXImageTransform.Microsoft.Shadow(Strength=5, Direction=140, Color=’#cccccc’);

    but not enjoying the shadow effect :(

Speak, my friend

At this moment, you have an awesome opportunity* to be the person your mother always wanted you to be: kind, helpful, and smart. Do that, and we'll give you a big ol' gold star for the day (literally).

Posting tips:
  • You can use basic HTML
  • When posting code, please turn all
    < characters into &lt;
  • If the code is multi-line, use
    <pre><code></code></pre>
Thank you,
~ The Management ~