Glad You're Ready. Let's Get Started!

Let us know how we can contact you.

Thank you!

We'll respond shortly.

Bulletproof font-face syntax with SASS and Rails

The Bulletproof font-face syntax effectively gives you broad browser support for your fancy (or austere) web fonts. However, using the Bulletproof font-face syntax with SASS carries a couple of gotchas. Reference this article when you are starting a new project and setting up the fonts. This technique implies you are Rails 3.2.3 (or greater) because it uses the font-path asset helper. If you are not using Rails, see the note at the bottom.

TL;DR A handy mixin

Step 1: Use this:

// mixin for bullet proof font declaration syntax
@mixin declare-font-face($font-family, $font-filename, $font-weight : normal, $font-style :normal, $font-stretch : normal) {
@font-face {
font-family: '#{$font-family}';
src: url(font-path('#{$font-filename}.eot'));
src: url(font-path('#{$font-filename}.eot?#iefix')) format('embedded-opentype'),
url(font-path('#{$font-filename}.woff')) format('woff'),
url(font-path('#{$font-filename}.ttf')) format('truetype'),
url(font-path('#{$font-filename}.svg##{$font-family}')) format('svg');
font-weight: $font-weight;
font-style: $font-style;
font-stretch: $font-stretch;

Step 2: Write one line for each of your fonts you include:

@include declare-font-face('Gill Sans', 'Gill-Sans-MT-Pro-Light', 200);
@include declare-font-face('Gill Sans', 'Gill-Sans-MT-Pro-Italic', 400, italic);

Step 3: Party

The Tricks of Bulletproof font-face syntax with SASS

It uses SCSS syntax, because SCSS can do multi-line and SASS can’t. It’s fine if you use SASS syntax, just name this file .scss and @import it just the same.

The mixin has defaults for last three parameters (denoted by the colons in the method signature). This way, you don’t have to go out of your way to specify normal each time.

It uses the font-path inside of the url(…). That is because I have seen lots of bugs with complex file names for the font files, and the quotes guarantee the file will be found. Either rename all your fonts with no dashes or spaces or other weird characters, or use this trick.

This isn’t really a trick, but it is more convienent to put all font variants inside the same font-family. It just makes it easier to have everything as “Gill Sans” and change the font-weight: italic, rather than having “Gill Sans” and “Gill Sans Italic” and however other many variants you have.

The original trick: Originally developed by Paul Irish (Bulletproof font-face syntax) and more recently by Fontspring (The New Bulletproof syntax), the syntax gives you broad browser support for your web fonts. IE8 loads the first .eot file and is done, but it “tricks” IE9 by relying on a bug in the parser. It dies on the question mark in the second line, and doesn’t continue on to the other file formats (which don’t work in IE9, and would cause it to not display). The other formats are handled by Safari, Chrome and Firefox, and carry no such trickery.

Not using Rails?

If you are not using rails, the “font-path” mixin will not be available to you. It’s ok! The mixin is still handy. Just replace all the uses of “font-path” with the correct path to your assets. For example, if your assets reside in the “public” folder, then change “font-path(…)” to “/public/…”.

Thank You

And be sure to follow me on twitter for new fancy posts!

  1. DougThwaites says:

    Awesome man, cant wait to try it!, Thanks

  2. Vic says:

    What font-path function does? How do I specify the font path?

  3. Ward Penney says:

    Hi Vic,

    The font-path function is provided by Rails Asset Pipeline. My apologies, I should make it more clear that this is only for work with rails.

    For your codebase, you should just replace that font-path call with something that points to where your font files are in your codebase.


  4. aza says:

    I tried several tutorials but this one really works and not in Rails.
    Thank you! :)

  5. Awesome mixin, Thank u

Post a Comment

Your Information (Name required. Email address will not be displayed with comment.)

* Copy This Password *

* Type Or Paste Password Here *