New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Three-column floated layouts

From: CSS: Page Layouts

Video: Three-column floated layouts

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.

Three-column floated layouts

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.

Show transcript

This video is part of

Image for CSS: Page Layouts
CSS: Page Layouts

71 video lessons · 40100 viewers

James Williamson
Author

 
Expand all | Collapse all
  1. 4m 20s
    1. Welcome
      54s
    2. How to use the exercise files
      3m 26s
  2. 1h 39m
    1. Box model review
      8m 47s
    2. Calculating element dimensions
      11m 11s
    3. Understanding margin collapse
      7m 59s
    4. Calculating em values
      7m 41s
    5. Calculating percentage values
      7m 51s
    6. Normal document flow
      13m 3s
    7. Controlling element display
      8m 53s
    8. Using CSS Resets
      7m 11s
    9. Fixed, fluid, and responsive layouts
      9m 9s
    10. CSS debugging tools
      6m 46s
    11. Using the Firebug Inspector and the WebKit Web Inspector
      11m 5s
  3. 53m 15s
    1. Page design workflow
      3m 6s
    2. Page design tools
      4m 56s
    3. Determining page structure
      7m 18s
    4. Creating image assets
      8m 58s
    5. Creating initial page structure
      7m 3s
    6. Adding meaning with classes and IDs
      5m 23s
    7. Structuring content with HTML5
      6m 6s
    8. Building internal structure
      10m 25s
  4. 1h 36m
    1. Floating elements
      7m 50s
    2. Clearing floats
      7m 28s
    3. Containing floats
      7m 50s
    4. Clearfix technique
      10m 38s
    5. Floating inline elements
      14m 34s
    6. Two-column floated layouts
      8m 17s
    7. Three-column floated layouts
      11m 30s
    8. Column height considerations
      7m 3s
    9. Creating equal-height columns
      10m 42s
    10. Floats: Lab
      5m 25s
    11. Floats: Solution
      5m 21s
  5. 51m 42s
    1. Relative positioning
      7m 59s
    2. Absolute positioning
      8m 59s
    3. Fixed positioning
      4m 23s
    4. Controlling stacking order
      8m 31s
    5. Clipping content
      8m 21s
    6. Controlling content overflow
      5m 38s
    7. Positioning elements: Lab
      3m 59s
    8. Positioning elements: Solution
      3m 52s
  6. 48m 46s
    1. Design considerations for fixed layouts
      3m 28s
    2. Establishing the layout grid
      7m 57s
    3. Defining column spacing
      9m 30s
    4. Applying the grid through CSS
      8m 56s
    5. Creating grid-based assets
      8m 26s
    6. Grid design resources
      6m 22s
    7. Building fixed layouts: Lab
      4m 7s
  7. 44m 35s
    1. Designing for flexible layouts
      2m 30s
    2. Calculating percentage values
      8m 45s
    3. Setting flexible width values
      6m 6s
    4. Making images flexible
      8m 10s
    5. Setting minimum and maximum widths
      7m 24s
    6. Building flexible layouts: Lab
      4m 53s
    7. Building flexible layouts: Solution
      6m 47s
  8. 49m 36s
    1. Responsive layout overview
      3m 49s
    2. Using media queries
      7m 16s
    3. Organizing styles
      8m 39s
    4. Making content responsive
      8m 33s
    5. Mobile design considerations
      7m 32s
    6. Building responsive layouts: Lab
      4m 23s
    7. Building responsive layouts: Solution
      9m 24s
  9. 1h 22m
    1. Creating multi-column text
      6m 36s
    2. Using borders to enhance design
      13m 59s
    3. Rounding corners
      6m 56s
    4. Adding drop shadows
      10m 35s
    5. Working with opacity
      6m 8s
    6. Utilizing the background property
      15m 5s
    7. Working with CSS sprites
      7m 58s
    8. Enhancing page design: Lab
      6m 22s
    9. Enhancing page design: Solution
      8m 38s
  10. 6m 25s
    1. Additional resources
      6m 25s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold

Are you sure you want to delete this note?

No

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.