XML technologies / CSS / CSS Cookbook / Page layouts / Fixed-width two-column layouts

(Excerpt from “CSS Kochbuch” by Christopher Schmitt)

Problem

Create a two-column layout with fixed column widths.

Solution

First enclose the content in elements div. These have attributes id that, for demonstration purposes, refer to the elements’ positions within the page (see the next figure):

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

The page’s default appearance

Figure: The page’s default appearance.

Now use the property float and specify the left column’s width using an absolute length unit instead of a percentage. Also specify the document’s own width using a length unit (see the next figure):

body {
 margin: 0;
 padding: 0;
 font-family: Georgia, Times, "Times New Roman", serif;
 color: black;
 width: 600px;
 border-right: 1px solid black;
}
#Kopfzeile {
 background-color: #666;
 border-bottom: 1px solid #333;
}
#linkeSpalte {
 float: left;
 width: 160px;
 margin-left: 10px;
 padding-top: 1em;
}
#rechteSpalte {
 padding-top: 1em;
 margin: 0 2em 0 200px;
}
#Fusszeile {
 clear: both;
 background-color: #ccc;
 padding-bottom: 1em;
 border-top: 1px solid #333;
 padding-left: 200px;
}

A two-column layout created with CSS

Figure: The two-column layout created with CSS.

Discussion

By default, block elements span the full width of their containing element. If the browser window is narrow, the block elements’ widths adjust accordingly and their text wraps.

Using absolute length units instead of percentages, however, preserves the column widths. Even if the browser window’s width changes, the column widths remain unchanged.

To give only the navigation column on the left a fixed width while keeping the main column on the right flexible, simply remove the property width from the element body.

To swap the columns, as in the next figure, modify the HTML as shown here:

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

Warning:
This solution uses id values such as rechteSpalte and linkeSpalte to emphasize which column is being styled. This approach is not generally considered particularly useful, however. It is better to use a semantic description of the content. The values should therefore refer to the actual content of the elements div, such as “navigation” or “advertising.”

You can then use the following CSS rules:

#Kopfzeile {
 background-color: #666;
 border-bottom: 1px solid #333;
}
#linkeSpalte {
 width: 340px;
 margin-left: 10px;
 margin-top: 1em;
}
#rechteSpalte {
 float: right;
 width: 200px;
}
#Fusszeile {
 clear: both;
 background-color: #ccc;
 padding-bottom: 1em;
 border-top: 1px solid #333;
 padding-left: 10px;
}

The columns have been swapped

Figure: The columns have been swapped.

See also

The recipe Two-column layouts for creating a two-column layout with flexible widths.

  

 

 

 

back to top