XML technologies / CSS / CSS Cookbook / Page layouts / Fixed-width multicolumn layouts using floats

(Excerpt from “CSS Kochbuch” by Christopher Schmitt)

Problem

You want to create a three-column layout with fixed widths.

Solution

First enclose the content in elements div with attributes id that, for demonstration purposes, refer to their position on the page (see the next figure):

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

Default page appearance

Figure: Default page appearance.

Next, enclose the elements div for the main and left columns in another element div with its own attribute id set to enclose. Finally, enclose all elements div in an element div whose attribute id is set to frame.

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

Use an ID selector for the element div identified as frame to set the page’s overall width:

#frame {
 margin-left: 20px;
 width: 710px;
}

Then define the elements div for the columns, and the element div whose attribute id is set to enclose, as floats (see the next figure):

#Hauptspalte {
 float: right;
 width: 380px;
}
#linkeSpalte {
 float: left;
 width: 150px;
}
#rechteSpalte {
 float: right;
 width: 120px;
}
#enclose {
 float: left;
 width: 560px;
}
#Fusszeile {
 clear: both;
 padding-top: 1em;
 text-align: center;
}

Three-column layout with fixed column widths

Figure: Three-column layout with fixed column widths.

Discussion

Because column widths are specified in pixels, they cannot change. To display the columns, enclose the main and left columns in an additional element div. The additional element div with the attribute id set to enclose defines the left and middle columns as a float positioned on the left. Within this element div, the main column is positioned on the right and the left column on the left.

See also

The recipe Flexible multicolumn layouts with floats for creating a three-column layout with flexible column widths.

  

 

 

 

back to top