Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
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.
Most of the time we'll start off with an entirely new exercise file, but in this case I wanted to pick up where we left off in our last movie, since it's really a continuation of what we were just doing with line spacing. So if you didn't watch the previous movie, go back and watch that really quickly, so that you get to know what we are doing here. So if line spacing allows us to control the spacing between multiple lines of text what about the space between the elements themselves? Now for that we typically use margins. Now we're going to talk about margins and padding in a little bit more detail later on in another chapter.
I just want to introduce them here as a means of controlling text formatting. So I'm going to preview this page before we do anything to it in a browser, just to sort of refresh our memory. And so in the last movie we set the line height property, which is dealing with the spacing between these lines of text. However, the spacing between the elements themselves is controlled by margins. I'm going to go back into our file that we were working with here, and we're going to be working with the element- spacing.htm and this is found in the 04_14. Now the structure of this page is exactly the same as the one we had in the last one, and pretty much to the syntax of the CSS is, exactly where it was when we left off with the last movie as well.
Now you'll notice that we don't have any margins going on in our document right now. So you might be wondering well, wait a minute what's controlling the spacing between them right now? Well, remember every single element has default styling that the user agent applies to it and those margins are part of that. So one of the first things you might want to do if you're really going to over ride these is to go ahead and strip them out. So I'm going to go all the way up to the top of my styles and just below, the sort of grouped aside in the article style, I'm just going to come in and I'm going to say, okay, body, h1, h2, h3, p, and then I'm just going to do margin of 0, and that's a very quick down and dirty, no-nonsense CSS reset.
We're not doing any padding or anything else, we're just sort of stripping out all of the margins. Now if I save this page, and I test this again in the browser, you can see what this does for us. It just goes ahead and gets rid of all the margins between it. Now the only thing controlling any spacing whatsoever is the line height, so now we need to go back in and sort of put in the proper margins that we want for our pages. Now one of the strategies that's really served us well up until this point with font sizing and line height is to just go ahead and apply one of the values that we want to the body tag, allow that to be sort of the default and let it inherit to everything else.
Doesn't quite work that way with margins, let's just experiment with that and see why? Now I do have a margin already applied to body, but I'm going to go down to the very bottom and do this again. And I'm just going to come in and I'm going to say margin: 1em. All right. Save that, and then I'm going to test this on my page. That's not we were expecting at all. Nothing happened with the elements, but everything else on the page gets sort of moved around and looks kind of weird now. Well, that's one of the things that you learned very early on about margins and padding. Those properties, unless you tell them to, do not inherit, and if you think about that that makes sense.
If you put 10 pixels worth of margin on the body and then every other element got 10 pixels worth of margins, pretty soon everything will just sort of be spreading away from each other in ways that you don't want. So I'm going to get rid of this. Now we need to go into the individual elements themselves and control other margins. So I'm just going to go right down here to the h1, h2, and p selectors. So here's what we're going to do. I'm going to start off by using instead of just margin, the margin-bottom property. So this is one of the nice things about margins and padding. You can apply them to whatever aside you want.
In this case since we're controlling the spacing between these elements I don't want to have any extra space above them so we're just going to do the margin-bottom, which is going to control the spacing below these paragraphs and headings. Okay, so here I'm going to change this to 24 pixels and I'm going to do that consistently for each one of these, margin-bottom: 24px margin-bottom: 24px. So we have the heading 1, heading 2 and the paragraph, all now have bottom margins of 24 pixels. So if I save this and preview this in my browser, okay, our spacing comes back which is nice, and then the spacing between the elements is all exactly 24 pixels, they're all exactly the same.
Now you might be saying to yourself, well, they don't look exactly the same. Well, do keep in mind that we do have a line-height, so the line-height basically is also on these bottom lines sort of pushing down a little bit below that as well. So what we have here is instead of a margin that is relative to the actual element itself or the size of that element, they're just all consistent and that doesn't give us a very nice flow to our page content. So I'm going to go back into our file and let's change this to a relative unit of measurement. So instead of 24 pixels, I'm going to go ahead and say 1em, and then I'm going to do the same thing here, 1em on the h2, and then a 1em for a paragraph and I'm going to save this.
Now you might be saying yourself, well, yeah, but you just swapped that out with 24 pixels with 1em, so you're still going to get the same space between them right? Not exactly. If I save this and test this in the browser, you can see that now our spacing is adjusted a little bit and the reason that it adjusts is because those em values are being calculated based on the size of that particular element. If we go to an h1, its font size is going to be 1.8ems, which is 1.8 times 16, and that's what the value of the bottom margin is going to be.
Same thing for h2, 1.4 times 16, that's going to be that value, and the paragraph spacing will be exactly, say 16 pixels if that's the default, because it's spaced off that as well. So even if you use a relative unit of measurement on an element like a heading 1 or heading 2, remember that its value is calculated based on that element's font size, not the default font size of the page, and that's a very important thing to keep in mind. Now you can also make these independent of each other. So if I go down to the h2, I could make that say maybe 0.2ems, and if I save this and test it, you can see what this does for us.
It just creates tighter spacing between our heading 2s and our paragraphs, whereas, the paragraphs and other elements and the heading 1 in our paragraphs have a greater amount of spacing. So you can tweak those values to sort of get that vertical rhythm that you're going for. Now there is a little bit more to using margins than this. We still haven't talked about vertical margin collapse, for example, but for now it's enough for us to know that the ability to control spacing between elements lies with the margin property and then if we begin to use relative units of measure for those margins, that the size of the margin will be based upon the size of that particular element, not the default size of the page.
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.