Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Although there is simply no way to show everything that CSS is capable of in one chapter, I do want to give you an idea of some of the options that you have when styling elements. We're going to start by exploring some of the things that you can control around styling text. So here we are in Chapter_06 again, the 06_04 folder, I've opened up the type.htm. Just so you have an idea of kind of what's happening on this page, we have a heading that says HTML and CSS and then three paragraphs below that, so we have an h1 and three paragraphs.
And we are just going to control some of the basic typography on the page. So I am going to go up to my style element, and I could see that there is already one rule in place and that's the body rule, which is affecting everything within the body. And I can see that I am setting the width of my page to 70%, and then I'm doing this little trick right here, which is margin. And margin is the space between elements. And by saying margin 0 we're saying set the margin to 0 to top and bottom. The first value when you use 2 here is top and bottom, and then right and left will be auto, and that basically means center of the page, if you will.
I want to focus on the heading 1 first. So I am going to go ahead and create a selector for the heading 1, and I am just going go ahead and create my opening and closing curly braces there. And now I am going to start creating some properties for these. Now, in this one we are focusing on typography or basic font styling. Now, the first thing that you can control, that most people want to control anyway, is the family. Which font am I using? So to do that we are going to do font- family, and you are going to see that a lot of the font properties have the word font, a dash, and then what you want to change. Now, for the font-family we're going to set a specific font, and in this case we are going to say Georgia.
Now, what's really interesting about this is that you can pass in a comma-separated list of fonts, so I could say Georgia comma and then in quotation marks, because it's more than one word, Times New Roman, and then another comma, and then Times, and then finally, serif. Now, what's going on here? Well, basically what I'm asking a browser is, do you have Georgia? And if the browser says no, I say well, okay, fine. Give me Times New Roman. It says, well, I still don't have that. I say okay, one more time. Give me Times, and it's like no, I don't even have that.
So that's when you're at your wits end and you say, fine, give me whatever default serif font you've got. These are fallback fonts, and it's basically saying if my first option is not available to me, give another one. And you can have just one font if you want; you can have multiple of these. It's always a really good idea, however, to at least give it that default family, give me serif or sans serif or cursive or monospace so that it knows sort of what style of font you want, in case it doesn't have any of these. Okay, now after a font family, I want to do font-size.
You could, if you wanted to, express this in pixels: 50 pixels, 40 pixels, 16 pixels, that sort of thing. I, on the other hand, am going to ems, and I am going to say 2ems. Okay, so what is an em? An em is a relative unit of measurement. We live in a very relative world these days. You don't know if somebody is going to be looking at your site on a browser or on a phone or tablets obviously, but I've even seen a stove that had a browser in it. What a relative unit of measurement does is it says, make this size relative to whatever your default font size is. So for a phone that might be 6 pixels; for a browser it might be 16 pixels. So by saying 2ems, you are basically saying give me twice whatever your default is.
The next thing I am going to do is I am going to say font-weight, and I want font-weight to be normal. Usually, you're looking at either bold or normal there. Since headings are almost always bold by default, if you don't want them bold, you have to explicitly set font-weight to normal. And finally, I am going to do font- style, and I am going to do that italic. So you can italicize by using font-style. So weight handles whether it's bold or not; style handles whether it's italic or not. If I save this and preview this back in my browser again, you can see that it changes the formatting.
The size stays about the same, because 2ems is very close to the default size of most heading 1. And it's no longer bold, the font- weight is normal, and the font is italicized. We are going to continue setting font properties for our paragraphs. So I am going to go ahead and create a selector for the paragraph, which would p, and use the opening and closing curly braces as well. So every single selector looks exactly the same except for the characters you use for the selector. We're going to do the same thing for paragraphs: we can do font-family.
I am just going to go head and copy that, paste that in there. Save myself just a little bit of time there. Next, I am going to do font size again and for paragraphs I am going to do 1em size, and that's basically saying whatever the default font size is on your user agent, give me that. And then I am going to set a property we haven't done yet, which is line-height. Line-height is line spacing. And then I am just going to do 1.6. Now that's not space between paragraphs; it's space between the lines within the paragraph. 1.6, that's using a multiple. You could use pixels.
You can even use ems there, but by using a multiple that's 1.6 times whatever the size of the font is. So about 160% of the size of the font is the amount of line-height I am getting. And then finally, you might want also control alignment of the text. We have a couple of options available to us in CSS: we can do center alignment; we can left-align it; we can right-align it; we can also justify. So I am going to do justify which is going to justify the type so that it's neither right- or left-aligned. It's going to be solid on both sides. So if I save this and again preview this again, notice that our paragraph text gets a little larger, because we told it to be 1 em.
We've got the line spacing going on and we have this justified text as well. It's not too efficient to have to keep doing the same thing over and over again, and if you have to define a font family for every single element that you are working with, that's not very efficient at all. So one of the things I want to pass along to you here before we move on is the concept of what's known as inheritance. So if I go up to the body and let's say I take this font family declaration and I just cut this out of the paragraph selector and then I paste it into body--maybe I want it to be something a little bit differently. Maybe I say Arial, Helvetica, and then we'll do sans serif.
So now if I save this, what I'm saying is, for the body element, which is this little guy right here, I am saying everything inside there should be Arial. That means that I've just globally told my entire file, I want your font to be Arial. Now what's really interesting about CSS is these interior elements can than overwrite that. Instead of trying to define the font for every single element, what I am going to do is define it once on body and then anytime I need it to change, that's the only time I have to declare it. So now if I save this and once again go back into my browser and preview it, you can see kind of how that works.
The heading is Georgia, but the body is Arial, even though it's being set up here on the body. I just want to set a couple of more properties here and than we'll move on. I am going to go back down to the paragraph, and I am going to change the paragraph's width to 60%. width is a property you can use on just about any element. That's going to control, as you would imagine, how wide it is. Now this 60%, I could have used pixels, I could have used ems, I could use any unit of measurement I want, but when you use percentages, what that's doing is it's saying, hey, go up to the body and give me 60% of whatever the body is.
And in this case, if I save that and preview it, you'll see that it shortens that considerably. So even though the body is taking up this much room, by saying 60%, I am just saying not quite all the way to the edge of the page. This creates a more readable column. And then the last thing I am going to do is control the spacing between these lines. For the headings and paragraphs, all the browsers have default margins that they put between them, and that controls how much space is between them. Well, if you want to take control of that about yourself, you can use margins.
For the heading 1, I am going to do a margin, and I am going to set that margin's value to 1 em 0 and .4 ems. Margins you can do shorthand. Notice here I am passing two values; here I am passing three. When you pass three values, this is the top, this is left and right, and this is bottom. So I am saying give me 1 em's worth of space above the heading; don't give me any margin left and right, but give me 0.4 ems on the bottom. Now if I save this and preview it, I want you to watch the spacing right here.
You can see it added a little bit of spacing above it and took away a little bit of spacing below it. I want to do the same thing more or less for the paragraph. All I am going to do for paragraph is this. So I am going to type in margin 0. Now what that would do for me is it would take away all of the spacing in the paragraph. You can see, they're just budding right up against each other. Now what's interesting is you can have multiple declarations in the same rule. So right after margin, I could come down here on the next one and say margin-bottom and explicitly set a bottom margin to say 1 em.
Now, if I save that and test it, I can see that that bottom margin is restored, and now I'm only spacing paragraphs from the bottom of the paragraph and not the top of it. If you feel a little overwhelmed, don't. Now, remember, this isn't a CSS course. The main purpose of these exercises is to show you a little bit of what CSS is capable of and to exposure to the syntax so that you'll have a head start when you begin learning CSS in earnest.
Get unlimited access to all courses for just $25/month.Become a member
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.
Your file was successfully uploaded.