Making a 3 Column Fluid Layout With CSS

3 Column CSS Layouts always seem to be the most sought-after by web designers. To create a layout with three columns, including two fixed width sidebars and a fluid center and not using tables seems to be, as A List Apart's Matthew Levine put it, The Holy Grail in his article on this.

While I was looking over the article, I thought I could make it easier for you, the common web designer to do it easier and without so many browser hacks. So spawned this tutorial. In this tutorial, you will be creating a valid XHTML 1.1 Strict and valid CSS layout with three columns, and no IE, Firefox, or other browser hacks.

The finished layout can be found here.

Making a 3 Column Fluid Layout With CSS

Basics

So to start, we need to define the basic page.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

<head>
<title>3 Column Layout</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css"> </style>
</head>

<body>
</body>

</html>

This should all be very basic. The doctype will be XHTML 1.1, and the rest should be obvious.

The first thing we will be doing is modifying the basic styles. In between the <style></style> tags, add:

1. { margin:0; padding:0; } BODY { background-color:#651; }

2. { margin:0; padding:0; } means that all elements will have an initial margin and padding of 0. This ensures that the layout will fit the page fully. Without having a margin and padding of 0 on the body, there would be a space all the way around the layout. BODY { background-color:#651; } sets the background color.

The Header

The next thing that most websites have now is a header. It seems to be a staple for websites, to have your saying and website name in a header at the top. So we'll go ahead and create that.

So to create the header, the style will be an id with two rules.

#header { background-color:#BFAC60; padding:.5em; }

This has a background color of kind of a darker gold. And you should add some padding to create some readability.

The HTML is just as easy.

<div id="header"><h1>3 Columns - 3 Divs</h1></div>

It's a simple <div> element with an id of header. Notice we did not have to add another rule for the header (h1). We already said in our first rule that all elements have 0 margin and padding, which is my pet peeve with headers. And that's a very easy header. It extends across the browser page.

The Columns

Now we get to the fun part. To actually make the columns.

<div id="left">Float Left</div>
<div id="right">Float Right</div>
<div id="center">Center Content</div>

The HTML is very simplistic. Three divs. One with an id of left, one with an id of right, and one with an id of center. This makes it so much easier to manage for you than crazy wrappers and everything.

The CSS, however, is still very easy.

#left { float:left; width:200px; padding:.5em; background-color:#dc8; }
#right { float:right; width:200px; padding:.5em; background-color:#dda }
#center { margin-right:215px; margin-left:215px; padding:.5em; background-color:#eec; }

Allright. We're going to use floats for this layout. Floats in CSS force an element to either the left or the right. Here we must make the floats go above the actual center. We can't make the divs in order of left, center, right, or right, center, left. It must be left, right ( or right, left) then center. The reason is that floats will be forced to its side, and if it's in the wrong order, it'll either come before or after it's supposed to.

Anyways, #left will float left. We're going to make it have a width of 200 pixels. Why? It's a good width. Why not? Then we have the simple padding of .5em, and a yellowish background color. #Right will be exactly the same, except it will float to the right.

Center is a bit different. For the center div, you must define the margins for both the left and the right, or else it will either be forced to another line, or force another div to another line. But after that, the padding is still .5em and we have another light gold-ish yellow background color. You can also notice that you don't need to define a width. It already will adjust to the width of the browser and leave room for the sidebars with the margins.

The Footer

If you've previewed your layout, it will be very, very messed up. And this can all be fixed with one simple div which must have clear:both. Which leads us to another trend: a footer. Most websites now have a footer at the bottom which gives some kind of info and maybe some contact info, or some links, etc. Your footer will serve two purposes: the most important is to fix the layout, and the second is to give that info.

Using float:left or float:right in layouts always gives some problems unless it is cleared. What the clear:both means is that the floats and the layout will be forced to the bottom, which fixes most problems with this. The footer CSS is as follows:

#footer { clear:both; background-color:#CCC08F; padding:.5em; }

That's it. It's the same as the header basically, except it has a clear:both statement to fix the layout. And you can probably figure out the HTML:

<div id="footer"><a href="http://www.shadow-fox.net/">Back to the article</a></div>

It's that easy.

The only thing that could make this better, is to make the links fit in a bit more with the color scheme.

a { color:#807859; text-decoration:none; }
a:hover { text-decoration:underline; }

Makes it a nice goldish color. Kind of like the Epiphone.

And that covers exactly how to make a great 3 column fluid width layout with relative ease.

The finished page is:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

<head>
<title>3 Column Layout</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
* { margin:0; padding:0; }
BODY { background-color:#651; }
a { color:#807859; text-decoration:none; }
a:hover { text-decoration:underline; }
#header { background-color:#BFAC60; padding:.5em; }
#left { float:left; width:200px; padding:.5em; background-color:#dc8; }
#right { float:right; width:200px; padding:.5em; background-color:#dda }
#center { margin-right:215px; margin-left:215px; padding:.5em; background-color:#eec; }
#footer { clear:both; background-color:#CCC08F; padding:.5em; }
</style>
</head>

<body>
<div id="header"><h1>3 Columns - 3 Divs</h1></div>
<div id="left">Float Left</div> <div id="right">Float Right</div>
<div id="center">Center Content</div>
<div id="footer"><a href="http://www.shadow-fox.net/">Back to the article</a></div>
</body>

</html>

Continued...

You can extend this to make a two column layout by simply removing one of the two divs (either #left or #right). By leaving #center and just taking out the corresponding margin, it will instantly make it a 2 column layout. Good job!


Related Articles

  • GooglePage and AdSense Solutions
    GooglePage (tm) is free and easy to use. GooglePage offers 41 page skins (templates) and a simple editor to create pages. Simply add text, upload pictures, video, music and files, click publish and your page exists on the world wide web. Google offers 100 Mb of space and is coupled to your GMail.....
  • Liquid VS Fixed Layout Debate In Modern Web Design
    This very informative article will help you to understand the continued comparison of benefits and liabilities of Liquid VS Fixed Layout. The author explains when a mixed approach works best.
  • Successful CSS Template Skins
    The CSS mantra is the separation of content and style. The content should be in the HTML and the CSS should take care of how the HTML is displayed on the page. However, most web-designs dont properly separate content and style. The HTML and CSS are there separately, but if you changed the HTML, th....
  • Dreamweaver CS3 and Cascading Style Sheets
    Cascading Style Sheets (CSS) is a W3C standard mark-up language for defining the appearance of web pages. The use of CSS allows developers to fully separate the content of the page from its presentation, speeding up the development process and also making the...
  • Website Designing with Browser Compatibility
    In the early days of the Internet, many sites advertising Best viewed with Netscape or Best viewed with Internet Explorer or the like. These days, such labels seem to be rarer. Web designer today put an inordinate amount of effort to promote their sites on the search engines.
  • Grouping Elements
    Grouping similar elements helps the brain quickly decode a page layout. Proximity, Alignment, Containment, Rhythm and Styling are all tools that help indicate grouping...
  • MySQL Join Tutorial
    What are joins and how do you make them?
  • Achieving Differentiation With Your Website
    Internet Marketing is not a miracle marketing strategy
  • Testing and Optimizing your Traffic
    You can have the most fluid, creative, perfect business website in your chosen field but if your marketing and promotional efforts do not bring users to your website it is useless.
  • Web Design's Most Important Question: How Does the User Benefit?
    Who do you focus on when designing your website? Your company or the customer? Many companies are under the impression that web design is either all about them or only about making the site visually stimulating for users.

Contact Web Design Outsource and get started today

Need Website Designing, Development, Redesigning, Maintenance and SEO services or help growing your company's web presence? Request a free Quote Now.