Viewers: in countries Watching now:
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.
Let's take the techniques that we used in our last movie a little bit further by creating three-column layouts. Now, although the source order doesn't matter that much when creating two-column layouts, it does come into play a little bit when considering different techniques when building three-column layouts. So as with our two-column layouts, we're going to take a look at three different ways to create roughly the same layout. So I have three files open here, the 3col_v1.htm, 3col_v2.htm, and 3col_v3.htm, and you can find those in the 03_07 directory.
Now, we're going to start with our version one first. Now, I just want to go down and show you the page structure really quickly here. So we have a header, just like we did last time, and then we have three sections. Notice that one just follows the other one, column1, column2, and column3, and then we have our footer where our copyright stuff goes. So just to show you what that looks like in normal document flow, we just have these three sections following one right after the other. So I'm going to go back up into my code, and the first thing I want to is just sort of explain the math that we're faced with here.
The overall width of our page is 960, and what I want to have is a gap of 20 pixels' worth of space between my columns. That leaves us with 940 pixels' worth of space for the columns themselves. If I divide that by 3, I get 306.6666666, and it just keeps going. Now, since we're using 20 pixels' worth of padding on both the left and right of each column to hold off the interior contents, that actually leaves us with 266.6666666 for each column.
Let's go to our column1 selector. What I'm going to do here is I'm just going to float column1 to the left. I'm going to give it that padding that we just talked about, 20 pixels' worth of padding, and then for width, we're going to go ahead and do 266.6 pixels. Now, browsers can handle a lot more decimals than that, so if you do complicated math and you get it out to, say, five or six digits out, your browsers can handle that. So the only reason I'm doing it right here is just for brevity's sake, if you will.
And then finally, I'm going to do a margin-right here of 20 pixels. So that's the first one. Now, I'm just going to go ahead and copy these properties, the ones that I just entered in, because I'm going to do the exact same properties for column2 and the exact same properties for column3, except column3 doesn't need the margin to the right. Now the margin to the right really is only giving us spacing here on the first two columns. So if I save this, go back into my browser, and refresh my page, you can see, there is our three-column layout.
Now, take a look. We have a little bit of a gap right there. The reason for that gap: all of our values are actually working out to 959.8 pixels. And this is one of the things that happens when you do float: left for all three columns, even if you're doing like a photo gallery and you float all your images to the left, a lot of times you're going to see just really weird little spacing issues like that. So let me show you a quick and easy way to fix this. I'm going to go to my column3 and I'm going to change it from float: left to float: right. And then what I'm going to do is I'm going to go up to column2 and I'm going to remove that margin-right for spacing.
I don't need that anymore, because the widths of the two columns are going to give me the space, and remember, one is floating to the right and the other one is floating to the left. And by the way, notice that the footer has clearing: both already applied to it, so I saved us a little time there. So if I save this and preview that, now that snugs right up against that. And this gap happens to be 0.2 pixels off from this gap, but I don't think most people are going to notice that. So that works. Here we have a three-column layout. We're floating the first two columns to the left. We're floating the third column to the right, controlling the gap between the first two through a margin, controlling the gap between the second two there through just the available space leftover after the column widths have been calculated.
Let's go in and do our second version now. Source order does come into play in this one, and I'm going to scroll down and show you the source order and how it differs. Our first column says, "This is where the really important stuff goes." Notice this time the second column says, "I have to be the far right column." So in terms of the way that we're doing this particular layout, this one has to be the far-right column. And then we have our third column which says, "I have to be the middle column." And all that has to do with the fact that I'm going to be floating column1 to the left, column2 to the right, allowing column3 to come up underneath them, and then control the width of column3 through the use of margins.
So it's kind of a take on the very first two-column layout we did, but this time instead of having a column on just one side, we're going to have columns on both sides. It will probably get a little clearer as we do it. So I'm going to go up to column number 1, and what we're going to do for that is we're going to float this to the left, we're going to give it a padding of 20 pixels, and we're going to give it a width of 266 pixels. I'm sorry, I'm tired of dealing with all those decimals. For the second column, column2 here, we're going to float this one to the right, we're going to give it padding of 20 pixels as well, and again, we're going to give it a width of 266 pixels too.
Now, if I save this and if I just preview this in the browser right now, you can see what's happening here: the first column is floating to the left; the second column is floating to the right; and then the third column is coming up underneath both of them. You can even see right here the height doesn't match anymore because it's just coming up between them. Now, our footer is clearing, so we don't need to really worry about that. So what we're going to do with this middle column is we're going to set its height equal to the other two, and we're going to control the width of it through margins, so we're going to do an equal amount margins on both sides. So I'm going to come into column3, and in column3 I'm going to go ahead and do a padding of 20 pixels, same as the other two; I'm going to go ahead and define its width same as the other two, 266 pixels; but for margin, here I'm going to do a technique that we learned a little bit earlier.
I'm going to do a margin of 0 and then auto for left and right. If you remember what that does is it basically says, okay, how much space is left to my right, how much space is left to my left? Go ahead and even those values out, which really centers the content. So this is going to center this 266-pixel column in the middle of the two other 266 columns. So if I save this and preview it, there's our spacing. The margins on both sides are centering this column in the page. It's coming up and floating up underneath these two floated elements, and then our footer is clearing.
So there's another three-column layout. It looks very similar to our first one, except for the fact that the source order is different, and in the second layout the source order matters. Okay. Let's do our final version, so I'm going to go to 3col_v3. We're going to take a look at creating a three-column layout when the structure of the page is may be a little bit more complex. Now, oftentimes when you have multiple columns, certain columns relate to each other while other columns don't. So in this case, if I scroll down and look at the code, I can see that the first two columns are actually contained with an article.
Now, this is very, very common. You might have a main content section where you have two columns that sort of relate to each other and then you might have a sidebar that you still want to form a three-column layout, but these guys sort of belong together. In order to do this, we're actually going to create a two-column layout for the article in the aside and then add in two additional columns inside the article. So you can sort of nest these layouts one inside of each other to create more complex pages. We're going to scroll up, and we're going to need to add a couple of selectors here.
So after heading1, I'm going to come in and do my article. So basically right now I'm just kind of doing the two-column layout. For article, I'm going to float that to the left, I'm going to give it a width of 634 pixels, and then I'm going to follow that with the aside. And with the aside, I'm going to float that to the right, give it a width of 266 pixels, give it a height of 500 pixels. Now, normally, you want the content to form the height, but in this case we're just putting a fake height in there so we can really kind of see the layout.
And then finally, padding of 20 pixels and a background of rgb(173, 169, 130). Okay. If I save this, and I test that in my browser, here is my article-- it's over here in the left-hand side--and here's my aside sidebar. Now I need to form the two columns inside that article. And really, we already know how to do that, because we've done it several times. So underneath the aside, I'm going to create a .col1 selector.
And inside that, I'm going to go ahead and assign it a background color. So that's going to be rgb(237, 228, 214). Underneath that, we're going to go ahead and give it the same fake height of 500 pixels. We're going to float this to the left. Now, remember, it's floating inside of our article. We're going to go ahead and give it a padding of 20 pixels, and we're going to give it a width of 266 pixels. Really the same measurements we've been using for all of our columns so far.
col2 is going to be done in a very similar fashion. In this case, I'm going to go ahead and give this a background of rgb(219, 126, 64). I'm going to give it a height of 500 pixels, just the same as the other ones. And for this one, I'm going to float it to the right. Now, remember, its rightmost edge is going to go up against the rightmost edge of the article, which we've already given that a defined width, so this is going to not butt it up against the other column on the right-hand side, It's going to give us the space between that.
and then I'm going to go ahead and give it a padding of 20 pixels, and a width, just like the other ones, 266 pixels. So if I save that and preview it, what do you know? There we go. So now we have a three-column layout, three versions of them. These two look almost exactly the same. These two look a little different, simply because the source order changes. So when you get to doing a three-column layout, they're a little bit more complex. Typically, source order does matter and you're going to have certain things grouped together, but you still have identical three-column layouts using three different techniques.
These two columns are grouped within an article which is floated to the left. We have an aside that's floated to the right. In this one, we float one column to the right, another column to the left, and allow the middle column to have some margins which define it, and it gets to just float up in the middle of the two. And then finally, our initial layout, if you remember this one, we have one floating to the left, another one floating to the left, and we could have this one floating to the left, but in order to make it a little bit easier to get it snug up against the edge of this, we're floating this one to the right. I'm trying to stress the fact that if you just master floating and clearing, you can pretty much create the basic structure of any layout that you can dream up.
Now, over time you're going to find that you begin to develop strategies around page structure and layout techniques that work with your projects. Just make sure, as you begin to create your own layouts and your own techniques, that you're focusing on writing the most efficient code possible and creating layouts that give you the most flexibility in presenting your content the way you want.
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.