Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Before we can begin positioning elements, we need to understand what the box model is and how it works. Every element, even in-line level elements, are considered to be contained within their box. So when you are a laying a page out, if you want a sidebar, for example, and the sidebar is going to be of a specific width and maybe it's going to have a background graphic or a background color to it, and you don't want the elements butting right up against it, you can actually use these box model properties to control the width, the height, the distance of interior elements away from them, or from the edges, and those types of element properties. So if you look at the slide, at the very top of it you can see that we just have a paragraph and it says the paragraph, this is element content. Well, below that, this has been styled and I want you to just for a moment imagine that the pink and the green weren't there and it is just the yellow box with a border around it.
Essentially, the box model properties consist of margins, borders, backgrounds, content, and padding. Now, the margin holds other elements away from this element and if you are used to using tables, you could use the metaphor of cell spacing, because margins push other elements away from them. So, it's not actually considered part of the width of the element, but it does help us to hold other elements away from this one. Now, the border will be at the very edge of the container's content. So, the border goes all the way around the edge and it can be as thick or as thin as you want. So, you can actually tell it to have no border, which is the default for almost every element, or you could specifically make sure it has a border. After that, the little green section you are seeing there is padding and you can specify exactly how wide you want that padding to be and that will hold off the content itself from the edge of the border. So, if you don't want your text butting right up against the edge of your box, you just give it a little bit of padding and it sort of pushes it away from the edge. Now, padding actually goes into defining the width of the element. So if you define a sidebar DIV tag, for example, and you make it 250 pixels wide and you give it 20 pixels worth of padding all the way around, you have actually got a sidebar that's 290 pixels wide, because 20 on one side, 20 on the other side, and 250 pixels in the middle. So you have to keep that in mind if you are going to be using padding.
Now we also have a background and that background can be either a color, or it could be an image, or it could be both if you would like. By default, most elements have a transparent background, but if you want it to have a solid yellow color as we have here, or a background graphic, or maybe even a combination of them, you could define a background property to control that. And then finally the content itself just floats inside that box once you have given it other dimensions. So understanding the box model is going to make it a lot easier when we layout our content, because we can take these Div tags or structuring, assign them width or even a height value, although we try not to do height, but you would assign them a width value and a background and a border and then position them exactly where you want them. So we will talk about positioning next, but right now we are going to go back in the Dreamweaver, and we are going to create a couple of elements on a page and then we are going to modify these box model properties so you can see how they effect the elements and the contents inside the elements. So, we are back in Dreamweaver and we are just going to start off with the blank file, so if you are following along with us, just go ahead and open up a new blank HTML document and we are just going to put a couple of elements on the page.
Because we are doing a box model, we could really use any block level element to do what we are about to do. Paragraphs or headings will work just fine. I am just going to go ahead and stick with the generic DIV element. So, I will just use a DIV and let us go ahead and I will give the first DIV an ID of boxOne. And I will just type in content for box1 and remember to close that DIV tag. And then open up another DIV tag and we will give that an ID of boxTwo and I will type in Content for box2. Remember to close that DIV tag as well.
So we will save this and I am just going to switch over to Design View. You could stay in screen mode if you want to, but we are pretty much done with editing the code. We just want to see how affecting the box model properties affects the objects that we have on the stage now. Now, going ahead and giving these IDs of boxOne and boxTwo, if you remember, we're back in Code view once again, it's boxOne, boxTwo. That allows us to create descendant selectors that are going to target just those elements. So, I am going to go ahead and go over to my CSS styles, and I will just go ahead and create a new CSS rule and I am going to go ahead-- and you will notice that it actually that it actually picked up the name of boxTwo, which is nice. I am going to change that, however, to boxOne. So, whatever element you are inside of, it tries to populate the selector type with that particular element.
So I am going to click Advanced, because we are doing an ID selector, #boxOne. So, whenever we are doing ID selectors, we have to put the hash mark or the pound symbol on the front there. And capitalization is very important. So, I used a lower case 'b' for the box and then 'One.' I used the Camel naming convention. So, I am going to stick with boxOne there and I'll say it's document only and I'll go and click OK. All right, now let's talk about some of our properties. Now first off we know that we can have margins and padding, and that sort of thing. So, from the Categories, I am going to go to our Box category.
And notice that we have settings here for Padding and we have for Margins and we can set individual values for top, right, bottom and left, or we can just set one padding value for all of them, or one margin value for the whole thing. What we are doing right now, that's exactly what I am going to do. I am going to go and do 10 pixels worth of padding and I will do 10 pixels worth of margin. I'll also go ahead and specify a width of say 200 pixels. Remember that in addition to that we have borders and backgrounds. So, I will click on the Border category and I will just do with the border the same all the way around and I will choose a solid border. And we will just do a one-pixel border and go ahead and choose any color that you would like.
I am going to go ahead and choose a bright red. I am also going to give this a background color, so we can see the actual element itself. So I am going to click on Background and for my background color, I will go ahead and just click that and I will choose sort of a lighter color, maybe a light yellow, and I will click OK. Notice exactly what happened to boxOne and I will expand our styles out here in the CSS Styles palette box so we can actually see this and I'll click on the #boxOne style so that we can see the properties for that. And I will just have to expand my CSS Styles palette out a little bit more in order to do that, so you may need to resize yours as well. So, we can see that it has got our background color, it has a border around it, and we have margins and padding now. The padding gives us the space just to the left of the C, all the way to the edge of the border of itself. The background color knows it is extending all the way through background of the object all the way to the border. Border gives us that nice, red border around it. Notice that we have a little bit of distance now between boxOne and boxTwo, and the reason we have that distance is because we also gave it 10 pixels worth of margin and we are going to find out something very important about margin here in just a second. Let's go ahead and create our second rule, and that one will be boxTwo. So I am just going to change boxOne to boxTwo, and click OK. Now I could go ahead and give it the exact same value, but where would the fun be in that? Let's change things up a little bit. I will click on Background, and just so we can easily identify it, I am going to give it a separate background color, maybe a light lavender or something like that. Just keep it light. And I will click on the Border category, and once again, I am just going to go ahead and give it a solid border of one pixel, and again, I will just choose a darker color this time around.
Now, I am going to click on my Box category here and I am going to go ahead and set my Width to 400 pixels. So, this is going to be twice as wide. For margin, I will go ahead and stick with 10 pixels worth of margin, and for padding, I am going to go ahead and do 20 pixels worth of padding. I am going to click OK. The second box restyles as well. Now, let's point out the obvious. We have got our background color, we have got our border, and we have got twice the distance between the content and the box number 2, than we do for the first box and that's because we are using 20 pixels worth of padding, rather than 10 pixels. Notice also that our 20 pixels worth of padding is going to calculate into the final width of our content. It's when we start looking at the margins that it gets kind of interesting. We have got 10 pixels of margin all the way around our first element, and we have 10 pixels of margin all the way around the second element. So, you would obviously assume that you are going to get 20 pixels of distance between the two of them, but that's not what happens. You will notice that we still only have 10 pixels worth of padding between the two of them, and that's because of a very important concept of margin collapse. Vertical margins collapse when they meet each other.
So, what you are looking at is you are looking at one having 10 pixels worth of margin, the other having 10 pixels worth of margin, they collapse down, and it accepts the higher value of the two. And since they are both the same, we simply get 10 pixels worth of margin around them. Notice if I go to boxTwo and I increase the margin to 20 pixels, and I can just change the value right there in the CSS Styles palette. Notice that now I actually get 20 pixels away from the edge of the stage, or the screen, but I also get 20 pixels away from the top element. And we don't get 30; we get 20 because vertical margins collapse.
Horizontal margins do not. Now, vertical margin collapse may seem sort of confusing at first and you may kind of wonder why that is, but think about all the default elements that you have in a regular HTML page. You have paragraphs, you have headings, you have tables, you have forms, all those block level elements, and everything a browser gives them a default margin value. So, your paragraphs come with a specific margin value on them. If the vertical margins didn't collapse, your paragraph spacing would actually be twice what it was normally. So, having them collapse actually helps your pages format better, but when you are laying your page out, you have got to keep that in mind.
So now we have reviewed the box model. In our next movie, we'll look at controlling the positioning of our elements, using both absolute and relative positioning.
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.