Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Any discussion on CSS-based layouts should start with the box model. Understanding the box model is crucial to controlling page layout. Every element in your document is contained within a box, even the inline level elements. Browsers use an element's box properties to determine how much space it takes up on your page. Controlling these values gives us control over much of our layout. The box model is made up of five basic properties: an element's width, height, padding, border, and margins.
Backgrounds do take advantage of the box model. They display inside an element all the way to the element's borders, but they're not technically part of the box model itself. Not understanding the box model clearly can have serious consequences to your layout. Without understand the box model, you might think that an element's total width and height are determined by the width and height property. In fact, they're determined by a combination of the width, height, padding, and borders. Starting inside an element and working on our way out, we start with an element's width and height properties. Next is padding.
The padding keeps the element's contents away from the edge of the element itself. Then we have borders. Now, borders are optional. You can have different styles of borders, such as solid or dashed or dotted, but borders also have a width and a color. So the width of a border, an element's padding, and the width and height of that element all calculate together to form the total width and height of the element. Now what about margins, where do they come in? Well, margins don't technically calculate to the width and height of an element. However, they do help define the relationship between elements.
So, they're used to determine the amount of space between one element and another. Now, the best way to truly understand the box model is to go ahead and control those properties for yourself. So, let's dive in and set some values. So, here I have the box_model.htm file open. You can find this in the 10_02 folder. Before we start modify some properties, I want to take a moment to discuss the structure of the page. Currently, we have a paragraph, which is within a div tag with an ID of container. That's also found within the body tag.
We have some very basic CSS going on our page right now. So let's take a moment and see what we have. So if I go over to the CSS Styles panel, I can see that I have a rule right here targeting the paragraph inside the container. All I've got is a background color. It's that light tan background color that we're seeing here. Now the container div tag itself also has a background color. It's sort of a darker teal blue color, but we can't see that right now. The reason that we can't see it is because if you have a block-level element, it's going to expand to fill its entire container element.
So right now our inner paragraph, since there is no padding or borders or anything holding it away from the edge of the div tag, is going all the way to the edge of the div tag. Let's start setting some box model properties and see how it affects these two elements. The first thing I'm going to do is select the #container p selector. I'm going to go ahead and add a width and a height to it. So I'm going to add a property. I'm going to add width. I'm going to give it a width of 300 pixels. Then I'm going to add another property. I'm going to add height and give it a height of 300 pixels as well.
Now, while I'm typing this in, you can see that I'm just sort of typing these values. But when you define a new property, anytime you say Add Property-- let me hit Enter so I get my height. But anytime you hit Add Property, you get a pulldown menu. So if you don't feel like typing these properties in that we're talking about, you can add them that way as well. Okay, so we have a height of 300 pixels. We have a width of 300 pixels. Now, that has created a dramatic change in our paragraph and our container. Within our paragraph now, we see the 300x300 size of it.
We now begin to see some of the background color of the containing div tag. So, why don't we see all of it? Well, your paragraph is lining up to the top left-hand corner of the div tag itself. That's pretty standard in HTML. Child elements will align top left-hand corner to their parent elements, unless you do something to move them away from that position. So, our paragraph is just butting right up against the top left edge. However, we've defined a width for our paragraph, but we haven't defined a width for our containing div tag.
Because of that, the containing div tag is now expanding to fill its parent container, which in this case is the body tag. So whereas we've defined the width and height for the paragraph tag, we haven't defined one yet for the container. Its height is being determined by its contents, in this case, the paragraph. Its width is being defined by the body tag. Now let's add some box properties to our containing element and see how it affects our layout. So I'm going to go up to the #container ID selector. I'm going to add a property here. I want to go ahead and define a width for it as well.
So I'm going to go ahead and add a width. I'm going to give it a width of 300 pixels. That's exactly the same width we gave the paragraph. When I hit Return, you'll notice that our parent container disappears. Or does it? Well, we don't see the width anymore, but within Dreamweaver here, we are seeing something going on top and bottom. What's going on there? Well, not maybe what you think. I tell you what, go up to the document toolbar and turn on Live View. When we turn on Live View, oh, that's what we expect. We expected to see nothing, because now the parent container is exactly the same size as our paragraph.
So, how come Design View shows us a little bit of space up top and a little bit of space on the bottom? What Design View is doing is it's taking into consideration the default margins for your paragraph. Every element in a browser has default margins. With a paragraph, we get about 16 pixels worth of space above it, 23 pixels worth of space below it. So how come we wouldn't see that when a browser renders it? Well, that is a curious little property called vertical margin collapse.
When a vertical margin touches another vertical margin, they collapse to the highest of the two values. So if you have a paragraph with 20 pixels worth of margin below it, followed by another paragraph with 10 pixels worth of margin above it, you wouldn't get 30 pixels between the two of them. You'd only get 20, the higher of the two values. The reason for that is we don't want all of our elements to be double-spaced. We want them to be single-spaced. That's why vertical margins collapse. Now, it even gets more curious when you evolve a nested tag with the parent tag like we have here.
When you have a nested tag, if its margin touches the edge of the parent container with nothing in the way, no other content, no other padding, no other margins, then you get this margin collapse. That is something that has caused problems in more than one layout, I promise you that. Let's add a little bit something more to our container and see if it deals with this vertical margin collapse. I'm going to go up to the containing element. Let's add a little bit of padding to this. We're going to go ahead and add 20 pixels worth of padding all the way around.
Now when we switch over in Live View, I see 20 pixels worth of space to the left and to the right of the paragraph. At the top and bottom, well, now I'm seeing more than 20 pixels. Okay, what's going on there? Well, remember what I said earlier, vertical margins collapse when a nested element's margins go all the way up and touches the edge of its parent container. It can't do that anymore. Now we have 20 pixels of padding right here, which means the margin also touches that padding. margins and padding would add together.
Now we're getting probably about 36 to 40 pixels worth of space between the top of the paragraph and the top of its parent container. Layout can sometimes be a little tricky. So these are things that you have to understand when you're starting you use box model properties to control your elements. Okay, let's take this a little bit further. Let's examine how we can use our box model properties to achieve a very common technique in CSS-based layouts, and that is the centering of one element inside of another one. I'm going to make the container a tad bit wider. Let's see.
I'm going to go to my container and I'm going to go ahead and make these 600 pixels wide. So let's say we want to center that paragraph inside of its parent container. All we have to do to do that is to take advantage of margins. If I go back into my paragraph selector, I can add another property. The property I'm going to add here is margin. This time I'm going to use a little shorthand notation. A shorthand notation allows us to add just one margin value instead of having to add separate values for the top, bottom, right, and left.
So what I'm going to do is 0 and then a space. Then I'm going to type in the word "auto." Let's talk about what that's going to do. The zero, the first value, is going to represent the top and the bottom. So when you use margin and use two values like we're doing here, the first value applies to the top and the bottom of the element. The second value applies to the right and left. So what we're doing is we're taking that default margin that's causing that little bit of extra space up top and we're going to zero that out. Now the next thing we're doing is auto-margin for the left and right. Now what auto-margin basically does is it says okay, calculate the width of the parent, split it in two, put half of it on the right, half of it on the left.
What's that going to do for us? Well, let me hit Return. That's going to center the paragraph right in the middle of the containing element. Later on, we're going to use that technique to center our page layout on our page. Notice also what happened to the spacing above our paragraph. No longer are we seeing that extra spacing. We're just seeing those 20 pixels worth of padding that we put in. By zeroing out that top margin, we've eliminated that extra spacing that was causing that little awkward gap that we had up there. All right, let's add one more box model property. I'm going to go right down through our paragraph.
Let's add a border. So I'm going to add a property to this. I'm going to type in border. Once again, we'll do a shorthand notation here. By hitting Tab, that will move me over to the next dialog box. I'm going to type in 5 pixels. So we're going to do a fairly thick border here, solid, #fff, so white. So 5 pixels, solid, and white, and hit Return. So now we have a 5-pixel border all the way around our paragraph. We have auto-margins right and left. We're getting our space up top from the padding of the parent containing element.
Now, I don't like the way the text is butting right up against the paragraph. So let's go ahead and fix that while we're at it. So I'm going to go ahead and add a property to our paragraph. We'll add some padding. Let's add 20 pixels worth of padding all the way around our inner paragraph as well. So now we get the layout that we're looking for, sort of. We have 20 pixels worth of padding all the way around the interior of our paragraph. We have 5 pixels worth of a border. Then we have some auto-margins going over here on the right and the left-hand inside. So when we're factoring up the total width of our parent containing element, we have to take a lot into consideration here.
First, we have to take in the width of the containing element itself. Currently, that width is 600 pixels wide, but we also have to take a look at the padding inside of it as well. We have 20 pixels worth of padding and 600 pixels worth of width. So, 20 pixels on one side, and 20 pixels on the other side. That gives us a total width of 640 pixels. For the most part, your interior content won't disrupt that width unless it's going to equal more than that. If we were trying to calculate the size of our inner paragraph, we would see it's 300 pixels wide.
We have padding of 20 pixels, which would be 20 on the left, 20 on the right, so 340, total. Then our 5 pixels worth of border would equal 350. So in this case, the interior paragraph really isn't affecting the outer div tag. But those are things that you have to keep your eye on when you're doing and planning your layout. Often you're going to find yourself subtracting certain width values or adding certain values to it, just so you can achieve the proper layout. I want to illustrate what that can do to your layout here for just a moment. Let's say I go back up to our containing element and I tell it instead of being 600 pixels to go ahead and be 300 pixels. What happens? Well, that's what happens.
Notice that the inner paragraph no longer will fit. So even though the inner paragraph is told to be 300 pixels wide, it no longer fits because of the padding and the border that we have on it as well. So what happens is our elements overflow each other. That's really not what we want. So if we're going to try to figure out how to have exactly the same amount of space all the way around this, let's say 20 pixels all the way around our outer paragraph, we've got to factor out what the width of the paragraph is, including its padding and its borders, and add the proper amount of padding around our containing element.
Let me show you what I mean. So our nested paragraph is 300 pixels wide. We have 40 pixels worth of padding when you add up both sides. So that's 340 pixels. Then finally, we have 5 pixels worth of border. So it's another 10 pixels. So now we're up to 350 pixels. So we have 350 pixels from the side- to-side, but we also want 20 pixels on either side of it. So if I go up to my containing element, I can say all right, let's go ahead and be 350 pixels wide. And we have 20 pixels worth of padding on either side.
And we get exactly what we're looking for. So, now that we have a handle on our margins, borders, padding, height, and width, and how they can affect overall dimensions and relationships with other elements on the page, we can move on to talking about more layout-specific topics. Next up, we're going to be using the CSS float property for layout.
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.