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>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;
}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;
}Figure: The columns have been swapped.
See also
The recipe Two-column layouts for creating a two-column layout with flexible widths.
Copyright of the German edition © 2007 by O’Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “CSS Kochbuch” is subject to the same conditions as the bound edition: the work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstr. 81, 50670 Köln