Form the beginning of my developer’s career, I had problems with clients (and designers) which want to use this “fancy Lobster font” they found lurking on the web.
There were some solutions like sIFR, but not so elegant. Then I tried with font-face. It was working, but there is always trouble with font licenses and other stuff.
And then, Google stepped in and solved the problem. Google released a Google Font API which helps you add web fonts to any web page.
Benefits of the Google Font API include:
- A choice of high quality open source fonts.
- Works in most browsers.
- Extremely easy to use.
- It is hosted on Google, so it saves your bandwidth.
- As it is on the same place on the web, browser will cache them ones picked up.
It is very easy to use the API. Just add a line in your head section that will load the necessary CSS:
[code lang=”html”]
<link href=’http://fonts.googleapis.com/css?family=Reenie+Beanie’ rel=’stylesheet’ type=’text/css’>
[/code]
And then use it in your CSS like this:
[code lang=”css”]
h1 { font-family: ‘Reenie Beanie’, arial, serif; }
[/code]
You can see the font in action here.
How does the API work? When a browser sends a request for a Font API stylesheet (as specified in a <link> tag in your web page), the Font API serves a stylesheet generated for the specific user agent making the request. It basically helps developers check the browser version and inserting the proper CSS.
For best display in IE, make the stylesheet <link> tag the first element in the HTML <head> section. In IE, if the link is placed after <script> tags, the entire page will block and not display anything until the web font is loaded.
Chrome, Opera, Safari and Internet Explorer will show a blank space before the font is loaded, and FireFox will first show unstyled font. Google provides a loader which makes all browsers behave like FireFox.
Here is an example taken from Google Font API documentation:
[code lang=”html”]
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js">
</script>
<script type="text/javascript">
WebFont.load({
google: {
families: [ ‘Tangerine’, ‘Cantarell’ ]
}
});
</script>
<style type="text/css">
.wf-inactive p {
font-family: serif
}
.wf-active p {
font-family: ‘Tangerine’, serif
}
.wf-inactive h1 {
font-family: serif;
font-size: 16px
}
.wf-active h1 {
font-family: ‘Cantarell’, serif;
font-size: 16px
}
</style>
</head>
<body>
<h1>This is using Cantarell</h1>
<p>This is using Tangerine!</p>
</body>
</html>
[/code]
Visit Google Fonts API to see the list of available fonts and read documentation.
Hi,
I wanted to let you know that I wrote a little plugin that makes it easy to use Google Web Fonts from within the WordPress admin panel.
I hope you like it – http://easywebcoder.com/wordpress-plugins/google-web-fonts/
I welcome any feedback.
Jeff Sebring
Thanks for your article, I have retweet it 🙂
Comments are closed.