XML technologies / CSS / Advanced websites with CSS / A three-column layout / The property float
(Excerpt from “CSS − Advanced websites with Cascading Stylesheets” by Rachel Andrew & Dan Shafer)
In Header layout we briefly introduced the property float to position header elements and float images so text wraps around them. Next, we will develop a multicolumn layout based on float. First, however, a simple example will deepen our understanding of how it works.
Our example document defines the same areas as our eventual production layout.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="floatexample.css" />
<title>Beispiel: Schwebende Spalten</title>
</head>
<body>
<div id="wrapper">
<div id="header">
<p>Seitenkopf</p>
</div>
<div id="main">
<div id="sidebar">
<p>Erste Seitenleiste</p>
</div>
<div id="sidebar2">
<p>Zweite Seitenleiste</p>
</div>
<div id="content">
<p>Inhaltsbereich</p>
</div>
</div>
<div id="footer">
<p>Fußleiste</p>
</div>
</div>
</body>
</html>You may first notice that the order of the elements div differs from the previous positioned layout: here, the two elements div for the sidebars precede the element div with ID #content. All page areas remain present. Before applying CSS, look at the browser’s display in the following figure:
Figure: The example without CSS.
Create a stylesheet floatexample.css and add these rules:
body {
margin: 0;
padding: 0;
font: small Helvetica, Arial, sans-serif;
}
#header {
margin: 0;
padding: 0;
}
#main {
margin-top: 10px;
}
#sidebar {
float: right;
width: 220px;
border: 1px solid black;
background-color: #dddddd;
}
#sidebar2 {
float: left;
width: 159px;
border: 1px solid green;
background-color: #99ff99;
}
#content {
border: 1px solid blue;
background-color: #9999ff;
margin: 0 240px 0 160px;
}
#footer {
clear: both;
border: 1px solid red;
background-color: #ff9999;
}Once attached, the stylesheet produces three bordered columns, a header and a footer, as in the next figure.
Figure: The page with its stylesheet.
This reflects our project’s main layout parts: a header, two sidebars, a main area expanding and shrinking with the window, and a footer. Add text to the columns and the footer stays beneath all three, below the longest, as the next figure shows.
Figure: The footer stays below the longest column.
How does it work?
In the previous chapter’s discussion of float, I explained that elements with float slide out of their normal context within the containing box. Content in that box without float wraps around them.
Our sidebars float to the main area’s right and left, with its content wrapping around them. To avoid that here, give the element div with ID #content wide margins extending to the page bottom, even when the sidebars are shorter than the main content.
The final piece is the property clear in the element div with ID #footer, ensuring the footer sits below every column without overlapping other layout parts. clear accepts the values left, right or both. Applying clear: both to the footer displays it only after the space occupied by both sidebars is free. Since the footer follows the main area in document flow, it also appears below that area.
Copyright © 2006 of the German translation, dpunkt.verlag GmbH
You may print the online version for your private use.
Otherwise, this chapter from “CSS − Anspruchsvolle Websites mit Cascading Stylesheets” 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.
dpunkt.verlag GmbH, Ringstraße 19, 69115 Heidelberg