Drop shadows without graphics
by ZetaGecko | 8 Comments | CSS
Drop shadows can be a useful design element for adding depth and variety to your webpage designs, but tranditional techniques for adding drop shadows have required adding images to your pages, resulting in some slowing of download times. Here's a technique for creating drop shadows using CSS which does not require images, and which allows for dynamically sizing based on the length of your content.
First, here's the output we're going to create:
And here's the code:
<div style="position:relative; top:7px; left:7px; width:170px; background:#bdf;">
<div style="position:relative; top:-7px; left:-7px; width:170px; background:#9cf;">
<div style="border:1px dotted #69c; padding:5px;">
This box has a fixed width of 170 pixels, but its length is determined by the amount of content in it.
</div>
</div>
</div>
The first div is the drop show. It uses relative positioning to move 7 pixels down and to the left. The second sits on top of the drop shadow, and uses negative relative positioning to move back to the original location within the flow of the page. Notice how the drop shadow div automatically grows to the same length as the content div, causing it to extend 7 pixels below the content div. That's the main thrust of this example.
But then there's the question of the innermost div. Why do we use another div to add padding and a border instead of simply putting those settings in the content div? In a perfect world, we wouldn't have to, but in the real world, different browsers interpret CSS a little differently. In some, the padding and border widths are counted as part of the width of the div--as padding and border widths increase, the width of the content area decreases. In others, as the padding and border widths increase, the overall width of the div increases. Using a separate div enables us to ensure that the overall width of the div is exactly what we want it to be in all browsers.
June 1st, 2005 at 8:41 pm
i need some help with this.
i tried to validate using w3c and i receive an error having to do with
Line 32, column 21: document type does not allow element here; missing one background: #a1a1a1;">
The mentioned element is not allowed to appear in the context in which you've placed it; the other mentioned elements are the only ones that are both allowed there and can contain the element mentioned. This might mean that you need a containing element, or possibly that you've forgotten to close a previous element.
One possible cause for this message is that you have attempted to put a block-level element (such as >") inside an inline element (such as your comment won't let me leave tags)
am I miss something?
and it would be nice if I could put this in a css file.
site is http://www.lockinventor.com
June 1st, 2005 at 8:57 pm
I believe the problem is that h3 is an inline element, so it can't contain a div.
June 1st, 2005 at 10:26 pm
thanks. You are correct. And I like shadow effect very much.
jay
June 4th, 2005 at 11:30 am
Could I get away with another question about this shadow effect.
if you go to http://www.lockinventor.com having a css sheet css.css and check out height of top banner and then go to http://www.lockinventor.com/cabinet.htm I can't make that height same as top banner from lockinventor.com. page controlled by css.css are less in height than those controled by tinacss.css
thanks.
jay
June 4th, 2005 at 6:30 pm
well since above I went and increased index.com when I was trying to reduce it. strange computer and miscelleanous are smaller in height.
June 4th, 2005 at 7:56 pm
never mind.
I fixed it. I put in a line-height: 0; for h1 into css and appears to have reduced border.
and I was able to give a shadow to an image
July 25th, 2005 at 12:16 pm
This is very nice..bro..
October 8th, 2005 at 11:43 pm
This is pretty cool!