StylizedWeb is a web design trends and tutorials blog, maintained by Ross Johnson who also runs a web design company and design blog.

Categories Archive

Popular Posts
Advertisement
Basecamp The Web Design Sketchbook
Najbolje ponuda IT poslova u Srbiji na itposlovi.info Advertise on Stylized Web

Outline text with JS

add to Save to Delicious Save to Stumble Save to Digg
The Web Design Sketchbook

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



Talk of the town
  • Section09: Thanks so much for this! I was wondering if there is a way to limit the amount of posts that are viewable?...
  • KUYUMCU KARAGÖZ: What is saying.Really it ripe nice.Yours respectfully.
  • Mike: The golden section is a really interesting and highly applicable bit of maths. I’m sure there are many...
  • CharlieM: I think they really went beyond on this article. Keep up the great work!
  • Dan: Thank you, thank you, thank you!
Amigos

What do you think?





8 Responses so far

By dusakabin
on April 9, 2008

Lovely post. I like your pencraft and that

By brem
on April 6, 2008

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

By Dejan
on March 26, 2008

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 ;)

By anthony ryan delorie
on March 26, 2008

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.

By An Jay
on January 17, 2008

Great article. Thanks for sharing it

By Dejan
on January 14, 2008

@ 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.

By Ty (tzmedia)
on January 14, 2008

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?

  1. Jan 21, 2008: Aplicale borde y sombra a tus textos usando JS | frogx.three