XML technologies / CSS / CSS Cookbook / Page layouts / Two-column layouts

(Excerpt from “CSS Kochbuch” by Christopher Schmitt)

Problem

Create two columns whose widths follow the browser window, as in the figure.

A CSS two-column layout

Figure: A CSS two-column layout.

Solution

Structure content with elements div. Give them attributes id with suitable values (see the figure).

Default page display

Figure: Default page display.

For demonstration, the attributes id use positional names. Normally prefer semantic values such as Hauptteil or Seitenleiste over positional names like linkeSpalte and rechteSpalte.

<div id="linkeSpalte">
 [...]
</div>
<div id="rechteSpalte">
 [...]
</div>
<div id="Fusszeile">
 [...]
</div>

In the stylesheet, use the property float to float the left column left and set its width to two-thirds of the document:

#linkeSpalte {
 float: left;
 width: 67%;
 background: #fff;
 margin-top: 0;
 margin-right: 1.67em;
 border-right: 1px solid black;
 padding-top: 0;
 padding-right: 1em;
 padding-bottom: 20px;
}

Internet Explorer for Windows through 5.5 needs its own width because of its box model bug (see Special values for Internet Explorer 5.x for Windows).

The right column flows around the left. Set top padding and margins on the right to 0 to align its top and first element with the left column:

#rechteSpalte {
 padding-left: 2em;
 margin-top: 0;
 padding-top: 0;
}
h1 {
 margin-top: 0;
 padding-top: 0;
}

To keep the footer at the bottom, set the property clear to both:

#Fusszeile {
 clear: both;
 padding-bottom: 1em;
 border-top: 1px solid #333;
 text-align: center;
}

Discussion

The property float behaves like the HTML attribute align, positioning images or other elements left or right while content wraps around them:

<img src="this.jpg" width="250" height="150" hspace="7" vspace="7" alt="example" align="right" />

A left- or right-aligned image allows content on the opposite side. A right-aligned image forces text left, as below. CSS floats offer similar behavior with finer control through borders, padding, margins and other properties.

Text wraps around a right-aligned image

Figure: Text wraps around a right-aligned image.

To keep the footer below both columns, set its property clear to both. This means the left column, defined as a float, must not be wrapped by footer content; the footer must appear below or after any floats.

One caveat: left-column content must be longer than right-column content. The left appears first, so the right wraps around it. Too much content in the nonfloating right column causes this anomaly:

Unwanted flow below the left column

Figure: Unwanted flow below the left column.

Adjust the right column’s padding or margin to reserve at least the left column’s width even below the float:

#Hauptspalte {
 width: 400px;
 /* Genug Innenabstand, um die linke Spalte auszugleichen */
 padding-left: 200px;
}
#Navigation {
 float: left;
 width: 175px;
}

Again, Internet Explorer for Windows through 5.5 needs its own width because of the box-model bug (see Special values for Internet Explorer 5.x for Windows).

To reverse columns (see the figure), first reverse their HTML order:

<div id="rechteSpalte">
 [...]
</div>
<div id="linkeSpalte">
 [...]
</div>
<div id="Fusszeile">
 [...]
</div>

Reversed columns

Figure: Reversed columns.

Then use these CSS rules:

#rechteSpalte {
 float: right;
 width: 67%;
 padding-bottom: 20px;
 padding-top: 0;
}
#linkeSpalte {
 width: 29%;
 padding-right: 1em;
 border-right: 1px solid black;
 padding-top: 0;
}

Warning:
This solution uses values for id such as rechteSpalte and linkeSpalte to show which column is being styled. This is usually poor practice: prefer semantic descriptions of the content. Values should describe what elements div contain, such as “Navigation” or “Werbung”.

See also

The recipe Fixed-width two-column layouts for fixed-width columns and Jeffrey Zeldman’s article “From Table Hacks to CSS Layout: A Web Designer’s Journal” for background on this solution.

  

 

 

 

back to top