Simple CSS column layout

A lot of designers are currently struggling to accomplish page layout that was simple to achieve using tables but is frustratingly difficult (if not impossible) with CSS. The following example shows a simple method to display a mutli-column content layout without having to worry about complex floats, margins, and padding.

It all begins with a simple unordered list with a line item and div for each column. We assign an id selector to each item that may have individual CSS styles applied to it (eg id="column1"). Then add a style sheet class for all items that share formatting (eg: class="r_column").

<ul>
  <li>
    <div id="column1" class="r_column">
      <p>First Column</p>
    </div>
  </li>
  <li>
    <div id="column2" class="r_column">
      <p>Second Column</p>
    </div>
  </li>
  <li>
    <div id="column3" class="r_column">
      <p>Third Column</p>
    </div>
  </li>
</ul>

The real magic occurs in the CSS where we make the list appear horizontally rather than vertically. Because of differences in browsers we have to set the margin and padding explicitly. The rest is explained in the code below.

ul {
        clear:both; // needed to clear the floated divs
}

ul li {
        padding: 0; // removes spacing between items
        margin: 0; // removes spacing between items
        display: inline; // makes the list side by side
        list-style:none; // removes the bullet from the list
}

div.r_column {
        margin: 0 5px; // set the space between items
        display: block; // needed to correctly render the div
        float:left; // make divs appear side by side
        width: 20em; // set the width of the column
}

Put it all together and we get:

  • First Column

  • Second Column

  • Third Column

Now isn't that about as simple as you can get?

Add your comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.
  .d@S99b.    .d88HSb.    .d88HSb.       d98@8    .d@S99b.  
d9EP Y@@b d8HP YS@b d8HP YS@b d8P5M9 d9EP Y@@b
YMMb. dSMP ESM 8M5 ESM 8M5 d9P M@9 YMMb. dSMP
"YMS95E" YS8b. d9E8 YS8b. d9E8 dHP 8M9 "YMS95E"
.d8P""YHb. "YEMMPEEM "YEMMPEEM d98 HEM .d8P""YHb.
M@8 9H@ S9S S9S S95ES@E9E8 M@8 9H@
YSMb d@SP Y5@b d8EP Y5@b d8EP SH8 YSMb d@SP
"YHHH5P" "Y9M85P" "Y9M85P" HME "YHHH5P"