XML technologies / CSS / CSS Cookbook / Page layouts / Displaying floated columns in any order

(Excerpt from “CSS Kochbuch” by Christopher Schmitt)

Problem

You want layout columns to appear in any order.

Solution

Use this code for the basic three-column layout with flexible widths. We will need the extra markup later in the solution:

<div id="ContainerAussen">
  <div id="Container">
    <div id="Inhalt" class="Spalte">
      <div class="eingepackt">
        [...]
      </div>
    </div> <!-- /ENDE #Inhalt -->
    <div id="Navigation" class="Spalte">
      <div class="eingepackt">
        [...]
      </div>
    </div> <!-- /ENDE #Navigation -->
    <div id="Informationen" class="Spalte">
      <div class="eingepackt">
        [...]
      </div>
    </div> <!-- /ENDE #Informationen -->
  </div> <!-- /ENDE #Container -->
</div> <!-- /ENDE #ContainerAussen -->
<div id="Copyright">
  [...]
</div>

Define these CSS rules:

.Spalte {
 float: left;
 border: 1px solid green;
}

#Inhalt {
 margin-left: 20%;
 width: 60%;
}

#Navigation {
 margin-left: -80%;
 width: 20%;
}

#Informationen {
 width: 19%;
}

/* Anpassungen für IEx \*/
* html .Spalte {
 display: inline;
}

* html #Navigation li {
 height: 1%;
}
/**/

This creates the basic layout below: two narrower outer columns and an inner column, all with flexible widths.

Basic three-column layout with flexible widths

Figure: Basic three-column layout with flexible widths.

Extend this simple foundation with the following rules to obtain the next figure:

body {
 font: normal 62.5%/1.7 Verdana, Geneva, Helvetica, Arial, sans-serif;
 margin: 0;
 padding: 0;
}
#Container:after {
 clear: both;
 content: ".";
 display: block;
 height: 0;
 visibility: hidden;
}

#Container {
 display: inline-block;
}

/* Vor IE5/Mac verstecken \*/
#Container {
 display: block;
}
/**/

#ContainerAussen {
 background: url("bg-left.gif") repeat-y 20% 0;
}

#Container {
 background: url("bg-right.gif") repeat-y 80% 0;
}

.Spalte .eingepackt {
 padding: 20px;
}

#Inhalt .eingepackt {
 padding: 20px 30px;
}

#Inhalt p {
 margin-top: 0;
}

#Inhalt p:first-child {
 font: normal 1.4em/1.6 Georgia, Times, "Times New Roman", serif;
}

#Inhalt p:first-child:first-line {
 text-transform: uppercase;
}

#Navigation ul, #Navigation ul li {
 list-style: none;
 margin: 0;
 padding: 0;
}

#Navigation ul li {
 margin-bottom: .4em;
}

#Navigation li a {
 background: #36C;
 color: #FFF;
 border-left: 7px solid #09F;
 display: block;
 padding: .4em .4em .4em 20px;
 text-decoration: none;
}

#Navigation li a:hover {
 border-left: none;
 border-right: 7px solid #09F;
 padding-left: 27px;
}

#Information {
 color: #555;
 font-style: italic;
}

#Copyright {
 border: 1px solid #B2B2B2;
 border-width: 1px 0;
 clear: both;
 padding: 10px 20px;
 text-align: center;
}

#Copyright p {
 margin: 0;
}

Complete multicolumn layout

Figure: Complete multicolumn layout.

Discussion

The float model has a turbulent history. CSS authors did not intend floats for page layout, just for controlling content flow around elements, like align="left" and align="right" control flow around an element img. Despite that original purpose, floats offer powerful, flexible alternatives to traditional table layouts.

Alex Robinson published an influential article covering, among other things, columns in any order (“Any Order Columns”). His technique makes multicolumn float layouts easy: columns can appear in any order, regardless of their position in HTML.

The markup

First define the columns in the markup as shown:

