XML technologies / CSS / CSS Cookbook / Page layouts / Fixed-width multicolumn layouts using positioning
(Excerpt from “CSS Kochbuch” by Christopher Schmitt)
Problem
You want to create a four-column layout with fixed column widths.
Solution
First enclose the content in elements div with attributes id that, for demonstration purposes, refer to their position on the page:
<div id="Kopfzeile">
[...]
</div>
<div id="linksAussen">
[...]
</div>
<div id="linksInnen">
[...]
</div>
[...]
<div id="rechtsInnen">
[...]
</div>
[...]
<div id="rechtsAussen">
[...]
</div>Then assign the property position for every column the value absolute. Use the properties left and top to position the columns, using pixels as units in this case:
#linksAussen {
position: absolute;
left: 5px;
width: 190px;
top: 44px;
background: #fff;
}
#linksInnen {
position: absolute;
left: 205px;
width: 190px;
top: 44px;
background: #fff;
text-align: justify;
border-width: 0;
}
#rechtsInnen {
position: absolute;
left: 405px;
width: 190px;
top: 44px;
background: #fff;
}
#rechtsAussen {
position: absolute;
left: 605px;
width: 190px;
top: 44px;
background: #fff;
}Discussion
Absolute length units for the properties left and top give the columns fixed widths. This is as simple as working with two or three columns, but more than four or five can quickly become impractical.
This layout works best with a header no more than 44 pixels high. Oversized text can break the entire layout, placing the header below the columns, because absolute positioning removes them from the normal document flow.
To avoid this, enclose the columns in an element div whose attribute id is set to Inhalt:
<div id="Kopfzeile">
[...]
</div>
<div id="Inhalt">
<div id="linksAussen">
[...]
</div>
<div id="linksInnen">
[...]
</div>
[...]
<div id="rechtsInnen">
[...]
</div>
[...]
<div id="rechtsAussen">
[...]
</div>
</div> <!-- Ende Inhalt -->The trick is then to use the CSS rule to define the enclosing element div (Inhalt) as relatively positioned:
#Inhalt {
position: relative;
}With this additional step, the header height no longer matters to the overall layout, which also improves scalability. The declaration top: 44px; can therefore be removed from every column.
See also
The recipe Fixed-width two-column layouts and the recipe Fixed-width multicolumn layouts using floats.
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