This is a tutorial on how to use CSS to create a simple two column layout.

The layout consists of a header, a horizontal navigation bar, a main content column, a sidebar, and a footer. It is also horizontally centered in the browser window. A pretty basic layout, and not at all difficult to create with CSS once you know how to deal with the inevitable Internet Explorer bugs.

456 Berea Street Home | Developing with web standards index

1. Basic structure

First of all, we create the basic HTML structure:

<div id="wrap">
    <div id="header"></div>
    <div id="nav"></div>
    <div id="main"></div>
    <div id="sidebar"></div>
    <div id="footer"></div>
</div>

After that, we put some content in the different sections:

<div id="wrap">
    <div id="header"><h1>Document Heading</h1></div>
    <div id="nav">
        <ul>
            <li><a href="#">Option 1</a></li>
            <li><a href="#">Option 2</a></li>
            ...
        </ul>
    </div>
    <div id="main">
        <h2>Column 1</h2>
        <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit...</p>
    </div>
    <div id="sidebar">
        <h3>Column 2</h3>
        <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit...</p>
        <ul>
            <li><a href="#">Link 1</a></li>
            <li><a href="#">Link 2</a></li>
            ...
        </ul>
    </div>
    <div id="footer">
        <p>Footer</p>
    </div>
</div>

Now we have a completely unstyled XHTML document which is structured in a way that lets us use CSS to control its layout.

View step 1.

2. Adjust the body and html elements

To make the content reach the edges of the browser window, we set the margin and padding of the body and html elements to zero. We also specify colours for text and background.

body,
html {
    margin:0;
    padding:0;
    background:#a7a09a;
    color:#000;
    }

View step 2.

3. On to the main containers

After that it’s time to give the content area a width and center it horizontally. We do that by specifying the width and margins of the main container, #wrap. We also give it a background colour to make it show up on the page.

The method we use to center the content is based on the fact that when an element’s left and right margins are set to auto, they will share whatever is left when the element’s width has been subtracted from that of its container. In this case the width of #wrap will be subtracted from the width of the browser window. To avoid problems that can occur in some browsers when the window is narrower than #wrap We set the <body> element’s min-width to the same value as the width of #wrap.

Note: for this method to work in Internet Explorer (version 6 and later only), the document must use a DOCTYPE that forces IE to use its standards mode. Read Fix Your Site With the Right DOCTYPE! for more info.

body,
html {
    margin:0;
    padding:0;
    background:#a7a09a;
    color:#000;
    }
body {
    min-width:750px;
    }
#wrap {
    background:#99c;
    margin:0 auto;
    width:750px;
    }

After that, we give the different sections of the document different background colours to make them show up.

#header {
    background:#ddd;
    }
#nav {
    background:#c99;
    }
#main {
    background:#9c9;
    }
#sidebar {
    background:#c9c;
    }
#footer {
    background:#cc9;
    }

View step 3.

4. Place the columns side by side

To make the two columns (#main and #sidebar) display side by side we float them, one to the left and the other to the right. We also specify the widths of the columns.

#main {
    background:#9c9;
    float:left;
    width:500px;
    }
#sidebar {
    background:#c9c;
    float:right;
    width:250px;
    }

This will make #sidebar appear to the right of #main, but the footer is not where it should be.

View step 4.

5. Push the footer down

The footer doesn’t get pushed down to the bottom of the content because of the way float works. When you float an element, it is removed from the document flow and doesn’t push elements that follow it down. In this case #footer will start right below #sidebar.

To avoid this we use the clear property to tell the footer that it can’t have any elements next to it.

#footer {
    background:#cc9;
    clear:both;
    }

View step 5.

6. Fix the background colour

Now we can see that the shorter column doesn’t continue all the way down to the footer. To make it look like it does, we use the same background colour for #sidebar and #wrap.

#sidebar {
    background:#99c;
    float:right;
    width:250px;
    }

Also, if you take a look at this step in IE/Win you may notice that the background colour of the footer is pushing up beside the main content. That will be taken care of later.

View step 6.

If you don't know which column is going to be the tallest, and you want both columns to have different backgrounds and look like they are going all the way to the bottom, you need to use a workaround. There are several ways to achieve that effect:

7. Make the navigation bar horizontal

#nav contains a regular unordered list of links. Since we don’t want it to look like an unordered list we restyle it.

#nav ul{
    margin:0;
    padding:0;
    list-style:none;
    }
#nav li{
    display:inline;
    margin:0;
    padding:0;
    }

View step 7.

8. Adjust margins and make IE/Win cooperate

Almost done. Time to adjust the margin and padding of some elements to make the layout a little less cramped.

#header h1 {
    padding:5px;
    margin:0;
    }
#nav {
    background:#c99;
    padding:5px;
    }
#main h2, #main h3, #main p {
    padding:0 10px;
    }
#sidebar ul {
    margin-bottom:0;
    }
#sidebar h3, #sidebar p {
    padding:0 10px;
    }
#footer p {
    padding:5px;
    margin:0;
    }

And the final thing is to compensate for a float bug in IE/Win. If you look at step 6, you’ll notice that the footer is not always being pushed completely below #main. Scroll up and down a couple of times if you can’t see it immediately.

It is very noticeable in this demo because every major element has a different background colour and everything is really tight. If you have a bit of space between the main content and the sidebar this bug doesn’t occur. Making the sidebar narrower is one way of avoiding it. So let’s do that:

#sidebar {
    background:#99c;
    float:right;
    width:240px;
    }
#sidebar h3, #sidebar p {
    padding:0 10px 0 0;
    }

To compensate for the narrower sidebar we remove the left padding from the elements that are contained within it.

And we’re done. We have created a simple layout that can be used as a starter for more advanced layouts.

View the final layout.

Please don’t hesitate to contact me if you can suggest improvements to this method.