Enhancing typography with CSS3
Video: Enhancing typography with CSS3One of the features of CSS3 that I'm most excited about is its ability to enhance typography and finally add a viable alternative when choosing fonts for our sites. In the past, we've either had to deal with using the same system fonts over and over and over again, all resorting to pretty complicated workarounds like Cufon or sIFR. Now, CSS has allowed us to fake font usage by swapping up background images and text but none of those approaches have even come close to the usability and accessibility of simply declaring a font for the page to use. We'll add font-face as part of the CSS3 specification and it allows authors to reference online fonts and then use them within the author styles.
Viewers: in countries Watching now:
In HTML5 First Look, author James Williamson introduces the newest HTML specification, providing a high-level overview of HTML5 in its current state, how it differs from HTML 4, the current level of support in various browsers and mobile devices, and how the specification might evolve in the future. Exercise files accompany the course.
- Understanding the history of HTML5
- Using new tags
- Understanding HTML5 semantics
- Coding ID and class attributes in HTML5
- Structuring documents
- Building forms
- Exploring HTML5 native APIs
- Encoding and adding HTML5 video
- Exploring associated technologies such as CSS3
Enhancing typography with CSS3
One of the features of CSS3 that I'm most excited about is its ability to enhance typography and finally add a viable alternative when choosing fonts for our sites. In the past, we've either had to deal with using the same system fonts over and over and over again, all resorting to pretty complicated workarounds like Cufon or sIFR. Now, CSS has allowed us to fake font usage by swapping up background images and text but none of those approaches have even come close to the usability and accessibility of simply declaring a font for the page to use. We'll add font-face as part of the CSS3 specification and it allows authors to reference online fonts and then use them within the author styles.
It's actually been around and supported within browsers for some time, but until recently the issues surrounding font licensing have really prevented any widespread adoption. Now lately, several online font hosting services and a rise in open-source online fonts has led to an increasing number of sites serving custom fonts using @font-face. Now, the syntax is pretty easy to learn. Check it out. So, that's pretty simple. You just say @font-face, then in the font-family property you give the name of the font that you want to reference later on in your code, and then you pass the source where the actual font itself can be found.
So, all that we would have to do after that is reference the name, in this case myFont, in the font-family declarations throughout the site, and then obviously in this sample, I would have the Embedded OpenType, that's EOT file font, on my server. Now, you could also link to another domain or a hosted service as well when you're pointing to the resource. In this case, if I wasn't licensed to distribute that font, I could be in trouble because I'm hosting it on my server. So, you need to be extremely clear on the permissions that you have for any fonts you're going to be using like this.
Okay, as with any new features, there are some support issues across multiple browsers that you need to be aware of. Safari has supported @font -face since version 3.1. Opera versions 10 and above, Internet Explorer since version 4-- No seriously. Since version 4! Firefox since version 3.5 and Chrome since version 4, but here's where it gets a little tricky. @font-face allows for multiple types of fonts to be used and they're not supported in all browsers. Here is what's supported.
Safari supports TrueType fonts and OpenType fonts, although the iPhone or iPad only supports SVG fonts. Internet Explorer only supports Embedded OpenType fonts and Firefox and Chrome supports TrueType and OpenType fonts. Opera supports TrueType, OpenType and SVG. Now the Web Open Font Format or WOFF is a relative newcomer to web font types and currently it's only supported by Firefox. Wow! That certainly muddles things up a bit, doesn't it? Well, the good news is you can modify your syntax to pass more than one font type.
Here's how we do that. Whoa... Hey, what's up with the smiley face? Well, here I'm going to turn to Paul Irish to explain all of this syntax to you. Now, Paul has an excellent blog post about cross-browser font syntax. I encourage you to go check it out. So essentially what we're doing here is we're declaring the font-family as normal, but then it gets a little bit tricky and it's got everything to do with Internet Explorer, which only supports the Embedded OpenType font format. Now, if you went ahead and just declared multiple font types, Internet Explorer will try to download everything else as well and you'd have some problems.
By using this tricky syntax that we're doing, we keep Internet Explorer from choking on the other font formats. Now, other browsers are going to simply keep going until they find a format they can use. So, that's what's up with the smiley face. See the local property tells your computer what the font is named locally, so that if it's already installed on the machine the browser is just going to request it instead. The problem is there's a chance that there might be some font confusion, so you might say Garamond, but their Garamond might look a lot different or it might not even be named the same thing. So the chance for confusion is pretty high.
So to make sure the exact font you want is used, the one you declared in the syntax, Paul passes a value that will not possibly be confused and he uses a smiley face, which is absolutely lovely. Now, the format type that follows that is there just to confuse Internet Explorer. So you'll notice the EOT font is always the first one that shows up in the source and then use the other syntax for everybody else. Okay, so now that you know all of that, let's talk about how to get a few web fonts prepared for our next exercise. Now, due to licensing issues, we did not supply the fonts that we will use in the next example, so they're not in your exercise folders.
So, I want to show you my favorite way to go and get license-free fonts for my web projects, and it's there that we're going to find the fonts that we're using for our next exercise. Now, Font Squirrel, which is fontsquirrel.com, is a fantastic site that's got a large collection of free fonts that you can use in your projects. Even more importantly they have this, the @ font-face Kits, which is where you need to go. So you're going to go to fontsquirrel.com and you're going to click on the @font-face Kits. So, what these are is a list of different fonts and you can see they've got a lot of these guys that not only will you be able to download the font type; you'll be able to download multiple font types and the syntax for cross-browser compliancy.
So these kits are really great way of including these fonts in your sites without having to write a lot of this code yourself or worry about the minutia. Okay, now I'm going to scroll pretty much all the way down there,. Two of these that we need for our exercise. The first one that we need is going to be found -- I keep going here, try not to go too fast-- in the Serif section we're going to find Mido. So, I'm going to go ahead and click the icon here for Get Kit. It's going to prompt me to go ahead and download that, the Mido-fontfacekit.zip.
I'm going to go ahead and do that, and then I'm going to scroll down and in the next section, the Slab Serif, I'm going to find ChunkFive. Now, those are the two fonts that I'm going to use to enhance the typography of the trail guide. Feel free to experiment. If you want to try some of the other ones, by all means go ahead and kind of play around these a little bit. So, I'm going to choose Get Kit on that as well and I'm going to save and download it. Okay, now once you've download these you're going to find a ZIP file. Go ahead and extract the ZIP file and let's talk about what you're going to find inside of this.
So, looking inside of this folder, I can see that I have the Embedded OpenType font, an SVG font, a TrueType, a WOFF and in some cases you're going to have OpenType fonts as well. The font license is included as well. I highly recommend that you read that license before you use these within your projects and there's also a demo page. More importantly, there is a style sheet. If you open up the style sheet, I'm just going to double-click on it, open it up with Dreamweaver but you can open it up with any text editor or HTML editor that you want. You can see that the font-face declaration is already made for me, including the smiley face, because I'll be honest with you, I don't know how to type on the keyboard to get the smiley face.
So you can just copy and paste it, and if you wanted to use another character other than the smiley face you could. It's just one of those things that is kind of cool to do and it ensures that it's going to work properly. Okay, so now we're ready to go and in our next movie we're going to use these fonts to spice up our trail guide page. Now, I need to mention here that @font -face is not the only viable solution for web typography. A quick search online will show many different techniques and libraries designed to make using on your web site easier. For example, Google now hosts a font library which can be accessed through some fairly simple code on your page.
I encourage you to learn as much about @font-face as well as those alternate techniques as you can to determine which of them is right for your projects.
There are currently no FAQs about HTML5 First Look.