Learn how to apply shadow automatically to an image using CSS
13
October

Learn how to drop a shadow to images or photos without using Photoshop! It’s fairly straight-forward and all you need is a few simple CSS codes and this transparent shadow in PNG format.
Below are the two parts of CSS needed. The first one sets the shadow image as the background, the second one will render a border around an image. Explanation of each CSS syntax are provided to give you a better understanding of how the code works.
.img-shadow {
float:right;
background: url(trans-shadow.png) no-repeat bottom right; /* Most major browsers other than IE supports transparent shadow. Newer release of IE should be able to support that. */
}
.img-shadow img {
display: block; /* IE won't do well without this */
position: relative; /* Make the shadow's position relative to its image */
padding: 5px; /* This creates a border around the image */
background-color: #fff; /* Background color of the border created by the padding */
border: 1px solid #cecece; /* A 1 pixel greyish border is applied to the white border created by the padding */
margin: -6px 6px 6px -6px; /* Offset the image by certain pixels to reveal the shadow, as the shadows are 6 pixels wide, offset it by that amount to get a perfect shadow */
}
Save the CSS, and call it in your webpage file through the following HTML codes.
<span class="img-shadow"><img src="tutorial-kinoshita.jpg"/></span>
As I’ve explained above in the CSS code, the PNG transparent image has compatibility issues with Internet Explorer. Let’s put the codes to test with a cyan background in two of the most popular web-browsers on the Internet, Firefox and Internet Explorer. The results below, explains it all.


It is believed that Internet Explorer will be able to support that format in their “Longhorn” version which will be released soon. So as for now, IE-oriented designers should use a white background for their image, instead.
If you like this post, please consider buying me a beer.
















1. .derkilicious » Blog Archive » links for 2005-10-17 | October 18th, 2005 at 11:44 am
[…] Apply drop shadows to images with CSS Apply shadows to images using a transparent PNG and CSS. Breaks in IE6. (tags: css shadows images) […]
2. mrruben5 | October 18th, 2005 at 10:08 pm
png backgrund’s can be made to work with the alphaimageloader, and IE7 will have support for png images default.
You can also use the shadow filter for IE6 instead of the background image. Just use the universal selector html hack to target IE6.
3. admin | October 18th, 2005 at 10:48 pm
mrruben5: thanks for pointing that out.
4. daisy | October 22nd, 2005 at 6:48 am
Excellent tut :D. a great way to use a stable shadow.
You can produce a similar if not better effect using css transperancy
5. admin | October 22nd, 2005 at 11:10 am
Thanks for the comment daisy. Well, I certainly will look into that for future tutorials.
6. bobslt | October 29th, 2005 at 5:24 pm
tnx… it was really cool…
nice…
7. October: End of Month Review - Alex Allied | November 1st, 2005 at 7:34 pm
[…] There are actually three websites under my domain to be accounted for the stats. I’ve launched an online photo album for the fans of Ayumi Kinoshita around mid-October. And THAT you guys, is the reason why the site traffic hiked. I’m speculating that it had accounted for 40% of page views on the stats. The most viewed page being Alexallied.com and Ayumi Kinoshita gallery’s photo viewer. Sadly, my hard-worked tutorial posts had to settle for 3rd and 4th. […]
8. em | January 12th, 2006 at 9:47 pm
for IE: in to the insert:
function correctPNG() // correctly handle PNG transparency in Win IE 5.5 or higher.
{
for(var i=0; i”
img.outerHTML = strNewHTML
i = i-1
}
}
}
window.attachEvent(”onload”, correctPNG);
now the png are visible in IE
bye
9. Alex | January 12th, 2006 at 11:26 pm
em: Thanks for pointing that out
10. mick g | February 2nd, 2006 at 11:20 am
thanks a million for the tutorial and png image!
11. Su | March 18th, 2006 at 12:11 pm
Thanks Alex!
Uh, about the IE issues, what did em meant when he/she said put that script? in the CSS below .image-shadow? or in the post itself?
12. Su | March 18th, 2006 at 1:55 pm
Hi alex, I did everything like you told me, but my pictures kept going to the right-est corner of the post.
is there a way to fix that? I’m using a blogger standard, is there some settings that i need to further do? thanks!
13. Mac | September 1st, 2006 at 9:47 am
The, um… “tutorial” here would be just a bit more credible *if* you actually used the technique you describe, here on the page.
All you have are jpegs of a picture with a border. Better to actually place an tag here and apply your CSS effect dynamically.
14. Nadjib | January 7th, 2007 at 2:52 am
great post, Alex! Thanks for posting this up. I finally found a way to make it look the same on IE as on Firefox. The only trouble I had though, was my image was positioned to the far right on IE…I fixed that by adding “position:relative” to img-shadow class.
thanks again!
15. Sreekanth | June 24th, 2008 at 2:30 am
Hey Alex,
Great post, works awesome on mozilla.
But, i am not pull it off all that well in IE. I am able to get the shadow to the right side of the image but not on the bottom.
any suggestions???