<div id="Container">
  <div id="Inhalt" class="Spalte">
    <div class="eingepackt">
      [...]
    </div>
  </div> <!-- /ENDE #Inhalt -->
  <div id="Navigation" class="Spalte">
    <div class="eingepackt">
      [...]
    </div>
  </div> <!-- /ENDE #Navigation -->
  <div id="Information" class="Spalte">
    <div class="eingepackt">
      [...]
    </div>
  </div> <!-- /ENDE #Information -->
</div> <!-- /ENDE #Container -->

Inside the elements div you may use any markup. The next figure shows the unstyled document; our example uses paragraphs and an unordered list.

Unstyled page layout

Figure: Unstyled page layout.

Each column uses an element div whose attribute id describes its content: Inhalt, Navigation and Informationen. We could have named them Mitte, Links and Rechts, but that would be shortsighted. What if CSS changes the order? The element div with the attribute id set to Links might end up on the right.

Defining the columns

With the markup established, assign the necessary rules using float to the columns:

.Spalte {
 float: left;
}

Little has changed in the next figure. Copyright moved up slightly, but the column div elements still stack vertically. This changes as soon as we define the dimensions of the blocks.

Copyright notice moved

Figure: Copyright notice moved.

Start with the block Inhalt. This rule gives it 60% of the viewport width and reserves 20% on the left for navigation:

#Inhalt {
 margin-left: 20%;
 width: 60%;
}

The next figure still looks disordered, but the layout is taking shape.

Layout after styling the content area

Figure: Layout after styling the content area.

The content’s 20% left margin reserves space for the left column. Next, give the elements div with ID Navigation a negative margin, moving navigation into that space:

#Navigation {
 margin-left: -80%;
 width: 20%;
}

Calculate the left margin from the content width, 60%, plus its left margin, 20%. This moves navigation into position (see the next figure).

Navigation moved to the left

Figure: Navigation moved to the left.

Complete the layout by giving the block with ID Information a width, as shown below:

#Information {
 width: 20%;
}

Right column moved into place

Figure: Right column moved into place.

This looks good, but the copyright is misplaced. The property clear fixes that easily (see the next figure):

#Copyright {
 clear: both;
}

Copyright below the other elements

Figure: Copyright below the other elements.

The layout seems finished, but Internet Explorer for Windows still has display problems, shown below. Let us fix them.

Display problems in IE for Windows

Figure: Display problems in IE for Windows.

This IE bug is documented as the “Doubled Float-Margin Bug”. A left float with a left margin has twice its specified margin in IE; the same applies to right floats with right margins.

Our left float has a 20% left margin, incorrectly doubled to 40% by IE for Windows.

Fortunately, setting its display role to inline corrects IE’s behavior. Use this CSS:

/* Anpassungen für IEx \*/
* html .Spalte {
 display: inline;
}
/**/

The unusual comments and specially formatted selector * html ensure only IE through version 6 recognizes the rule. As shown below, it then behaves correctly.

Special rules make the layout work in IE for Windows

Figure: Special rules make the layout work in IE for Windows.

At last, a flexible three-column template! What else can we do with it?

Creating space

Typographers call column spacing the intercolumn space, or gutter. Margins are one way to increase it, but other methods exist. First wrap each column in additional elements div:

<div id="Container">
  <div id="Inhalt" class="Spalte">
    <div class="eingepackt">
      [...]
    </div>
  </div> <!-- /ENDE #Inhalt -->
  <div id="Navigation" class="Spalte">
    <div class="eingepackt">
      [...]
    </div>
  </div> <!-- /ENDE #Navigation -->
  <div id="Information" class="Spalte">
    <div class="eingepackt">
      [...]
    </div>
  </div> <!-- /ENDE #Information -->
</div> <!-- /ENDE #Container -->

The extra elements div have their attribute class set to eingepackt, from “to wrap,” meaning enclose or wrap. (Translator’s note.) Give them more space with a class selector (see the next figure):

Increased column spacing

Figure: Increased column spacing.

