Google Fonts tutorial
Google came with a new idea in their list of web toys. A font directory that allows you as a web developer to include some “fancy” fonts in your pages without struggle.
What is Google Fonts Directory all about?
All the fonts in the Google Font Directory are licensed under open source licenses that let you use them on any website, from a private blog to a big commercial site. The idea seems to be good and comes to complete the Google AJAX Libraries API.
For now there are only a few fonts available but in time I think the list will grow.
There are two ways to use this on your site
The first one is simple and only requires a css file retrieved from Google’s servers like below:
<link rel="stylesheet" type="text/css"
href="http://fonts.googleapis.com/css?family=Tangerine">
<style>
body {
font-family: 'Tangerine';
font-size: 48px;
}
</style> |
You need to specify the family of the font and if it contains spaces you need to replace this with a “+” (plus character) sign
<link rel="stylesheet" type="text/css"
href="http://fonts.googleapis.com/css?family=Droid+Sans"> |
You can load multiple fonts in a single call. This will decrease the loading time because you use only one http request. The multiple fonts will be separated by a “|” ( pipe character ) sign.
<link rel="stylesheet" type="text/css"
href="http://fonts.googleapis.com/css?family=Tangerine|Inconsolata|Droid+Sans"> |
Normaly the fonts are loaded using the regular version but you have more control over the loaded font using some selectors for the style and weight, using a “:” ( colon character ) sign, and the values if more than one separated by a “,” ( comma )
<link rel="stylesheet" type="text/css"
href="http://fonts.googleapis.com/css?family=Tangerine:bold,bolditalic"> |
The style and weight of the font can be specified using a fullname, an abbreviation or a numeric value ( for weight )
bold:b bold:700 italic:i bolditalic:bi |
<link rel="stylesheet" type="text/css"
href="http://fonts.googleapis.com/css?family=Cantarell:i|Droid+Serif:700"> |
Take into account that not all the fonts have the styles you might need so first of all verify in the font directory if it does.
The second option
The second option to load the fonts is to use the webloader (Javascript) that Google offer for this. This script can be called directly ( which is faster) or using their AJAX API functions. The main advantage of this method is that you can load fonts provided by TypeKit and Google or from any other font provider at the same time from the same library.
WebFont.load({ google: { families: [ 'Tangerine', 'Cantarell' ] }, typekit: 'myKitId', custom: { families: ['OneFont', 'AnotherFont'], urls: [ 'http://myotherwebfontprovider.com/stylesheet1.css', 'http://yetanotherwebfontprovider.com/stylesheet2.css' ] } }); |
The disadvantage of this method is the increased loading time comparing to the simple method.
Other things to consider
Loading external fonts is slow, at least the first time, until the font is cached on the visitor’s computer. Don’t overuse it.
You need to specify a fallback version for the font in case the browser is not supported. Here the browser uses a serif font in case it’s not able to load the “Tangerine” one.
body { font-family: 'Tangerine', serif; font-size: 48px; } |
The crossbrowsing issue
Each browser behaves in a different way, of course IE being the worse. To make a consistent behaviour across all the supported browsers you should use the webloader. This gives you the chance to display the styled elements with an initial font until the requested font file is loaded, and when this occurs to change it.
.wf-inactive p { // Show paragraphs in serif font until fonts have loaded. font-family: serif } .wf-active p { // Show paragraphs in Tangerine when the fonts have loaded. font-family: 'Tangerine', serif } |
If the number of fonts will increase then Google Fonts Directory will be a good alternative compared to other webfonts providers that offer this as a paid service. But you still can have fun with it, I’m sure google will continue to improve this service.
Check the demo
A demo page can be seen here. Interesting to see how IE behaves.
More resources
and of course
Check the Spanish version of this post:
Page 1 of 2 | Next page