Skip to page content or skip to Accesskey List.
Search evolt.org
evolt.org login: or register

Work

Main Page Content

Simple CSS drop shadows

Rated 3.93 (Ratings: 18) (Add your rating)

Log in to add a comment
(21 comments so far)

Want more?

 
Picture of csaila

Craig Saila

Member info | Full bio

User since: August 26, 1999

Last login: February 21, 2006

Articles written: 6

#boxContent { position: relative; left: -2px; top: -2px; } #boxContainer { position: relative; background: #666; margin: 4px; } //-->

There are a number of ways to create Web-based drop shadows:

Using a couple lines of CSS and a bit of HTML, this article's method for text shadow works in the version-four browsers, and degrades safely in older browsers. With the exception of Netscape 4.x (which displays no shadow), the same is true with the box shadow.

A headline with a drop shadowA headline with a drop shadow

The above drop shadow, created by positioning two identical text blocks, can be used on any block-level text element—keep in mind that shadows make text harder to read.

The headline HTML:

<h4 class="container">
A headline with a drop shadow<font color="#FFFFFF" class="text">A headline with a drop shadow</font>
</h4>

The block-level element carries the class container and two versions of the text. The repeated text is set to the right of the original and wrapped with a FONT coloured the same as the parent's background colour making it "invisible" to non-CSS capable browsers.

Note: Because evolt.org strips out the FONT element, a SPAN was used instead. Here is a working example with the FONT element for viewing.

In CSS-enabled browsers, the repeated text (carrying the class text), is positioned absolutely to rest on top of the other text block.

That first block (carrying the class container) is coloured grey and positioned relatively to create the shadow effect.

The headline style:

.container { position: relative; left: 1px; top: 1px; color: #666; }
.text { position: absolute; left: -1px; top: -1px; color: #000;} 

The container class must be positioned relatively using values opposite the text class: 1px instead of -1px. The negative value lets the text class keep the same left margin as the rest of the content.

While text colour should match the body text, the colour and positioning can be changed to affect the shadow's shade and offset.

Try it for yourself with this JavaScript-based shadow maker

Note: The tool may not work in browsers with poor DOM support like Opera, Internet Explorer 4 and Netscape 4.x.

The box shadow

The following box shadow method can be used on tables, images, or any other non-text, block element. The containing element must be a DIV—or a TABLE if you want it to degrade for older browsers.

A drop shadow around a box

To the left should be a box with a drop shadow created using two styled DIVs with the box class. The general concept is the same as in the text shadow, though.

The box HTML:

<div class="box" id="boxContainer">
 <div class="box" id="boxContent">
 A drop shadow around a box
 </div>
</div>

Because of the way Netscape 4.x handles some aspects of CSS, two style blocks with different media attributes are needed. The style block containing the interior box's background colour should have that attribute set to screen.

The box style:

.box { float: left; width: 100px; height: 100px; }
#boxContent { border: none; background: #9FC; }

All of the box class and boxContent id attributes can be changed except border: none—this property allows Netscape 4.x to display the contained box as a box.

The second block should call the CSS for the box shadow via the @import method or by setting the style's media attribute to all. Newer browsers will display both the box and the shadow—older browsers will only display a shadowless box.

The style:

#boxContent { position: relative; left: -2px; top: -2px; }
#boxContainer { position: relative; background: #666; margin: 4px; }

Like the text shadow, altering the positioning changes the shadow's offset. The margin affects the spacing between the box and the surrounding text.

Each DIV is styled using an id which allows for a number of boxes with different visual styles.

Wrapping up

The two methods outlined in this piece can be tweaked to create other effects such as glows and blurs—just adjust the positioning and colours.

While fairly simple, basic, these methods are safe—unlike other methods. DOM-capable browsers, for example, could be made to do the same thing using JavaScript, but doing so could cause problems in older browsers.

A last comment

To make the shadows forward-compatible, attach the CSS, Level 2 attribute text-shadow to the elements with shadows. This will save time in the future, and shouldn't hurt the pages now.

Craig Saila has been working the Web since 1996, and is currently the lead Web producer with The Globe and Mail’s family of Web sites. Throughout his work, he’s divided his time between client-side development and online journalism — dual interests which are apparent at his site, saila.com.

&lt;font&gt;

Submitted by pixel on December 18, 2001 - 12:42.

I find it quite ironic that you use a font tag with CSS. CSS eliminated the need for the font tag, and in fact, it is deprecated.

thanks for the laugh!

--Eric

login or register to post comments

Font or not

Submitted by csaila on December 18, 2001 - 14:28.

The font is only used so the page can safely degrade to browsers with no CSS support.

Don't care about Netscape 3 (for example) and you want it to validate?

Replace the font with span or some other appropriate inline element, as was done in this article.

login or register to post comments

... that is the question.

Submitted by pixel on December 18, 2001 - 14:34.

good reason. still, thanks for the entertainment! we all got a great laugh at the irony! the whole <font style=""> is just darn entertaining. --Eric

login or register to post comments

Nice.

Submitted by haidary on December 18, 2001 - 22:35.

Well I liked it. I've been toying with the idea of trying to draw with CSS enhanced elements while keeping file size / load time down. I especially enjoyed the example you set up. Do you mind if I download the code? I know I could just do it, but I thought I would ask first. You should package it into an extension for Dreamweaver so other people can enjoy it. If your interested and you don't have any tools to package with, you should check out ' dwfile.com ' . If your not interested, or just plain hate Dreamweaver, then please just ignore me:-)

login or register to post comments

Lynx and text

Submitted by bmason on December 19, 2001 - 01:27.

That technique may be safe in the "CSS-wanting" graphical browsers for a text effect, but repeating text copy to achieve a visual effect is a rotten thing for, say, Lynx users.

login or register to post comments

bad for Lynx

Submitted by pixel on December 19, 2001 - 07:11.

I'd have to agree with bmason.. the effect is wonderful, but when doing it with text, it does become quite useless on many browsers, such as lynx, avantGo, etc. putting a showin on box elements is great, though. it degrades very well.

login or register to post comments

Text browsers and the shadow

Submitted by csaila on December 19, 2001 - 14:16.

Yeah, this is the one drawback of the example used here. That being said, it is possible to avoid. The easiest way is to write the repeated text element (i.e., the one contained by everyone's favourite oxy-moron &#60;font color="#FFFFFF" class="text"&#62;) using:

  • a client-side (i.e., JavaScript) method, or
  • a server-side (e.g., ASP, XSSI) method

If/when Avant Go and the like ever recognize media="handheld", that attribute could be used to turn the class box's display to "none".

Other ideas welcome.

login or register to post comments

Repeating the text goes against all of the design

Submitted by prowsej on December 22, 2001 - 11:23.

Repeating the text goes against all of the design principles of CSS! HTML is about strucutre and not screwing up the code to achieve a smalll graphical effect. My 2 cents: more than half of your audience is going to be using Internet Explorer, anyway. And the IE-specific code is a whole lot cooler and better done. Use the browser-specific method. With non-IE browsers there are more where this will screw up than where this will work.

login or register to post comments

doesn't work in mac/ie5.1

Submitted by fraying on December 23, 2001 - 02:08.

I appreciate how much work has obviously gone into this article, but anyone who chooses to do this should know that it does not work in IE 5.1 on a Mac (just a plain old box), and neither does the shadow maker utility (whole page reloads onclick). Sorry to be the bearer of bad news!

login or register to post comments

IE 5.1?

Submitted by haidary on December 24, 2001 - 18:14.

I know that it is essential to have sites accessable but IE 5.1? I thought they just skipped to IE 5.5 then 6.lol. If the only browser that has problems above 3.0 is IE 5.1 then that's great!

login or register to post comments

Partial fix for Mac/IE 5.x

Submitted by csaila on December 27, 2001 - 11:50.

The Shadow Maker tool should now function properly in IE 5.x for the Mac, but unfortunately, because of the way the box model seems to be handled in that browser the fix for the box shadow is a bit more involved.

Here’s the solution for Mac/IE 5.x: if you do not care about Netscape 6/Mozilla, change the boxContainer position to absolute:

#boxContainer { position: absolute; background: #666; margin: 4px; }

Otherwise, and this is recommended, write the above change via JavaScript after detecting that browser. For example:

<script type="text/javascript"></script>

See the above demonstration in action.

login or register to post comments

The script...

Submitted by csaila on December 27, 2001 - 11:52.

Got stripped, so here is another attempt:

var agt = navigator.userAgent.toLowerCase();
if((agt.indexOf("msie 5") != -1) && (agt.indexOf("mac") != -1)){
 document.writeln("<style type=\"text/css\">#boxContent \{ position: absolute \}</style>")
}

If this doesn't work, please view the source in the linked example.

login or register to post comments

excellent follow-ups!

Submitted by mwarden on December 27, 2001 - 12:32.

csaila, thanks for following up on all this!!!

login or register to post comments

Mac 5.1 followup

Submitted by fraying on December 27, 2001 - 13:27.

Thanks, Craig! And, I misspoke before. I had ie 5.0 going when I posted. I've just upgraded to ie5.1 (for mac os 9.2) and the text shadows now work, and so does the shadow maker tool. There is indeed a drop shadow on the fix you just posted, too, but not on the box in this article.

And, Damon, no, Microsoft might have skipped 5.1 for the PC, but 5.1 for the Mac just came out, and is the latest and greatest.

Thanks for the updates!

login or register to post comments

Media types

Submitted by detverdner on January 3, 2002 - 02:53.

I like this article, because it shows how CSS can be used. But I think there's a problem in the repetition of content to reach a desired formatting. A solution might be to add styles for other media types; for example to add:

@media aural {
.text {speech: none;}
}

...or something similar. I know it's not backward compatible, but I think it's better than doing nothing for non-screen users. Just my 2 cents...

login or register to post comments

Oh...oops.

Submitted by haidary on January 6, 2002 - 00:26.

Fraying, Sorry about that. It was partly meant as a joke. I havn't used a mac in a while. I was pretty sure I had used IE 5.5 on the mac but it turns out I'm wrong. I think I got it confused with 4.5.

login or register to post comments

Harsh

Submitted by dtagger on January 10, 2002 - 11:47.

Whoa! Just poped in, you guys are harsh.

Take me back to the Server Side, aah standards.

Remember - "The best flavour for vitual ice cream is vanilla"

login or register to post comments

Full page of text?

Submitted by astral on January 3, 2003 - 14:29.

I've been trying to use CSS text shadows for a little while now, and for the life of me I can't come up with a way to get consistent a consistently displayed shadow on a page of text. I tend to get separated text, where suddenly the shadow underlying the text starts displaying away from it's master text, and then it suddenly snaps back in to place after a br. I've been using a variation of this article (with some from one I found on another site), but everything I find seems to be for headlines only. I like the idea of the redundant font tag, since it lets me insure backwards compatibility, but at this point I'd be happy with anything. Any ideas?

login or register to post comments

Doing a full page of text

Submitted by csaila on January 3, 2003 - 18:42.

You do the same thing as the headings, except with paragraphs. For each paragraph of text apply the "container" class, and within each paragraph duplicate that text at the end in a font or span with the "text" class. Keep in mind, drop shadows on large blocks of text can decrease readability. HTH.

login or register to post comments

SVG for drop shadows?

Submitted by haidary on April 30, 2003 - 07:08.

Hey, Thought I'd bump this back up by asking you guys what your thoughts are on using SVG for drop shadows? Browser support for SVG is getting better and there's plug-ins for mobile devices now. It's not as well-supported as this way but I'd say there's moderate support for it. It also gives you greater control over your shadow. Even better control than the IE filter if my memory serves me correctly. Since you embed the SVG doc with an object element, you can specify alternate content for browsers that don't support SVG. Anyway, I thought it was a good idea, just thought I'd ask what you guys think since I could very well be wrong.

login or register to post comments

play smack-the-newbie

Submitted by Crystaviel on August 11, 2004 - 14:50.

...or, just clue me in as to how I might best solve my problem. I'm trying to figure out how to use this feature to create white text that's completely ringed with black, instead of just the one drop shadow on the lower right side... and I just ain't figurin' it out. Can anyone help me out? Drop me a note at cristaviel@aol.com, willya?

login or register to post comments

The access keys for this page are: ALT (Control on a Mac) plus:

evolt.orgEvolt.org is an all-volunteer resource for web developers made up of a discussion list, a browser archive, and member-submitted articles. This article is the property of its author, please do not redistribute or use elsewhere without checking with the author.