.Spalte .eingepackt {
 padding: 20px;
}

#Inhalt .eingepackt {
 padding: 20px 30px;
}

Changing column order

Any-order columns rely on clever margins: positive ones reserve space, while negative ones move columns from their normal positions.

Examine just the column widths for a moment:

#Inhalt {
 width: 60%;
}
#Navigation {
 width: 20%;
}
#Informationen {
 width: 19%;
}

Without margins, the floated columns follow HTML source order, as shown below.

Navigation between the other two columns

Figure: Navigation between the other two columns.

Give navigation (<div class="Navigation">) a left margin and information (<div class="Information">) a negative margin to reverse the last two columns. This CSS produces the next figure:

#Inhalt {
 width: 60%;
}
#Navigation {
 margin-left: 20%;
 width: 20%;
}
#Informationen {
 margin-left: -39%;
 width: 19%;
}

Last two columns swapped

Figure: Last two columns swapped.

Finish by moving the content column (<div class="Inhalt">) to the right (see the next figure).

Use this CSS:

#Inhalt {
 margin-left: 40%;
 width: 60%;
}
#Navigation {
 margin-left: -100%;
 width: 20%;
}
#Informationen {
 margin-left: -80%;
 width: 19%;
}

Content column moved right

Figure: Content column moved right.

As before, a margin reserves space left of the content column. Negative margins then move the navigation and information elements div into their new positions.

Layout algorithm

Calculate a column’s negative margins with this algorithm:

  1. Find the rightmost point of all columns preceding it in the source.

  2. Do the same for the leftmost point.

  3. Subtract the right value from the left to obtain the left margin of the column to move.

If this fails, there is always good old trial and error.

Faux columns

Return to the original layout below and improve its appearance. First: background images!

Finished layout without graphics

Figure: Finished layout without graphics.

The technique called Faux Columns was developed by Dan Cederholm and uses horizontally tiled column backgrounds.

Tiled backgrounds suit fixed-width designs particularly well, but the technique needs only small changes for flexible layouts.

First prepare an image for each side of the content column (see the two figures):

Image for the column’s left side

Figure: Image for the column’s left side.

Image for the column’s right side

Figure: Image for the column’s right side.

Next wrap the container block in an extra element div:

<div id="ContainerAussen">
  <div id="Container">
    [hier steht der Rest der Layout-Schablone]
  </div> <!-- /ENDE #Container -->
</div> <!-- /ENDE #ContainerAussen -->

Finally add these stylesheet rules:

#Container:after {
 clear: both;
 content: ".";
 display: block;
 height: 0;
 visibility: hidden;
}

#Container {
 display: inline-block;
}
/* Vor IE5/Mac verstecken \*/
#Container {
 display: block;
}
/**/

#ContainerAussen {
 background: url("bg-left.gif") repeat-y 20% 0;
}

#Container {
 background: url("bg-right.gif") repeat-y 80% 0;
}

The columns now appear full-height regardless of content length, as shown below.

Layout with graphics

Figure: Layout with graphics.

Add further styles beyond typography to this base. The solution’s rules work well, producing the next figure.

Finished page layout

Figure: Finished page layout.

An alternative solution

Floats are powerful but have a steep learning curve. Many designers prefer absolute positioning using exact x and y coordinates.

Unfortunately, absolute elements leave document flow; content wraps within the containing block. Consequently, positioned layouts cannot use the property clear. Floats, in contrast, allow context-sensitive sections. For example, the rule clear: both; puts a footer below all columns, which is not straightforward with absolute layouts.

Talented web designer Shaun Inman wrote a JavaScript function to solve this. Include the script and marked elements automatically clear positioned elements (see the next figure).

Absolutely positioned layout

Figure: Absolutely positioned layout.

The only potential drawback is requiring JavaScript enabled in the browser. Test your pages: if content remains accessible in the target browser without JavaScript, all is well.

See also

The recipe Asymmetric layouts on asymmetric layouts with absolute positioning.

  

 

 

 

back to top