stylish text with text-shadow

This post is from Frederic Wenzel, who works on Mozilla’s Web Development team.

The text-shadow CSS property does what the name implies: It lets you create a slightly blurred, slightly moved copy of text, which ends up looking somewhat like a real-world shadow.

The text-shadow property was first introduced in CSS2, but as it was improperly defined at the time, its support was dropped again in CSS2.1. The feature was re-introduced with CSS3 and has now made it into Firefox 3.5.

How it Works

According to the CSS3 specification, the text-shadow property can have the following values:

none | [, ] * ,

<shadow> is defined as:

[ ?   ? |   ? ? ], 

where the first two lengths represent the horizontal and vertical offset and the third an optional blur radius. The best way to describe it is with examples.

We can make a simple shadow like this, for example:

text-shadow: 2px 2px 3px #000;
A simple shadow

(All of the examples are a live example first, then a picture of the working feature — so you can compare your browser’s behavior with the one of Firefox 3.5 on OSX)

If you are a fan of hard edges, you can just refrain from using a blur radius altogether:

text-shadow: 2px 2px 0 #888;
I don’t like blurs

Glowing text, and multiple shadows

But due to the flexibility of the feature, the fun does not stop here. By varying the text offset, blur radius, and of course the color, you can achieve various effects, a mysterious glow for example:

 text-shadow: 1px 1px 5px #fff; 
Glowing text

or a simple, fuzzy blur:

text-shadow: 0px 0px 5px #000;
Blurry text

