2 Column CSS Layout: Fixed Width And Centered

One of the more common css layout patterns you’ll come across is the fixed width 2 column layout. It’s relatively easy to code and understand, though it sometimes trips up developers new to css layouts.

A few weeks ago I promised to offer boilerplate code for some common css layout patterns. This is the first of that boilerplate code and I’ll continue to offer more in the coming weeks.

If you prefer to skip the explanation and get right to the code you can view the source of the demo. Note that I’ve added some extra css just to make the demo more presentable.

2 column css layout with sidebar on right

The HTML

The html for this layout is rather simple as you can see below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<div id="container">
    <div id="header">
        header content here
    </div>
    
    <div id="content">
        main content here
    </div>
    
    <div id="sidebar">
        sidebar content here
    </div>
    
    <div id="footer">
        footer content here
    </div>
</div>

We have a single div for each of the 4 main areas of the layout — header, content, sidebar, and footer. All four divs are wrapped inside a container div, which we’ll use to center and set the width of the page.

One thing to note is the structure of the html above follows the structure of the page itself. The header comes first in both and the footer comes last. The content area div is before the sidebar.

Later in this post I’ll offer two ways to change the layout above so the sidebar is to the left of the content. One of those ways is as simple as changing a single word in the css.

The CSS

If you’ve already viewed the source code of the demo you’ll note the css like the html is rather simple. In fact most of that css has been added solely to make the demo a little more presentable.

Here’s all the css you need for this layout minus the stuff to make the demo more presentable.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#container {
    width:960px;
    margin:20px auto;
}

#content {
    float:left;
    width:620px;
}

#sidebar {
    float:left;
    width:340px;
}

#footer {
    clear:both;
}

If you combine this css with the html above you’ll have a 2 column layout that’s 960px wide. Let’s walk through the css one div at a time.

#container — We’re using the container div so we can set the overall width of the layout (960px) and then center the layout on the page.

The 20px top and bottom margin is arbitrary. I added it to give the demo a little room to breathe. The magic is done by setting auto on the left and right margins, which when combined with an explicit width, centers the layout on the page

Some people prefer not to use a container div and instead use the body tag as the container. I like using a separate container div, but either will work. If you’d rather not include the container div just remove it from the html above and move the css I show here from #container to body.

#header — No css is needed for the header div. By default its width will be 100% of it’s container and by default it will position itself as far to the top and left as it can.

These defaults are exactly what we want so there’s no need to add more. A big part of understanding css layouts is realizing you often don’t need to do anything.

#content — The important css is the float: left. It isn’t needed to have the browser render the div where we want, however floating the content is necessary to have the sidebar div sit next to it.

The width is arbitrary. You can set it to be anything you want as long as the sum of the content and sidebar widths is less than or equal to the width of the container div.

#sidebar — Like the container, the sidebar is floated left, which is typically how I code this layout in practice. You could also float the sidebar to the right. Which you use is personal preference.

I’m unaware of any advantage or disadvantage to either approach. I usually float both to the left, though it’s simply because that’s how I first did it and now it’s become habit.

As with the content div the width here is arbitrary, except as noted above.

#footer — All we need to do with the footer is clear the two floated divs to ensure it sits below them. Since both content and sidebar were floated to the left we could have used clear: left instead of clear: both, but using clear: both is more flexible as we’ll see below.

Like the header the width of the footer will be 100% of the container which is what we want.

The rest of the css I’ve used in the demo is simply to make it more presentable.

css box model

Floated Elements

If you don’t have experience using floats you may be wondering why the above works. It’s a little much to explain in a few short sentences so I’ll direct you to a couple of others posts I’ve written in a moment.

The gist is that every element in an html document is defined by a rectangular box. There are two types of boxes, block and inline.

Divs are block level boxes, which are laid out vertically in the normal document flow. The box carves out a space forcing the next box to sit below it.

Floated elements are removed from the normal document flow and shifted as far left or right as possible. Other elements are allowed to flow around or next to them.

By floating both our content and sidebar divs above we break them out of the normal document flow and allow them to sit next to each other.

By clearing the footer div we get it to behave as though the the two floated divs are in the normal document flow and so the footer drops below them.

I realize that’s not much of an explanation. For a more in depth explanation of the css box model and floated elements you can read the 2 posts below.

2 column css layout with sidebar on left

Changing the Position of the Content and Sidebar

I mentioned above there were 2 ways to change this layout so the sidebar is on the left of the content instead of the right. Both are very simple to implement

The first is to change the order of the divs in the html. The sidebar being first will float to the left edge of the browser and the content will float as far to the left as it can go, which is against the right edge of the sidebar.

When I know in advance I want the sidebar to be on the left I generally place it first in the html.

The second way is even easier. Just float the content div to the right instead of to the left.

1
#content {float: right}

This is why we cleared our footer on both sides instead of just the left.

The above css shows off the power and flexibility of css layouts. By changing a single word in a single css file we can move columns from one side of our layout to the other. We’ll see the full power of this with the code for a 3 column layout I’ll share next week.

It’s not something you’ll do often, but think about how much time and effort it would take to move columns around with a table-based layout.

Equal Height Columns

I should point out that this layout doesn’t automatically create columns of equal height. The demo makes it appear like it does, but that’s only because I set the height of the content and sidebar to be the same to make the presentation nicer visually.

The height of each of the 4 main divs will will expand and contract based on what’s inside and most of the time that means they won’t have the same height.

Creating equal height columns is a bit trickier, though possible, and there are several methods for making it happen. I’ll save those for another post.

Summary

I hope you can see how easy it is to set up a 2 column fixed width layout. There’s really very little html and css needed. An important part of css layouts is understanding you need less css than you think.

The key to this layout is floating the 2 divs we want to sit next to each other and then clearing the first div that comes after them, which in our case is the footer.

From here it’s all about filling up each of the 4 main divs with content, though that will be specific to your project.

One thing I would suggest is taking this code and play around with it. Reorder the divs, remove a float, change a float from left to right, and try clearing the footer to the left or right only and see what happens.

You have 4 blocks to play with and a few simple lines of html and css to move them around. The more you play, the better you’ll understand how and why this all works and the easier it will be for you to create more complex layouts like the 3 column layout I’ll offer next week.

Have fun.

Download a free sample from my book, Design Fundamentals.

13 comments

    • Thanks Vivek. I’m not familiar with his layouts. Taking a quick look they don’t appear to offer equal column heights either. I promise to add a post about how to set those up here at some point.

  1. This is good common tutorial. But I shall request you to write some good tutorial on “How to Center Verically an image inside header”. I am telling you dynamic vertical placement inside any DIV, I have mentioned header, but can place it in footer too.

  2. Thank you for the concise tutorial. You write very clearly. I’m looking forward to reading your other articles.

  3. Steven, I like your style and could not have found your tutorial at a better time. I have been having difficulty transforming a table-based, 2-column centered template in to a CSS only site. Your solution worked perfectly. Thank you for taking the time to share with us all.

    Cheers!
    – Sean

  4. Thanks for the great tutorial, well written and easy to understand. Which means you really understand it!
    Simplicity is the way to go! Thanks also for the code samples.

  5. When i did what you said up there, nothing happened. No header, footer, content, sidebar at the right place. they are just like no css.
    I dont think it will work with few basic lines.

    • I’d have to see your code to know why it didn’t work. Could you place what you have online somewhere or email it to me?

      Or you can click on the first image at the top of the post. It’ll take you to a working example where you can view the source code and copy it.

      What I show in this post does work, though it doesn’t show things like the basic html and body tag or where and how to add the css.

Leave a Reply

Your email address will not be published.

css.php