Start learning with our library of video tutorials taught by experts. Get started
Viewed by members. in countries. members currently watching.
CSS: Page Layouts introduces basic layout concepts, gives advice on how to create properly structured HTML based on prototypes and mockups, and goes into critical page layout skills such as floats and positioning. Author James Williamson shows how to combine these techniques to create fixed, fluid, and responsive layouts. Designers are also shown how to enhance their pages through the creative use of CSS techniques like multi-column text, opacity, and the background property. Exercise files are included with this course.
One of the most important skills in CSS regarding page layout has nothing to do with positioning elements on the page. It is in fact the ability to accurately calculate an element's dimensions. Now on the surface that sounds like something that's pretty easy to do, but as we are going to see, it gets a little bit more complicated than maybe you'd think at first glance. So to illustrate this concept, we are going to be working with the dimensions.htm file, and you can find this one in the 01_02 directory, in the Chapter_01 folder. Okay, so if I take a real quick look at the structure of the page, you are going to see that it's very similar to the exercise file we just worked with.
We have two divs, an outer div and then an inner div with the class of box applied to it, and that has a paragraph inside of that. Our sort of default styles that we have going on: the outer div tag is now set to a width of 600px wide, it has a margin of zero for top and bottom, and then left and right is set to auto. Now what that does for us is when we set left and right to auto, it'll go ahead and just take that content and center it within whatever parent element it's floating inside of, which in this case is the body tag. And then we have a background color applied for our outer that's a light-blue color.
For inner box, the only thing that we have right now is just a background color. And for our paragraph, we have not only a background color that's going to allow us to sort of visually define the contents of the box, but we also have the height set to 100%, and that way no matter what happens with the inner box, the paragraph will sort of expand to fill the height of it as well. So the first thing we are going to do is we are going to go into our box here, and we are going to go ahead and define its width as being 300 pixels wide. Easy enough. I am going to save that, and let's just preview that in one of our browsers. Okay, so what we're seeing here is we are seeing the overall width of 600 pixels--that's our outer div tag--and then our inner div, or the box div tag, is 300px wide.
So we're getting exactly what we expect here. We are getting exactly half of that outer div. Okay, so I am going to go back into my box and let's add some more box model properties. On the next line I am going to go ahead and do padding of 50px. Now remember, padding and width are cumulative, so this is going to add 50 pixels worth of padding all the way around our box, so it's going to increase both our height and our width, but we are just going to focus on width. So what's happening here is we have 50 pixels' worth of the padding on the left-hand side, 50 pixels' worth of padding on the right-hand side, and then a width of 300px. That gives us a total width of our element now of 400 pixels.
So I am going to save this, refresh that in my browser, and that is indeed exactly what we are seeing. We are seeing the background color now of the box div tag. This is the background color of the paragraph, and then we can visualize that 50 pixels' worth of spacing here and 50 pixels' worth of spacing here, 300 pixels' worth of spacing here, and that it's taking up a total of 400 pixels' inside of that outer div tag. Awesome! Now one of the elements that people really often forget about when they are calculating the width of their elements or factoring how much space something can take up within the layout are borders, but borders are part of the box model and they do calculate to an element's size.
So if I go down on the next line and I type in border, and let's say we do 2 pixels of border solid, and then black, all right, so I am going to save that, if I preview that in my page, that does increase the overall size of my element. Now borders show up on the outside edge of the padding edge, so essentially 2 pixels have been added to the outer edge of my element. But because it's inside of this outer div tag, it is inside that. It doesn't hang over or overlap. It stays inside the content edge of its parent.
So essentially, now we have added 2 pixels to the left and 2 pixels to the right, assuming now we have a total width if 404 pixels. Okay, so how do margins play into that equation? It's confusing. Sometimes you will hear people say margins don't calculate to the overall width of an element; sometimes you will hear people say they do, and it becomes a really confusing thing. So let's take a look at what margins add to our element and sort of how they play into the overall size of our element. So if I go back into my code, if I go down onto another line within the box selector and this time I am just going to apply a margin to one side.
I am going to do margin-left. And we'll go ahead and do 50 pixels there, okay. So I am going to save this and again I am going to refresh this in my browser. Okay, so here is essentially what's happening. The actual width of my element really hasn't changed, but the amount of space that it takes up has changed. So we are pushing our element away, 50 pixels from the edge of its containing element, so the amount of space it needs within the layout to do this are now 454 pixels, so 404 pixels for the total width of the element and then the 50 pixels over here on the left-hand side.
So let's clarify the relationship between margins and their elements, in terms of size. The best way to think about it are there are two sorts of blocks of calculations that are used for an element. You have the containing block and the content block. Now the content block are all of the border, padding, and width of an element. So the width property, any padding, and border, they all go together to make up the actual content block of an element. The containing block are the margin edges. So in this case the containing block is 454 pixels or the content block is 404 pixels. May not seem like a big deal.
We certainly need to understand how much space something is taking up within the layout, so understanding the margins is extremely important. However, certain things happen when an element is considered overconstrained, so what does overconstrained mean? Well, overconstrained means an element no longer fits inside of its parent. So what happens when we start increasing the values of the size of our element and increase it to a point to where it just doesn't fit inside this 600 pixels anymore? Well, when something is overconstrained, your browser has to make a decision, or the user agent has to make a decision, about what to do, and often it will sacrifice margins and padding in order to make things fit.
There are other properties that we can use too to control this sort of overflow, if you will, and we'll talk a little bit about that in just a moment. But let's go ahead and make this overconstrained and see what happens with our margins. All right! So I am going to go back into my file, and I am going to ahead and change the width of our box now to 400 pixels. So that gives us a total of 504 pixels of width. Now remember we are inside of a div tag whose width is set is 600. So for the moment we still fit; however, what I am going to do is I am going to take margin-left, I am going to get rid of left, and I am just going to apply that as margin. So if you think about this, the content block is going to fit, because the content block is 504 pixels.
The containing block will not fit. The containing block, with 50 pixels' worth of space on either side, is now 604 pixels, whereas our outer div tag is now 600 pixels. All right! Everybody still with me? Cool! So save this. And I am going to update this in my browser. Now at first glance, it looks like everything is okay. It fits, right? Not entirely. What happens is because we have 50 pixels' worth of margin on either side, it's sort of centering, or floating, if you will, in the middle of our div tag, and it gives us the illusion that it fits. But the problem is--well, it's not really a problem, but what our browser has done is it essentially has ignored certain margin values.
It simply just throws out the margin values that would overlap on either side. Now, if you don't believe me, let's take this to a little bit more of an extreme. So I am going to go back into box, and let's say, I go in and say, okay, on the margin-right, go ahead and add 100 pixels' worth of space of there on the right-hand side. So if I save this and test it. Nothing happens. And 100 pixels, it's just thrown away, because the browser is saying, "No, I can't push it that far. It won't fit anymore. So I am just going to sort of ignore that, okay." Now what about the content block, what happens when the content block no longer fits? All right! So what I am going to do now is I am going to go in and remove all those margins, and then I am going to take my padding and I am going to change my padding to 98 pixels. Why 90 pixels? Well, remember we have borders of 2 pixels on either side, so that it's essentially giving us a combination of padding and border of 100 on either side. So we have 100 pixels' worth of padding and border on the left-hand side, 100 pixels' worth of padding and border on the right-hand side, 400 pixels' worth of width, that's going to give us an equal value to our outer div tag of 600 pixels.
So if I save this, and preview it, it fits precisely. You can't see the outer div tag anymore, but I promise you, it fits precisely. Okay, so what does happen if I make it a little bit wider? So if I increase the width to say 500 pixels and save that, what happens? Well, it's hard to see what's happening right now. So let me do something really quick so you can visualize what's happening here. I am going to go ahead and take my height here and move my height to say 800 pixels so you can see this. All right! So let's save that and let's preview it. All right! So you can see what's happening.
The inner div tag is simply overlapping, it's sort of spilling out of the outer div tag. So what happens when a content block no longer fits is we have something that we call overflow. Now you can control overflow by using the overflow property. If I go back into my outer div tag, for example, and I am just going to type in overflow. And there are a couple of values you can use here. You can use hidden, and if I use hidden, it simply crops off any content block that would overflow the element. I can do visible.
I can do scroll. I can do auto. Now auto and scroll are very similar. Auto basically just says, if it does overflow, go ahead and give me scrollbars, and you can see that at the bottom in the element here I have scrollbars that let me scroll through this. Harder to see, because of the height of the element, but I think you get the idea. All right! So that's the overflow property. Now I am going to get rid of the overflow property here. And I want to show you one more thing, in terms of what happens when we have an overconstrained element, so we've got one more thing to take a look at here. All right! I am going to go back to my box and I am going to change the width of the box back to 400 pixels.
Now I am going to go up to my outer element here and I am going to get rid of this height property so that we are not confused by that, and what I am going to do is I am going to set my padding on the outer div tag to 50 pixels. So that's just affecting the overall width of that element as well. So now that the outer div tag is now 700 pixels wide, obviously. All right! Now with my box width being 400 pixels, of course I have 100 pixels on either side, so that's 600px, right? So if I save this and preview it, we can see there is the additional 50 pixels' worth of padding for our outer div tag.
Now keep an eye on that padding. If I come back in the box and I increase the total value of box here, let's say I increase its width to 450px and save that and preview it, notice what happens. It expands to fill that empty space. The padding on this side is basically sacrificed. So essentially, it fills this in and it says, "Okay, I have 50 pixels' worth of padding here and I have this huge total width over here," and this padding is basically sacrificed. It's actually still there and other elements would respond to it, but because this is now overflowing, it overflows that padding value.
Now the reason I'm mentioning this is because it's really important for you to understand what happens if you don't really plan your elements properly. If you are not carefully considering the dimensions of your outer elements and the dimensions of your inner elements, then your layout is either going to break or behave unexpectedly if elements become overconstrained. So obviously the more complex the page gets, the harder it can be to accurately calculate an element's width. This can cause layouts to break, spacing to be uneven, and content to overflow element boundaries. That's why it's so important not to lose sight of how even small changes to your elements, like increasing your border by 1 pixel, can make a big difference in your entire layout.
There are currently no FAQs about CSS: Page Layouts.
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.