Finally, you can add ”more than one shadow”, allowing you to create pretty “hot” effects (courtesy of http://www.css3.info/preview/text-shadow/ css3.info):

text-shadow: 0 0 4px white, 0 -5px 4px #FFFF33, 2px -10px 6px #FFDD33, -2px -15px 11px #FF8800, 2px -25px 18px #FF2200
Multiple shadows are hot

The number of text-shadows you can apply at the same time in Firefox 3.5 is — in theory — unlimited, though you may want to stick with a reasonable amount. Like all CSS properties, you can modify text-shadow on the fly using JavaScript:

Animated shadows with JavaScript


Start/stop animation

Performance, Accessibility and Cross-Browser Compatibility

The times of using pictures (or even worse, Flash) for text shadows on the web are numbered for two reasons:

First, there are significant advantages to using text instead of pictures. Not using pictures saves on bandwidth and HTTP connection overhead. Accessibility, both for people who use screen readers and search engines, is greatly improved. And page zoom will work better because the text can be scaled instead of using pixel interpolation to scale up an image.

Second this feature is largely cross-browser compatible:

  • Opera supports text-shadow since version 9.5. According to the Mozilla Developer Center, Opera 9.x supports up to 6 shadows on the same element.
  • Safari has had the feature since version 1.1 (and other WebKit-based browsers along with it).
  • Internet Explorer does not support the text-shadow property, but the feature degrades gracefully to regular text. In addition, if you want to emulate some of the text-shadow functionality in MSIE, you can use Microsoft’s proprietary ”Shadow” and ”DropShadow” filters.
  • Similarly to MSIE, when other, older browsers do not support the feature (including Firefox 3 and older), they will just show the regular text without any shadows.

A caveat worth mentioning is the ”drawing order”: While Opera 9.x adheres to the CSS2 painting order (i.e., the first specified shadow is drawn at the bottom), Firefox 3.5 adheres to the CSS3 painting order (the first specified shadow is on top). Keep this in mind when drawing multiple shadows.

Conclusions

text-shadow is a subtle but powerful CSS feature that is — now that it is supported by Firefox 3.5 — likely to be widely adopted across the web in the foreseeable future. Due to its graceful degradation in older browsers, it can safely be used by developers and will, over time, be seen by more and more users.

Finally, some words of wisdom: Like any eye candy, use it like salt in a soup — with moderation, not by the bucket. If the web developers of the world overdo it, text-shadow may die a short, yet painful death. It would be sad if we make users flinch at the sight of text shadows like typography geeks at the sight of “Papyrus”, and thus needed to bury the feature deeply in our treasure chest.

That being said: Go try it out!

Further resources

Documentation

Examples

About Christopher Blizzard

Making the web better, one release at a time.

More articles by Christopher Blizzard…


24 comments

  1. […] My Web dev friends will love this: stylish text with text-shadow: http://hacks.mozilla.org/2009/06/text-shadow/ […]

    June 10th, 2009 at 06:43

  2. […] http://hacks.mozilla.org/2009/06/text-shadow/ via @nitot […]

    June 10th, 2009 at 06:52

  3. […] text with the CSS text-shadow property This is a cross-post of an article I wrote for the hacks.mozilla.org blog. It shows off some of the fun stuff web developers can do […]

    June 10th, 2009 at 07:26

  4. […] 原文地址:stylish text with text-shadow […]

    June 10th, 2009 at 08:00

  5. Francisco Costa

    Great trick!

    June 10th, 2009 at 10:00

  6. Shahbaz Javeed

    Looks like the latest Opera 10 beta uses the CSS3 shadow rendering order. The flaming text looks similar to the reference rendering.

    June 10th, 2009 at 11:23

  7. Garbled

    Oh god, you just had to mention Papyrus!

    June 10th, 2009 at 12:06

  8. Frederic Wenzel

    Shahbaz: Thanks for the heads-up! I am glad to hear that. It’ll make developers’ lives much easier!

    Garbled: Oh yes, you know I had to :)

    June 11th, 2009 at 02:12

  9. Neil Rashbrook

    For me the glowing text effect wasn’t very obvious for some reason…

    June 11th, 2009 at 03:12

  10. […] stylish text with text-shadow […]

    June 11th, 2009 at 05:55

  11. […] stylish text with text-shadow […]

    June 11th, 2009 at 07:44

  12. John allsopp

    For folks who’d like to play with text shadow, and other cool CSS3 stuff supported in Firefox 3.5, try

    http://westciv.com/tools/shadows/
    http://westciv.com/tools/boxshadows/
    http://westciv.com/tools/transforms/

    john

    June 11th, 2009 at 15:14

  13. Frederic Wenzel

    John: Thanks for posting these links, being able to see the effect change in real time, that’s great!

    June 12th, 2009 at 01:44

  14. […] respectively. The third length is the blur radius (compare that to the blur radius in in the text-shadow property). Finally the fourth length is the spread radius, allowing the shadow to grow (positive values) or […]

    June 12th, 2009 at 05:00

  15. Florian

    The statement that this degrades gracefully is not quite true, some of the examples look quite horrible in opera mini, which seems not to implement blur radius (and of course the absolute pixel values are far too large for tiny cellphone fonts).

    June 12th, 2009 at 12:01

  16. […] Johnson has put together another fun demo. He’s using some JavaScript and the new text-shadow property to build a spotlight effect. It’s embedded below. If you can’t view it, click […]

    June 25th, 2009 at 21:55

  17. […] And for those interested in all the things that will be possible on the web sometime in the future. You’ll need Firefox 3.5 (some might work on Safari 4 too) Shadows: http://hacks.mozilla.org/2009/06/text-shadow/ […]

    June 26th, 2009 at 00:37

  18. […] http://hacks.mozilla.org/2009/06/text-shadow/ […]

    July 1st, 2009 at 03:01

  19. […] Johnson has put together another fun demo. He’s using some JavaScript and the new text-shadow property to build a spotlight effect. It’s embedded below. If you can’t view it, click through […]

    July 1st, 2009 at 11:35

  20. […] 原文地址:stylish text with text-shadow […]

    July 8th, 2009 at 22:25

  21. […] stylish text with text-shadow […]

    December 4th, 2009 at 09:40

  22. CSS Design Blog

    Text-Shadow is one of the most interesting properties if you want to realize Text like all the apple pages do.

    December 10th, 2010 at 02:09

  23. […] de las que he mencionado, también destacaría a text-shadow y […]

    January 25th, 2011 at 15:53

  24. zavera

    Great tricks with text-shadow, i think css3 is making things more easier for designers and the MSIE 9 and all other lattest browsers are fully supporting these text effects!

    January 12th, 2013 at 00:33

Comments are closed for this article.