Faux Columns with CSS

One of the questions I get asked the most often regarding my personal site’s design is the following:

How do you get the right column’s background color to extend all the way down the page?

It’s a simple concept, really — one that many of you may already be familiar with.  But for those who aren’t, the following technique can be a handy little trick.

Vertical stretch

One of the somewhat frustrating properties of CSS is the fact that elements only stretch vertically as far as they need to.  Meaning, if a 200-pixel tall image is contained within a<div>, the <div> will only expand down the page 200 pixels.

This becomes an interesting dilemma when you use <div>s to section your markup, then apply CSS to create a columnar layout.  One column may be longer than the other (see Figure 1). Depending on the amount of content contained, it becomes difficult to create a layout with two equally tall columns when a unique background color is desired for each column.

figure 1
Figure 1

There are a few ways to make the columns appear equal in length, regardless of the content that they contain.  I’m sharing my particular solution (for use with an absolutely positioned layout), which happens to be pretty darned … simple.  This same little trick is also used elsewhere, including this very site.

The cheat

The embarrassingly simple secret is to use a vertically tiled background image to create the illusion of colored columns. For SimpleBits, my background image looks something like Figure 2 (proportions changed for demonstration), with a decorative stripy thing on the left, a wide white section for the content column, a 1 pixel border, and a light brown section for the right column’s background followed by the reverse of the left side’s decorative border.

figure 2: background image for vertical tiling
Figure 2

The whole image is no more than a few pixels tall, but when vertically tiled, it creates the colored columns that will flow all the way down to the bottom of the page —regardless of the length of content in the columns.

The CSS

This elementary CSS rule is added to the body element:

background: #ccc url(../images/bg_birch_800.gif) repeat-y 50% 0;

Essentially, we’re making the entire page’s background color grey and tiling it vertically only (repeat-y).  The 50% 0 bit refers to the positioning of the background image — in this case, 50% from the left side of the browser window (resulting in a centered image) and 0 pixels from the top.

Positioned columns

With the background image in place, my positioned layout sits on top, with padding and margins set for the left and right columns, ensuring that they will line up in the right place — within the faux columns created by the background image (see Figure 3).

figure 3
Figure 3>

It’s important to note that if borders, padding and margins are desired on either column, then we must still make up for IE/Win’s botching of the box model with Tantek Çelik’s Box Model Hack or Mid Pass Filter.

Alternatively, if borders or padding can be avoided altogether by just using margins instead, then the the Box Model Hack will not be necessary.  And if the column’s content is simply sitting (transparently) on top of the tiled background, then it should be easy to avoid the hack.

Whatever floats your boat

While I use absolute positioning to create a two-column layout on my own sites, equally fine results can be achieved via the float property (as seen here at ALA).

The same idea applies: tile the background image, then float a column in position to overlay the faux-column backdrop behind.

End notes

It’s a simple concept, but one that may alleviate one of the frustrations that designers frequently encounter when building CSS-based layouts.

 

references: tantek, zeldman

 

We will be happy to hear your thoughts

      Leave a Comment

      Web Training Guides
      Compare items
      • Total (0)
      Compare
      0