Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
The key to web fonts is the @font-face declaration. This small bit of CSS code allows you to incorporate nonstandard fonts into your web page. With the @font-face declaration, you can change headings like this into headings like this. Let's go through it step by step. So I'm going to go over to main.css, and let's just go straight into Code View and scroll down past the opening comments there, and we'll put-in our @font-face declaration right at the top of all of our other CSS rules.
So we start with @font-face and then open up a curly brace, and I'll just close that out so that we have a complete statement, and now we enter-in a font-family property. Now the name of the font that you enter here is the name you'll use when you're inserting it into the CSS rules as part of other font families. I'll put this in quotes, and the name is DragonwickFGRegular.
We'll close that off with a semicolon. We want to find the source for this font. It is a local file that I have within the directory here. So I'll go ahead and highlight the URL and then press Return or Enter so I can browse for it. It's in Chapter 3, in the 03_05 folder. You'll see an _fonts folder, and then there are four variations of this. We're going to start by choosing the Embedded Open Type variation with the file extension of EOT and click Choose.
Then again, close it out with a semicolon. Not all browsers support all font formats, so you need to use multiple formats and include them in the @font -face declaration correctly. Although Internet Explorer 9 now supports the Web Open Font Format, WOFF file extension, and the TrueType format, earlier versions of Internet Explorer only recognize the Embedded Open Type or EOT format. Moreover, unless you're careful, the earlier versions of Internet Explorer will load all the other formats.
So to avoid that issue, you have to use a little code trickery. So let's continue with our @font-face declaration, and we'll put-in another source. This time instead of putting in a URL, we're going to say that it's local, which means that it's going to be on the user's system. What we're trying to do is to avoid actually picking any of the files on the user's system. So we'll put-in an opening parenthesis and a pair of quotes and this time instead of a font file name, we'll actually put-in an exclamation mark which is extremely likely not to be the name of any font that is on a user system.
We'll follow that by closing off the parenthesis and then putting-in a comma. I'm going to put this on the second line here, just give it a little bit of space to show that it's all part of the same source. So this is where we're going to bring in the other two formats. So we'll do a URL, and this time, I'm going to go ahead and just copy this path, paste that in, and change the file extension from EOT to WOFF; that's the Web Open Font Format.
Then after I close the parenthesis, I'll put-in another attribute which is format, and in quotes WOFF. Close the parenthesis and add a comma, so we can add in the next format. Again, a URL version, and let's copy that, and then paste it in here. Our final format is a TrueType and they use a file extension of TTF.
We will again put in the format attribute with TrueType. Now, if you're typing along with me, and you notice that whenever I type a single quote, a double quote appears, that's because I'm using Dreamweaver 5.5. If you're not seeing that, you're probably on Dreamweaver 5. This whole technique that we're seeing here was developed by a gentleman named Paul Irish who called this method the Bulletproof Technique, which seems to be actually quite true, and it's in wide use throughout the web.
Now that you made it possible to use a linked font, you must apply the font family to the desired selector. So let's go to Design View and open up the CSS Styles panel. I'll go ahead and select the h1 tag which is what we're going to look to replace, and I'll see which rule is already controlling the font, and that's the #mainContent h1. So let's go to that code by right- clicking on that, and choosing Go to Code. I'm just going to add in my new font name right in front of the existing font family, DragonwickFGRegular. Okay.
Let's save our page, go back to Design View, and you should see it immediately appear. If you don't see the font-face right away, just click Refresh. Changing a single font is a pretty simple example, but you can see the power of web fonts immediately. Not only is this text selectable and thus can be copied and pasted, but it's also eminently more search engine-friendly. Of all the improvements brought by HTML5 and CSS3, I really think that web fonts place among the most impactful, as they open the door for a far truer expression of the designer's artistry.
Get unlimited access to all courses for just $25/month.Become a member