A Quick Note on Text Shadows

I was recently doing some work for a client and they had a menu that would appear over a background image. The background image would be different for each page. Unfortunately, the background image on some pages made the menu text difficult to read.

The designer thought it would be a good idea to have a drop shadow behind the text. This sounded good to me, except that I didn't know how to do that. Fortunately, it is really easy.

First, let's look at the effect:

As it turns out, there is a CSS declaration just for this called "text-shadow". This was originally added in CSS2 (and removed in CSS 2.1) and now part of the text module in CSS3.

Here is the CSS code used for this effect:

text-shadow: 2px 2px 1px black;

The first number of the x-offset. That is, the distance the shadow is moved to the right of the original text. The second number is the y-offset. That is, the distance the shadow is moved down from the original text. Negative numbers are allowed for both of these.

The last number is the blur radius. This is basically how much the shadow should be "smeared" out. This can quickly result in the text being illegible or even effectively invisible, so low values are typically used.

The color value is, of course, the color of the text shadow.

What about IE?

It practically goes without saying that this isn't supported in IE. Fortunately, IE has actually long had its own way to do this that doesn't get in the way of any other browser. That is by using a shadow filter. Filters in IE are actually pretty nifty. As they are exclusive to IE, however, I have never spent much time or energy learning them.

Here is the filter code that I used in this example:

filter: Shadow(Color=#000000,Direction=135,Strength=3);

Here the color value works the same and "Direction" indicates in what direction the shadow should move from the original text (I assume this is by degrees out of 360 from the top working around clockwise). The "Strength" is the distance it should be moved. The shadow filter, however, doesn't have a way to "blur" the shadow.

Another option for IE is the "DropShadow" filter:

filter: DropShadow(offx=2, offy=2, color=#000000);

Other Nifty Effects

The text-shadow declaration can actually be used to create multiple layers of shadows, for neat effect. See the W3C Text Shadow examples for more. Line25.com also has a good article on Using CSS Text-Shadow to Create Cool Text Effects.

Not all of these can be duplicated with IE filters, of course, but using text-shadow in combination with IE filters can provide more text styling options than you may have thought possible. It certainly allowed more flexibility than I had thougt possible.

Comments
Steve, good note on the text-shadow declaration. Note that you might also want to add vendor prefixes for some older browsers that don't support the non-prefixed declaration:
-moz-text-shadow: 2px 2px 1px black;
-webkit-text-shadow: 2px 2px 1px black;
text-shadow: 2px 2px 1px black;
# Posted By Tom Mollerus | 10/26/11 12:19 PM
Tom,

Great point!

It actually worked on every browser I tests on, but I can't remember how far back I went on each version. Incidentally, Adobe's BrowserLab is really nice for this sort of thing
# Posted By Steve Bryant | 10/27/11 9:43 AM
useless style content
# Posted By javed | 11/2/11 4:45 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.