14th November 2003, lunch time | Comments (65)
Status | |
---|---|
Code usage | No longer in use on this blog |
Updates | Available |
Demo | Available |
Downloads | Available |
A few people have noticed that the drop shadows I use[d] on images here aren’t part of the pictures themselves, but are added on through the web equivalent of Magic Fairy Dust — CSS. I thought I’d explain how it’s done in case anyone else wanted to know.
I have two different implementations of this technique: one is for images that are used for decorating a post; one is for images that are used to illustrate a post. Both can be seen in the demo.
Now then, before we look at the code, have a nose at this picture; it should give you an overview of what actually happens. (I’ve used a blue background so you can see what’s what, and only swapped to a white background at the end for the full effect.)
This is the code to insert into your web pages.
<div class="img-dec"><img src="cat.gif" width="260" height="200" alt="A stupid waving cat" /></div>
<div class="img-ilus" style="width: 270px;"><img src="cat.gif" width="260" height="200" alt="A stupid waving cat" /></div>
Rather than spend ages trying to describe how I made the drop shadow, I’ll just provide the Photoshop file and GIF files for you to download, and you can see for yourself.
You’ll notice that the image is actually pretty big, at 800×800 pixels. I originally started off with a much smaller file, planning never to display images above 300×200 pixels on my blog, but I soon found that I out grew this limit, and the drop shadow gif just wasn’t large enough. So, knowing that such a simple image is actually pretty small in terms of file size (the 800×800 version I use now is only 3.47KB) I thought I’d save myself future hassle and produce a larger-than-I'll-ever-need version.
My drop shadow is made using a white background, because that’s the colour of my site. If you find that you need yours to sit on top of lots of different colours (maybe you have a style-switcher on your site), then you could always set a transparent background and save the image as a PNG instead. (Be sure to check the current browser support for this though.)
Pop these rules into your style sheet.
div.img-dec {
background: url('shadow.gif') no-repeat bottom right;
clear: right;
float: right;
margin: 0 0 15px 25px;
padding: 0;
position: relative;
}
div.img-dec img {
background-color: #fff;
border: 1px solid #a9a9a9;
display: block;
margin: -5px 5px 5px -5px;
padding: 4px;
position: relative;
}
div.img-ilus {
background: url('shadow.gif') no-repeat bottom right;
margin: 10px 5px;
padding: 0;
position: relative;
}
div.img-ilus img {
background-color: #fff;
border: 1px solid #a9a9a9;
margin: -5px 5px 5px -5px;
padding: 4px;
position: relative;
vertical-align: bottom;
}
And that’s it. It should be pretty easy to get that up and running on your own site, just make sure the CSS paths to the shadow image are correct. Good luck!
Update: Thanks to Susanne Jaeger for helping me get this working in all three major browsers after Tony pointed out a bug.
2003/11/14: I see that both Chris Hester and Tom Gilder have come up with their own solutions (very nice too), but while theirs rely on adding in six empty <div>
s, this technique doesn’t require any additional markup, and that’s what I was aiming for.
2004/03/30: Sergio Villarreal has extended this technique (albeit by using extra markup) in an article on A List Apart. Phil Baines then took Sergio’s code and simplified it. It's worth taking a look at both of their explanations.
Jump up to the start of the post ↑
A collection of miscellaneous links that don't merit a main blog posting, but which are interesting none-the-less.
Our enemies are innovative and resourceful, and so are we. They never stop thinking about new ways to harm our country and our people, and neither do we.— George W Bush (9)
Stuff from the intersection of design, culture and technology.(3)
A selection of blogs I read on a regular basis.