Nimbupani Designs

Drop Shadows with CSS3

Paul Irish showed me this implementation of CSS3 drop-shadows. The demo only works for webkit based browsers (though it will work on Firefox if the appropriate vendor-prefixes are added to the stylesheet).

drop-shadow.png

I first noticed this kind of drop-shadow on Mike Matas’s old website. Looking at the CSS for the CSS3 implementation of the drop-shadow (uses box-shadows to avoid the use of an image), there were a number of things that I found missing:

  1. The article is absolutely positioned. It will not work in Firefox if it is not so (the simple fix is to add display: block; to the style of the article element).
  2. The method requires a block element within article to render correctly. The CSS for the block element has a lot of styles that I think are unnecessary and complicated.
  3. The stylesheet was longer and more complicated than necessary.

As an attempt to try a simpler version of it, I came up with this implementation of CSS3 Drop-shadows. This is what I did:

  1. Add a wrapper around the container that needs shadows. Position it relatively.
  2. Absolute position the pseudo-elements like the demonstration above, except position them behind the main content with z-index: -1, like so:

    article:before, article:after {
    	-webkit-box-shadow: 0 15px 10px rgba(0,0,0, .7);   
    	-moz-box-shadow: 0 15px 10px rgba(0, 0, 0, 0.7);	    
    	box-shadow: 0 15px 10px rgba(0, 0, 0, 0.7);
    	position: absolute; 
    	bottom: 15px; 
    	z-index: -1; 
    	width: 40%; 
    	height: 20%; 
    	content: ""; 
    	background: rgba(0, 0, 0, 0.7); 
    }
    

    Thanks simurai for better box-shadow values!

  3. Fin. This is 44 lines of code as compared to 84 lines of code in the original demo and works on all the latest versions of Opera, Chrome, Safari and Firefox. But, Opera’s, and Firefox’s implementation of transforms are not as good as Webkit’s.

It is interesting to attempt it for fun, but this is definitely not ready for production use!

Update: I have noticed that it sorta breaks on very wide boxes, I have no obvious solution yet, but I have updated the background for article:before and article:after to mitigate that.

Comments

Comments are cached. It may take up to 5 minutes for your comment to appear.
 
Guest's picture

Wonderful…Thanks to Paul Irish and thanks for you ofcourse keeping us posted on the implementation of CSS3 drop-shadows.

 
Guest's picture

Hey.. that’s a really smart way using the before: and after:.. One thing I was thinking.. if you look closely, you can see the hard edges in the shadow.. so maybe something like this would make it look more smooth:

article:before, article:after {-webkit-box-shadow: rgba(0, 0, 0, 0.4) 0px 15px 10px;bottom: 10px;pushing the box up by 10px and pushing the shadow down again by 15px.

 
divya's picture

Thanks Simurai! You are right! The box had to be pushed by 15px though. 

 
Guest's picture

i still think you can get better results with the help of a tiny shadow image under the box the new »image-border« tag combinded stretch:

http://sandbox.faroit.com/CSS3.html

 
Guest's picture

This is a brilliant use of the :before and :after pseudo-elements. I keep finding them more and more useful. :)

Thanks for sharing! :D

 
Guest's picture

Great box ! Thx !

 
Guest's picture

Very nice demo, one backdraw: degradation is rather bad though. Needs some css-amandments…..

 
divya's picture

@Faro Thanks! The intention of this demo is to try and do away with images as much as possible. But your demo does look cool!  

 
Guest's picture

I wrote about using :before and :after for these sorts of things (to keep HTML semantic), you might find it interesting - http://nicolasgallagher.com/multiple-backgrounds-and-borders-with-css2/

 
divya's picture

Thanks Nicolas, it is indeed useful!

 
Guest's picture

This is so awesome! But wait a minute, why wouldn’t it be ready for production? I’m a big fan of progressive enhancement, and I definitely think that this can be used right away. However, I might be talking rubbish, ‘cause haven’t even tested this yet. I’ll do it right now…

 
Guest's picture

I’m back with the results. This is really an awesome technique that works in all modern browsers, and with the use of a library like modernizr, it can be made to degrade nicely in other browsers (I’m looking at you IE).

So, to conclude, I’m going to start using this right away, together with modernizr, html5, css3, and some lovely MooTools to spice it all up.

P.S. gosh, it’s really awesome being a web dev/designer these days :)  

 
divya's picture

Wow thanks Zlatan! It does work now, it was freezing up older versions of webkit browsers, but seems to be no longer the case! 

 
Guest's picture

No, thanks to you for making something like this :)

Oh, and btw, I just found out about your project HTML 5 Boilerplate, and I must say that I’m impressed!  

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account, used to display your avatar.
  • Allowed HTML tags: <p> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Adds typographic refinements.

More information about formatting options