R E C E N T     E N T R I E S

POSTED: Wednesday, April 01, 2009

CUFON Styles : font color, line-height and sizing

After our brief post regarding font replacement using CUFON we have been innundated with requests on how to manipulate the look and feel of the replacement fonts.

Examples:

This font is placed as a <cite> element

This is placed as a <pre> element

First, a quick recap.  Using a generator script located here you may upload your .ttf or .otf font (if you don't own the license to the font you shouldn't upload it).  The generator saves the result as an SVG font and then converts the paths to VML which is supported natively in IE browsers. The resulting document is converted into JSON and functional javascript. 

So, to include a font in a web page you need only to save the generated .js script, upload it to your site, and then load it with the usual <script> tag like any other javascript.  The external javascript blocks execution until the files have loaded which prevents flickering on font replacement and the compressed files are easily 60% to 80% smaller in byte size than the original.

To implement you simply upload the two javascript files (one for the font replacement and one the standard cufon-yui.js file) and reference them like this:

<script src="cufon-yui.js" type="text/javascript"></script>
<script src="myfont.js" type="text/javascript"></script>

Easy enough and styling is even easier.  First, in order to replace your existing font with the new font you must wrap the text in a CSS element like <h6> or even <p> if you want to replace all of your paragraph text with the new font.

<h6>This font is placed as an h6 header</h6>
<pre>This is placed as a <pre> element</pre>

Now use a simple javascript call to replace the existing font with the new font.

<script type="text/javascript">
Cufon.replace('h6');
</script>

<script type="text/javascript">
Cufon.replace('pre');
</script>

You can even add styles to the javascript like this:

<script type="text/javascript">
Cufon.replace('h6', {
textShadow: '#333 1px 1px, #fff -1px -1px'
});
</script>

Now, how to set font size, color, line-height, margins and padding as promised in the article title?  Use simple CSS!

<style type="text/css">
h6 {
background: #e5e5e5;
color: #FC9;
font-size: 200%;
line-height: 1.2em;
margin: 1em 0;
padding: 10px;
}
pre {
background: #fff;
color: #880000;
font-size: 150%;
line-height: 1.2em;
margin: 1em 0;
padding: 10px;
}
</style>

That's all there is to it!
Happy Coding!

Comments

1. How about smaller line-heights? I've tried lowering the space between lines on a cufon enabled list but can't get them close enough to match the design.

Thanks.
by madastro
2. You can try setting the line height smaller but if you are in a list then the CSS must apply to the < li > element. Is that what you are trying to do?
by TexasWebDevelopers

3. thanks, i tried using line-height on the < li > but it doesn't work, used the height property instead.
by madastro
4. Hey there, thanks for the tips.

One more thing for those of us who are used to editing CSS live with tools like Firefox Web Developer toolbar - changes you make to Cufon styles won't be reflected until the next page refresh!

Looking back it makes sense, but it can be puzzling when live CSS fixes you're used to seeing immediately don't take effect right away.
by APeters
5. Line-height won't work without a strict doctype in browsers other than IE 6 or 7
by mark
6. Hi.. Before this i was using SIFR for the font replacement to enhance websites. but after reading your post i will definitely try this.
by neel
7. mark - thanks for that.
by Mike
8. I set the line height by giving the class 'cufon' a margin top and bottom:
span.cufon {margin:5px 0;}
by Joern
9. It's really kludgey, but you can edit the font file itself to adjust the line height if you want the line height to be less than 1.2em - find the attribute "ascent" in the .js file and start lowering its value.
by Andrew

10. Thanks for that Andrew. This works OK apart from in IE7. Has anyone found a workaround for this?

Thanks Matt
by Matt

11. thank you
by şarkı dinle
12. stop replacing fonts with javascript, retards.. use WOFF or similar webfont standard!
by Dennis

13. Regarding line height issue, try changing the doc type of the file to strict.dtd

">"http://www.w3.org/TR/html4/strict.dtd">

Hope this helps.
by Veena

14. thanks
by waqar




Name
URL
Email
Email address is not published
Remember Me
Comments

CAPTCHA
Write the characters in the image above