Start learning with our library of video tutorials taught by experts. Get started
Viewed by members. in countries. members currently watching.
In this hands-on course, James Williamson demonstrates the concepts that form the foundation of Cascading Style Sheets (CSS), including styling text, adding margins and padding, and controlling how images display. The course also explores the tools needed to work with CSS, the differences between embedded and external styles, how to use selectors to target elements, and what to do when styles conflict.
Now that we have discussed the syntax behind defining a font family and using a font stack, I want to talk about Web fonts. Web fonts allow designers to use the in-line @font-face rule to define an external font as a font family to use within the page. The font referenced is an external resource that the browser then request and uses on the page. This means that designers can now use any font they wish, providing they have the proper license to use the font and access to the font itself.
The way that we defined fonts in the last movie where we just defined a regular font family stack, that relies on the client machine to have the fonts installed. This technique @font-face essentially allows you to use any fonts that you have reference to, the browser will request the font and then display it. Let's take a closer look at how this works. I have the web-fonts.htm file open from 04_02 directory. What I want you to do is at the very top of your style declarations, above any of the other rules, we're just going to do a comment.
So I am going to do /* and I am just going to type in @ font-face rule declarations and then */ to close the comment out (/*@font-face rule declarations */). And just leave myself a little bit of space. So the way that this works is we are using @font-face rule to basically group all these external resources together and say this is what we are going to name the font. Then later on in our styles, we reference that font name in our normal font stacks and that's the resource that it uses.
The actual declaration @ font-face is a little complex. So in order to make life a little bit easier for you, you'll find in the 04_02 folder, a folder called assets. I am going to open that up and inside that I am going to open up syntax.txt. So this is just a text file that already has the syntax written inside of it. It's going to save us a little bit of typing and this is going to make it a little bit easier for us to go through the syntax in terms of how this works. So what I am going to do is I am going to copy the very first @font-face declaration.
You will notice I need the first line, including the curly braces, the opening curly brace and then the closing curly brace as well. So I am going to go ahead and highlight all of that and copy it and then in Web fonts in that sort of clear space that I created here, I am going to paste it. So let's go over a little bit of what the syntax is doing. I am not going to go over line by line because there is a lot there. But one of the first thing that we do is we declare a font-family name for this and this is essentially us telling the browser hey, whenever I use this name of this font, I want you to go grab this font and use it.
We are giving multiple locations for this font to our browsers and that's because it's still very new in terms of its implementation, and different browsers require different types of font. So you have to supply it with several different resources. Now those fonts themselves, currently I have them located in the _fonts directory. I am going to open that up and you can see here all these different font types, there is the Cantarell font. Now we are going to be using in this lesson Cantarell, which was an Open Source font that is free to distribute and it was created by the very talented Dave Crossland.
Be sure to read more about the Cantarell font, it's really nice. But you have to have access to these resources. Now there are several different ways that you can have access to those resources and I am going to talk to you about them towards the end of the movie. So just so, you know, when it points to those fonts that where it's pointing to. The next thing we do is at the very end of this, where we have requested all these resources that we need in order to make this work and all the various browsers, we then basically declare, and this is optional what font-weight we want applied when this font is applied.
Often times there are several different fonts within a font-family. There might be Cantarell Bold, Cantarell Heavier, Cantarell Lighter, Cantarell Regular and we get to choose what we want to use. So I'm saying that when font-weight is applied as normal and font-style is normal I want to use Cantarell-Regular. That's essentially what I am doing here. So now the next thing I want to do is I want to use the font. In order to use it, I have to go down to where I might have some additional font-family declarations and I am going to replace those. So I am going to replace the font stack that we have got currently going on in the body tag and I am going to replace that with Cantarell, and the spelling and capitalization matters here, Arial, and the reason I am using sans-serif font there as a fallback is because Cantarell is sans-serif itself, and then Helvetica, sans-serif.
So other than the initial @font-face declaration and the use of a nonstandard font with in the font stack, the font stacks are really kind of exactly the same as we had them for. I am going to save this, and then I want to test this in one of my browsers and when I do that I can see that I am using Cantarell. How cool is that? I am going to go back into my page and I want to go back to that syntax.txt file. You may have noticed that this was not the only declaration in this file, we have several different declarations.
We have three more in fact. I am going to go ahead and copy all three of those and then over in our webfonts.htm just below my initial font- face declaration I am going to paste them. Now it's very important that these font-face declaration's show up at the very top of your code. You want to make these available anywhere in your styles, and if you have the font-face declaration little bit further down, they might not be available. All right, so what's going on? I mean all of these are using the exact same font-family name, but you will notice this one is Oblique, this one is Bold and this was BoldOblique.
I am also saying okay, use Oblique when the font-style is Italic and the font-weight is normal. Use Bold when the font-weight is normal but the font-size is Italic and use BoldOblique when both the font-weight is Bold and the font-size is Italic. The order that you place these matters. Most of the time you want to go Normal, Italic, Bold, BoldItalic. If you don't do that a lot of browsers sort of have a little bug that shows up. The next thing I am going to do is I am going test this, so I am going to go down in my Styles and right after my body selector, I am just going to write a selector for all of my links that goes ahead and applies the font style of Italic to that.
So now I am going to Save this and test it, make sure enough my links are now Italicized. They are actually using the oblique version of the font, they are not going ahead and skewing those characters within the browser. I want you to consider this a very brief introduction into how web fonts work. There's a lot more to consider before you start using web fonts in your project, not the least of which is ensuring that you got a proper license to use the fonts you want for your site. Just because you have a font on your hard drive doesn't mean you can use it on your web site.
You have to be very familiar with the terms and license to do that. Now I want to give you a couple of resources that can help you learn more about Web fonts. So let me switch over to my Chrome browser and here is fontspring.com this is one of the foundry sites that actually offers hosting for web fonts and they have got an article written back in February for The New Bulletproof @ Font-Face Syntax and this is the syntax that is being used most widely by the designers and implementers right now.
I will say that the syntax has continued to evolve over the last couple of years and I wouldn't be surprised if we see it change a little bit more but if you're looking to have this syntax sort of walk through and explained to you why it's done a certain way, why certain things are omitted or added this is definitely the article to read, and it's got a bunch of updates and comments in it that can help you out as well. Another site that you definitely want to bookmark and go to is fontsquirrel.com. The lovely thing about this site is that it offers a tone of free fonts that you can use commercially or privately in your Web sites.
Not only does it offer the fonts, but it also offers you what it calls @font-face kits, this will give you a download that includes all of the fonts, the fonts-license and CSS with all of the syntax already written in it using the new bulletproof syntax. So this really is just an amazing resource if you are a designer that wants to start using web fonts. Now if you use these fonts you have to host them on your own web site. You may not want to do that and they're a lot of hosting services out there now such as type kit, fonts.com.
Google actually has a free hosting service, the Google fonts API that you might want to research, where you can go and have another service host those fonts for you so that you are not hosting them on your own site. Now finally, if you want to learn more about web fonts, I would recommend my Web Fonts First Look course where I go into the syntax in detail, I talk about some of the hosting companies out there, some of the options and what your responsibility as a designer is in terms of using and utilizing web fonts on your own page. So be sure to go give that look after you finish with this title.
There are currently no FAQs about CSS: Core Concepts.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.