Using Google Fonts Locally

» 03 February 2011 » In Guides, Web Design »

Google Fonts, or more popularly known as Google Web Fonts, provides a great collection of open source licensed (can be used commercially and non-commercially) true-type fonts. My favorite is Droid Sans, the font I’m currently using now sitewide.

Because I love Droid Sans, I want to use it everywhere. Even in my school projects. I just finished a project (that is due tomorrow) that demonstrates HTML frames. And I wan’t (of course) to use Droid Sans as the main font for the web page. And if I used the font as I regularly do:

<link href='http://fonts.googleapis.com/css?family=Droid+Sans:regular&amp;subset=latin' rel='stylesheet'> 

The font won’t be viewable offline. So the solution for this, is to download the font and use the CSS Google recommends. Take a look here.

@media screen {
@font-face {
  font-family: 'Droid Sans';
  font-style: normal;
  font-weight: normal;
  src: local('Droid Sans'), local('DroidSans'), url('http://themes.googleusercontent.com/font?kit=s-BiyweUPV0v-yRb-cjciC3USBnSvpkopQaUR-2r7iU') format('truetype');
}
}

But how do we download the font? That’s easy look at the url('...'):

src: local('Droid Sans'), local('DroidSans'), url('http://themes.googleusercontent.com/font?kit=s-BiyweUPV0v-yRb-cjciC3USBnSvpkopQaUR-2r7iU') format('truetype');

Just enter that URL to your browser, and viola!

Now that we got our font, we use the CSS, and we modify it a little bit:

@font-face {
  font-family: 'Droid Sans';
  font-style: normal;
  font-weight: normal;
  src: local('Droid Sans'), local('DroidSans'), url('path/to/DroidSans.ttf') format('truetype');
}

So this works for the latest stable versions of Chrome, Firefox, Opera, Safari, and.. Wait.. It’s not working in IE8. This is because IE lte 8 only accepts EOT type fonts. So how do we fix this? I’m not an IE user, but a big part of the Internet population uses IE, so we do not have a choice.

Good news is, you can convert TTF fonts to EOT. After that, save the file on the same directory where our TTF font is, and add it in the CSS.

@font-face {
  font-family: 'Droid Sans';
  font-style: normal;
  font-weight: normal;
  src: url('path/to/DroidSans.eot');
  src: local('Droid Sans'), local('DroidSans'), url('path/to/DroidSans.ttf') format('truetype');
}

Now, you have to remember that you have to place this on the top of your CSS document. Then you can just use this as a regular font in CSS:

body {
	font-family: 'Droid Sans', Arial, sans;
	color: #666;
}

Make sure, that the path to the font is always relevant to the CSS file. And that’s all for now. :)

Tags: , , , , ,

Trackback URL

  • Haneef

    Very helpful post; thank you for sharing your knowledge. I’m working on a project that will be deployed locally, and the clients who access it don’t have access to the Internet. I’ll be trying this out on a project later this week.

  • http://www.facebook.com/profile.php?id=1447256006 Vasco Costa

    Awesome post. Many thanks.

  • http://www.sachagreif.com/ Sacha Greif

    If this is just about using fonts locally, why not just download the font and install it as a system-wide font?

    • henrikcederblad

      I think that approach lowers the portability; if you want to instantiate the project to other computers (especially if owned by different people), it might be too much to ask for the font to be downoaded and installed system-wide on each machine.

  • http://twitter.com/sandragornall Sandra G

    Thank you, this was very helpful! (Just started playing with Google web fonts – so much fun!)

  • Bigicebear

    RE: Sacha Greif:
    “If this is just about using fonts locally, why not just download the font and install it as a system-wide font?”

    The author is referring to using a web page locally – or Off Line if you will – when a web connection is no longer available

  • http://salvatorecapolupo.it/ Salvatore Capolupo

    Thank you,  this solution is very useful when you got a lot of external HTTP calls for improving site speed.

  • Ngala

    src: local(‘Droid Sans’), local(‘DroidSans’)Is there a reason why this is repeated twice ?where the 1st one is spaced between.

  • http://twitter.com/CumpsD David Cumps

    The google api serves up a different response depending on the browser you request with, so just copy pasting the code you got when asking it with Chrome, will not be perfect, since you’d have to implement the browser detection logic as well and serve different local versions.

  • Dan Peltier

    This is an EXCELLENT tutorial….very helpful for providing local, internet-less devices with beautiful fonts!

  • sanjay

    super post :)

  • http://www.justinmccandless.com/ Justin McCandless

    Thanks for the post, I’m in China and online access to Google webfonts is too sketchy to rely on.

  • Jeff

    Just what I’ve been looking for!! Thank you!

  • r7 un

    what do you do on a mac, if the fonts are in .woff format?

    • http://ruel.me Ruel

      I’m afraid, I have no idea yet. But since my new workstation is on a mac, I might be able to figure out some time later. :)