XML technologies / CSS / CSS Cookbook / Page layouts / Flexible-width multicolumn layouts using positioning
(Excerpt from “CSS Kochbuch” by Christopher Schmitt)
Problem
Create a four-column layout whose column widths adapt to the browser window (see the following figure).
Figure: A four-column layout with percentage widths.
Solution
First wrap the content in elements div. Their attributes id indicate the elements’ positions on the page for demonstration purposes (see the next figure):
<div id="Kopfzeile">
[...]
</div>
<div id="linksAussen">
[...]
</div>
<div id="linksInnen">
[...]
</div>
[...]
<div id="rechtsInnen">
[...]
</div>
[...]
<div id="rechtsAussen">
[...]
</div>Figure: The default content display.
Define the CSS rules with the property position set to absolute. Position the columns with the properties left and top:
#linksAussen {
position: absolute;
left: 1%;
width: 20%;
top: 4em;
background: #fff;
}
#linksInnen {
position: absolute;
left: 22%;
width: 28%;
top: 4em;
background: #fff;
text-align: justify;
border-width: 0;
}
#rechtsInnen {
position: absolute;
left: 51%;
width: 28%;
top: 4em;
background: #fff;
}
#rechtsAussen {
position: absolute;
left: 80%;
width: 19%;
top: 4em;
background: #fff;
}Discussion
Giving the property position of an element the value absolute removes it entirely from document flow. An element arranged with the property float can have other elements flow around it. If an element’s position is absolute, it is treated like a ghost.
By default, absolutely positioned elements sit in the top-left corner of the nearest ancestor or the initial containing block. In other words, to position a child whose position is absolute within its parent, first give the parent the value absolute or relative (but not static!). Other document elements can easily cause overlaps (see the next figure).
Figure: An image and absolutely positioned text overlap.
Avoid this by using the four CSS properties top, left, bottom and right to move the element anywhere in the document. Use percentages so column widths adapt when the browser window changes.
Also use percentages for the property left to specify the distance from the viewport’s left edge. For the property top, use em units to leave room for the heading. If the heading is an image, use pixels instead to reserve enough space for it.
This method offers great positioning freedom, but absolute positioning has drawbacks. Placing columns side by side is easy; placing a footer beneath them can be difficult if their exact heights are unknown.
If you add horizontal padding and borders to elements div, account for the box model bug in Internet Explorer for Windows through 5.5, and give IE its own width (see the recipe Special values for Internet Explorer 5.x for Windows). The easiest way is to use conditional comments (see the recipe Conditional comments).
See also
the CSS 2.1 specification for positioning elements with the value
absoluteandmore information on containing blocks.
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