Quick Tip: How to Work with @Font-face
Due to the fact that @font-face can be a bit overly complicated, it hasn’t caught on quite as much as it should. Once you start reading about licensing, different font formats, browser consistencies, it can potentially become more trouble than it’s worth.
But – in five minutes, I’ll try to simplify the process of working with custom fonts as much as I possibly can. Services like Font Squirrel help to make the task a cinch!
Final CSS
@font-face { font-family: 'blok-regular'; src: url('type/Blokletters-Potlood.eot'); src: local('Blokletters Potlood Potlood'), local('Blokletters-Potlood'), url('type/Blokletters-Potlood.ttf') format('truetype'); } @font-face { font-family: 'blok-italic'; src: url('type/Blokletters-Balpen.eot'); src: local('Blokletters Balpen Balpen'), local('Blokletters-Balpen'), url('type/Blokletters-Balpen.ttf') format('truetype'); } @font-face { font-family: 'blok-heavy'; src: url('type/Blokletters-Viltstift.eot'); src: local('Blokletters Viltstift Viltstift'), local('Blokletters-Viltstift'), url('type/Blokletters-Viltstift.ttf') format('truetype'); } h1 { font-family: blok-heavy, helvetica, arial; }
Notice how we’re referencing both an .eot and .ttf font? This is because, of course, Internet Explorer only uses its own format, that has yet to truly catch on. As such, we must first import that .eot file, and then move on to the different formats for Firefox, Safari, etc. It’s essential that you load the .eot version first.
Next, we search for the font on the user’s computer by using the “local” attribute. If it’s unfound, only then do we pass a url that will load the font.
Why Doesn’t IE Try to Load the TTF Fonts?
This was definitely a concern. If Explorer can’t work with the truetype format, we don’t want to waste time trying to download the font. Luckily, because of all those local attributes, and the commas, IE won’t understand any of it. As such, it will simply skip the line all together, thus, only utilizing the .eot version.
- Follow us on Twitter, or subscribe to the Nettuts+ RSS Feed for the best web development tutorials on the web.