StylizedWeb

Subscribe to our updates:

A Design, WordPress and Tutorials Blog.

Dedicated to helping you learn the art and science of the web.

Outline text with JS

During my work as frontend coder I had a task to place some white text on dynamic background image and the problems would accrue if background image was white so text wouldn’t be readable. This isn’t really every day requirement but it can be very useful. The solution I’m using is outline text JS, originally written by Anthony Ryan Delorie.

You can use this JavaScript in two ways, to outline text and to add shadow to text. It works in all major browsers. Click here for demo.

Outline

Outline
 
<script>
<!--
 
var message="outline";
var thickness=1;
var color2="black";
var color1="white";
var extra="font-size:40px;";
var x = -thickness;
var y = -thickness;
 
while (y<=thickness) {
while (x<=thickness) {
 
document.write ("<span style='"+extra+" color:"+color2+"; position:absolute; left:"+x+"; top:"+(-y)+";'>"+message+"</span>");
document.write ("<span style='"+extra+" color:"+color2+"; position:absolute; left:"+x+"; top:0;'>"+message+"</span>");
document.write ("<span style='"+extra+" color:"+color2+"; position:absolute; left:"+x+"; top:"+y+";'>"+message+"</span>");
 
x=x+1;
}
y=y+1;
}
 
document.write ("<span style='"+extra+" color:"+color1+"; position:absolute; left:0; top:0;'>"+message+"</span>");
 
//-->
</script>
 
Shadow
 
<script>
<!--
 
var message="Shadow";
var range=2;
var extra="font-size:40px;";
var color1="gray";
var color2="black";
var x = range;
var y = range;
 
document.write ("<span style='"+extra+" color:"+color2+"; position:absolute\; left:"+x+"; top:"+y+";'>"+message+"</span>");
document.write ("<span style='"+extra+" color:"+color1+"; position:absolute; left:0; top:0;'>"+message+"</span>");
 
//-->
</script>
 
Both
 
<script>
<!--
 
var message="Both";
var thickness=1;
var color2="black";
var color1="white";
var color3="gray";
var extra="font-size:40px;";
var range=5;
var z = range;
var d = range;
var x = -thickness;
var y = -thickness;
 
while (y<=thickness) {
while (x<=thickness) {
 
document.write ("<span style='"+extra+" color:"+color2+"; position:absolute; left:"+x+"; top:"+(-y)+";'>"+message+"</span>");
document.write ("<span style='"+extra+" color:"+color2+"; position:absolute; left:"+x+"; top:0;'>"+message+"</span>");
document.write ("<span style='"+extra+" color:"+color2+"; position:absolute; left:"+x+"; top:"+y+";'>"+message+"</span>");
 
x=x+1;
}
y=y+1;
}
 
document.write ("<span style='"+extra+" color:"+color3+"; position:absolute\; left:"+z+"; top:"+d+";'>"+message+"</span>");
document.write ("<span style='"+extra+" color:"+color1+"; position:absolute; left:0; top:0;'>"+message+"</span>");
 
//-->
</script>
 

NOTE: It’s not working when doctype -> XHTML 1.0 is included

Leave a comment on Stylized Web Have some feedback? Leave a comment

10 Comments So Far

  1. By Ty (tzmedia) posted on January 14, 2008 at 3:39 pm

    that’s pretty cool, I commonly use photoshop layers to produce graphics, either stroke or a heavy outer glow. Too many times letters don’t stand out on a various color background image.

    Q: Can both effects be applied at the same time?
    Like black letter outline, grey drop shadow?
    thanks.
    you credit someone else with the technique, are there any links of more demo’s from them?

  2. By Dejan posted on January 14, 2008 at 4:13 pm

    @ Ty (tzmedia)
    Good question, yes it is, I’ve written code for it and updated demo and source.
    About the original author, no i don’t know if there are some other examples, and if there are they should match my b/c code is pretty much the same.

  3. Great article. Thanks for sharing it

  4. Pingback: Aplicale borde y sombra a tus textos usando JS | frogx.three

  5. By anthony ryan delorie posted on March 26, 2008 at 4:53 pm

    wow. googled my name and found this blog.

    thanks for the credit, i wrote that back in school. i wish i had shared more and gotten more into open sourcing my stuff and benefited more by getting my name out there. glad to see in 2008 people still getting use of a script i thought was cool at the time i wrote it.

    I’d plug a blog, but i don’t maintain one frequently enough.

    also glad to see the emergence of javascript over recent years.

  6. By Dejan posted on March 26, 2008 at 5:03 pm

    Hi Anthony, thanks for visiting. I have used your code many times and i think that’s the only solution for this right now.
    I’ve tried to find your home pages but i guess there is no such so i just gave your name

    If you are are interested in sharing some of the rest of your code feel free to contact me ;)

  7. Sir, yes sir! I’m agreeing with you, but I don’t think everybody do. You should not be so rude, it frightens of.

  8. Lovely post. I like your pencraft and that

  9. By ying posted on August 29, 2011 at 10:49 am
  10. js
    good servise

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>