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.
In this chapter we're going to talk about styling container elements. One of the most basic concepts of CSS is taking parent elements and then styling them as page regions for their content. In order for us to do that, we'll need to understand the Box model. So we'll start there. Now the Box model is a term used to describe the rules and properties surrounding the physical properties of all HTML elements on the page. It's easier if you think about it like this. All elements on your web page are considered to be a rectangular box.
The properties of this box; its width, height, padding, and margin define not only the size of the element, but also how it relates to the elements around it. Without these properties we wouldn't be able to control layout at all. The first thing I want to do is just introduce the different parts of the Box model visually by adding some styles to a couple of elements that we're going to have on a page. After this exercise, we'll explore each of those properties in a little bit more detail. So I've opened up the box-model.htm file and you can find that in the 05_01 folder.
In this chapter of course we're going to be working out of the Chapter_05 folder as well. Very simple page structure, if I scroll down, we can see that we simply have a div tag with a paragraph inside of that and that is followed directly by another div tag with a paragraph inside of it. The reason that we're going to use the paragraph inside of it is because I want to be able to visualize all of this Box model properties, including padding, content, margins, and sometimes in order to do that you have to stack elements on top of one another. So I am going to go up to our styles and I am just going to go ahead and create a new selector for our div tags, and inside of that I am going to just apply a background color initially.
So I am just going to say background: #3ff. Now, I am using hexadecimal shorthand notation here. We're going to study this in the next chapter and we're going to be talking about the background property as well in more detail a little bit later on in this chapter. So if I Save this file and Preview this in one of my browsers, I can see that sort of blue color that I just set to my divs. Now, the page actually has a gray background color, so we're going to use it to help us differentiate between the elements and the page itself. All right.
Another thing you might notice, there's a space between the divs, I went ahead and zeroed out the margins for both the divs and the paragraphs. So as we add those margins back in, we'll be able to visualize those a little bit better. Okay. Now, going back into the div tag, one of the things that we can do is we can control the size of the element by also controlling its width and height. So on a new line within the div rule, I am going to go ahead and set the width property to 300 pixels, and then I am going to set the height property to 300 pixels as well. So go ahead and Save that, Preview that in your browser.
And now we see quite a bit of a change. Notice that now the div tags are 300 pixels wide, and they are 300 pixels taller. That's very hard to see because there's no space between them. This white space that you're seeing out here to the right-hand side, the body tag is being styled through a body selector and it has been given a width of 600 pixels. So each of these div tags is exactly now half of the width of our body tag. Padding is a property that often causes a good amount of confusion when it's paired with the width property. A lot of people just assume that this width that we just defined for the div tag is the total width of the div tag, it is not.
One of the things that you need to really think about when you're doing width and height is that really you could almost refer to those properties as content width and content height, because that's what they're defining. They are not defining the overall width of the element. As we're going to see in just a moment, the overall width of the element is actually determined by the borders, the padding, and the width and height. So I am going to go down to the next line and I am going to add the padding property. So we're just going to say padding, and I want to go ahead and do 10 pixels worth of padding. Adding the padding, as we did this way using the syntax, it goes ahead and adds padding equally to each side.
So top and the bottom have 10 pixels of padding and the right and the left have 10 pixels of padding. Now, if I Save this, go back into Safari and Refresh my page, I can see that the width and the height of these has increased a little bit. It's really hard to visualize this again, because I don't have really anything that's showing me exactly where the padding is versus the content width. So I am going to go ahead and do that now by taking advantage of the paragraph inside of the div tag. So I am going to go back to my CSS, just below the div tag, I am going to create a selector for my paragraph, and the only thing I am going to do here initially is just give it a background color as well.
So I am going to say background and this will be #c90. So if I save this and go back out to my browser and Refresh, you can see that now we actually get to see the padding in our div tag. Now, the paragraph doesn't extend all the way down to the 300 pixels worth of height. So just notice the tops, the left, and the right-hand side, you can see that sort of 10 pixels worth of padding all the way around that. The width of the paragraph is stretching all the way to the edge of the content width of its parent element.
So that padding is actually holding the contents of the div tag away from the edge of the div tag, that's what padding does for us. Now, what happens if you don't define say a width and a height? I am going to go ahead and remove both of those, and one of the things that I want to talk about is the behavior of what we call block-level elements; a div tag, a paragraph, headings, these are all block-level elements. Essentially, block-level elements sort of occupy their own space within the normal document flow, and when left to their own devices, they will stretch to fit the width of their parent element.
Let's go ahead and observe that behavior. So I'm going to go ahead and Save this, go back out to my browser and Refresh it, and you can see exactly what's happening now. The div tag is extending all the way out to fill the body tag, which is at 600 pixels wide, and then the paragraph is extending out as far as it can. Now, it can only go as far as the edge of the padding of the div tags, because again, padding sort of keeps the content away from the edge of the element. So the padding inside the div tag is keeping the paragraph content away from its edge.
So the blue that you're seeing is the padding of the div tags and that sort of ocher color that you're seeing there is the actual content of the div tag itself, which in this case is the paragraph. Let's visualize this a little bit more and see how height and width are affected through the use of borders, which is another property of the Box model. If I go back into my styles, I am going to first go into the div tag selector and I am just going to go ahead and give it a border, so we're just going to do border: 1px solid #300.
So make sure you have white space between these properties, we're using border shorthand notation here. So we're just creating a 1-pixel solid border that has this sort of maroon color applied to it. And then in the paragraph we're going to go ahead and do the same thing. Here I am just going to do border: 1px solid #000, which will be black. So if I Save this, and once again, I am going to go ahead and Preview this. You may or may not have caught that change, but essentially what has happened now is the border also needs to fit within the 600 pixels, and we've added 1 pixel border to the left-hand side, we've added a 1-pixel border to the right-hand side, and then we've added 1 pixel border inside the paragraph, and 1 pixel border to the right side of the paragraph.
So what that means is that essentially on either side we've had to reduce the content by a certain number of pixels. So essentially what happens is borders, padding, and the content width of an element all sort of add up to make its total width. If you change one of those and you don't have the other ones defined, the other ones will basically shrink or expand based upon the available space that you're giving it. I am just going to go back into my code. If I take this to an extreme and take this sort of outer div tag border and cranked it up to 10 pixels, watch what happens to the inside content here.
Notice that the height gets much taller and the width of the paragraph gets a lot smaller, because we still need that 10 pixels of padding. We now have 10 pixels of borders on either side and we have much less room for the content now. So anytime that you modify one of those properties, whether it be a border or whether it be padding, if you don't have an explicit width set for the element, you're going to lose some of that width, or if you reduce those amounts, you're going to gain some width, so you'll always have to keep that in mind. I am going to go back in and I am just going to reset that border to 1, because that looks a little ungainly.
So far we've talked about content, we've talked about the width and height in terms of relating to the content, we've talked about padding and we've talked about borders. What we haven't talked about yet are margins. So let's go ahead and see what margins can do for us as well. I am going to go right over here to the div element sector and I am just going to type in margin, much the same way we did padding, and with margin I am just going to type in 10 pixels as well. So this should apply 10 pixels worth of margin all the way around the outside edges of our div tag; so top, bottom, left, and right.
Let's see what this does for us. So if I Save my file and go into Safari and test this again, I can see that what's happening now is we're getting spacing between these div tags, they're not butting right up against each other anymore, which is nice. We're also getting 10 pixels worth of spacing on the outer edges of it as well. Now, this is not increasing the size of the body element. The body element is still at 600 pixels, it's not going to flex or move when we give it an explicit value like that. So what happens is this content has to flex a little bit.
So again, what happens, we lose content width right in through here when it's not predefined. It allows that content width to sort of flex based on these other values. One of the things that you might have noticed if you're a real keen observer here, you might have noticed that we gave 10 pixels worth of margin all the way around these. We're seeing it on the edges, but we're not seeing it on the bottom, we're not seeing it on the top, and we're really not seeing it between it, because if we were seeing 10 pixels worth of margins for both of those, they would be 20 pixels apart, right? Well, what you're seeing on the top between them and on the bottom here is what we call vertical margin collapse.
Later on in this title when we have the movie on margins, we're going to talk more about vertical margin collapse and why it happens. I want to take a moment to reinforce the concept of how you calculate the total width of an element and how you know how much space is left for the width of the content. So I am just going to go down to a new line here and we're going to do a little bit of math. This is not CSS syntax, this is just to help me visualize this, because I am not very good at math. All right. So we have 600 pixels worth of space available to us and we're getting that from the body selector up here.
So if we go down and really calculate what we have here, we know that we have 10 pixels worth of padding on either side of our div tag. So that's 10 pixels plus 10 pixels. Now, I know, because I am at least that good at math, that that's 20. So we have 20 pixels worth of space being taken up by the padding. Then we have 10 pixels of margin on either side, so we know that that is 20 as well. So we have 20 pixels based on the padding, we have 20 pixels based on the margins. Then, and this often gets overlooked, we do have 1 pixel worth of border on the left and right sides.
So that's going to give us an additional 2 pixels to our total element width, and you can't forget the border of our paragraph, can we? It's 1 pixel as well. This is kind of tricky, because the border is applied to the paragraph and the paragraph is expanding to fill the content width of the div tag. So it doesn't really add up to the content width of the div tag, what it does is it limits the content width of the paragraph. So we're going to leave that off for just the moment. So we have 20 pixels worth of padding, 20 pixels worth of margin to content width and 2 pixels worth of border.
So that's going to equal, obviously, 42 pixels worth of defined space that we currently have on this div tag. So what does that leave us in terms of the actual content width that we have to deal with? So if you add that up, that's going to give us a total of 558 pixels to deal with. So we have now 558 pixels worth of content width for this div tag and that's the available space for our paragraph to fit into. Now, I mentioned this is not CSS syntax, this is just helping me visualize this, so I want to go ahead and get rid of this, but I do want to put that theory to test.
So here's what I am going to do, I am going to go into the div selector and I am just going to say, okay, fine, what if I made the width something bigger than that? What if I said 565 pixels, what if I did that? So if I Save this, go back to Safari and Preview it, you can see what happens. We don't break anything per se, but our element width no longer fits, our content width no longer fits into the space that we're giving it, it no longer fits in that available space. What browsers are instructed to do when this happens is that if there's still room inside this parent element, in this case the body selector, it's instructed to ignore the margins.
So margins, while they do calculate in terms of where an element fits into the layout, one thing that you have to remember about margins is that they will be sacrificed in order to help an element stay within its parent container. So in this case, the right margin is being just sort of thrown away, and that's one of the reasons why I tell people margins are important for planning document layout, but you should not use them to calculate the total width of an element, because they can be discarded. You certainly need to take them into consideration and know how far your elements are going to be apart, but if you're looking for the total container box of your element, margins really don't come into play for that.
So what happens if we increase the width to a level that you simply don't have anymore margins to sacrifice? Well, let's find out what happens there. So I am going to go back into my code and I am going to show you a very common mistake that a lot of new designers make. They're going to go in and declare like a width of 600 pixels, because they say, okay, I've got 600 pixels worth of space to deal with, so I need my div tag to be 600 pixels wide as well. They're ignoring the fact that they have the padding, the border, and the margin. So what happens? So if I Save this, go back into my browser and test it, what happens is we end up with overflow.
So right now, these div tags are overflowing their parent container, in this case, the body selector. Now, browsers are instructed in terms of how they should deal with overflow and the default overflow instruction is to make that content visible. They error on the side of showing it rather than not showing it. But there are some things that you can do. For example, if I come up to the HTML tag right here, I can come up and I can say overflow: hidden Overflow is the property that we use to tell browsers how we want to manage the overflow of our content.
So if I save this and test this in the browser, nothing really happens. Well, remember, this is on the body element, this is on the body tag, not so much the HTML. So if I come back into the body element and come in and say overflow: hidden, Save that and test it. Boom! Now that content is actually being clipped off. We can't see to the right of that content anymore, because it's gone and there's actually no way to scroll to it, no way to get to it, it really does hide it, it clips it right off.
Now, there are other things that we can do. For example, we could come in and just say scroll, and if we say scroll, we're going to be given scrollbars that allow us to scroll around this element. Now, different browsers render this a little bit differently. You'll notice that in Safari, it's giving me a bottom scrollbar, but it's also giving me the Chrome for the vertical scrollbar as well, which in this case we don't need. So maybe a better choice instead of scroll would be auto. If I Save that and test it, I can see that auto only gives me the scrollbars that I need, in this case, I am getting scrollbars on the bottom hand side, and this allows me to kind of go back and forth here on this element.
Now, that's actually a really useful thing to know, because it's quite a common technique actually to create a parent container that's not wide enough to contain the children of it and then add scrollbars so that people can scroll through it. It's very reminiscent of what people used to do with frames and you can create some neat little widgets that way as well. I know that was a lot, but really those are just the basic elements of the Box model. Once again, width, height, padding, borders and margins. Now, we're going to explore every single one of those properties and how they work with each other throughout this chapter.
We're also going to explore other properties, like the background property, which isn't technically a Box model property, but it's still very important when styling container elements.
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.