XML technologies / CSS / Advanced websites with CSS / A three-column layout / Making all columns equally tall

(Excerpt from “CSS − Advanced websites with Cascading Stylesheets” by Rachel Andrew & Dan Shafer)

Our previous layout does not fully match the graphic design: columns should be equally tall regardless of content. Some layouts do not need this, but here is a solution when they do.

Fake the columns with background images. Dan Cederholm’s article “Faux Columns” describes this technique: images on elements div create apparent columns. Use it twice: first on the element div with ID #main for the right column, then on a new element div inside #main for the left.

Create an image with a blue right-hand bar matching the sidebar, a cream strip matching the page and a fine one-pixel light-blue line, as shown.

The right sidebar’s background

Figure: The right sidebar’s background.

In CSS, find the selector #main and add the background rules. The rule background-repeat: repeat-y tiles vertically, while the property background-position: top right anchors the background right.

#main {
  margin-top: 10px;
  width: 100%;
  background-image: url(img/sidebarbg.gif);
  background-position: top right;
  background-repeat: repeat-y;
}

Make the element div with ID #main as tall as the tallest column because it holds the background. Currently, #main is only guaranteed to match the element div with ID #content, since floats do not affect its height, as below.

Floats do not affect parent height

Figure: Floats do not affect parent height.

Add an empty element div with the rule clear: both immediately before the closing tag <div> of #main. This empty element div appears below all columns, like the element div with ID footer. Being inside #main, it makes #main as tall as the tallest column. The next diagram shows this.

Parent height adjusted with clear

Figure: Parent height adjusted with the property clear.

Add these HTML lines:

  </div> <!-- content -->
  <div id="clearone"> </div>
</div> <!-- main -->

Add this stylesheet rule:

#clearone {
  clear: both;
  height: 1px;
}

The right sidebar now reaches the longest column’s bottom, as shown.

A background creates the right column

Figure: A background creates the right column.

For the left, add a new element div for the background. Insert an element div named id="main2":

<div id="main">
  <div id="main2">
    <div id="sidebar">
     ...
    <div id="clearone"> </div>
  </div> <!-- main2 -->
</div> <!-- main -->

Create its background, 161 pixels wide and four high, with a one-pixel light-blue border on both sides.

Define rules for #main2, assigning the image at the bottom left of #main2 and a white background color.

#main2 {
  margin-top: 10px;
  background-image: url(img/leftbg.gif);
  background-position: bottom left;
  background-repeat: repeat-y;
}

The full-height faux columns are nearly finished; only bottom edges need adjustment.

Faux columns nearly finished

Figure: Faux columns nearly finished.

Remove the bottom border of the element div with ID #content by replacing the property border with borders on the other three sides:

#content {
  margin: 0 240px 0 160px;
  border-top: 1px solid #b9d2e3;
  border-right: 1px solid #b9d2e3;
  border-left: 1px solid #b9d2e3;
  background-color: white;
  color: black;
}

Remove the left sidebar’s bottom border by deleting border-bottom:

#sidebar2 {
  float: left;
  width: 159px;
  border-top: 1px solid #b9d2e3;
  border-left: 1px solid #b9d2e3;
  background-color: white;
  color: black;
  margin: 0;
  padding: 0;
}

The columns now appear full height, as below.

All columns equally tall

Figure: All columns equally tall.

Creating exactly fitting backgrounds takes image-editor tinkering, but once ready this solution works very well.

   

 

 

 

back to top