How to Create Columns with Div’s?

When I started using div’s rather than table for web page layout, the most daunting experience for me was how to create columns using div’s. So, here is the solution for all those website developers who are struggling to use div’s for web page layout.

The <div> tag can be defined as the division in a web page. It is a block level element. This implies that the default behavior of div’s is to stack up one above the other. This serves the purpose of using div’s for a simple web page layout where all the elements stack one above the other. When we have a columnar web page layout (which happens to be the most common layout for majority of the websites), we need to know how to use div’s to create two or more columns in a web page.

The following method will cause div’s to stack up side by side rather than one above the other.

Let’s say that we have two div’s:
<div id=”div1″></div>
<div id=”div2″></div>

Now, in the stylesheet declare the following rules.

#div1 { float:left; width:50%;}
#div2 { margin-left:50%; width:50%;}

The result will be two columnar div’s. You may use this method to create as many columns as needed on the web page.

Now, if you need these two columnar div’s to expand vertically equal to each other you can use the following method or use min-height.

First create a background image that if tiled vertically will look like two columns. Here, a background image has been used in the container div that holds the body and the footer of the web page.

This background image must be 1 pixel high and as wide as the container that contains all the elements of your web page.

In the html document:
<div id=”container”>
<div id=”div1″></div>
<div id=”div2″></div>

Declare the following rules in the stylesheet:

#container { background: url(the_Url_Of_The_Background_Image.gif) repeat-y 0% 0%; }

#div1 { float:left; width:50%;}
#div2 { margin-left:50%; width:50%;}

The result will be the appearance of two columnar div’s expanding vertically equal to each other. So, although we cannot extend a div vertically equal to another div, we can make it appear so.

18 thoughts on “How to Create Columns with Div’s?”

  1. Works while right column is same height or taller than left column. On Firefox, if left column is taller it extends below container div.

  2. ohh, at last, somebody has taken those divs in order, thanks, simple and clever

    But how can i make 3×3 divs structure in order with css?

  3. Quick question,

    Is it possible to have a div table, based on the above, to act and behave like an html table? For example, i want to resize the screen, using the bottom right-hand “resize option” where we can click and drag.

  4. Can you show how this can be done using php? I have a pagination script in php, and I have a while loop to display info from the db. Can you show a php example?

    Thank you

  5. Sorry, Brian. I’m out of touch completely as I quit web developing about 3 years ago. So, I’ll not be able to show you using php.

  6. How do i create multiple columns using div? doesnt seem to work…say there are 3 divs..div1, div2, div3… how do you style the div2 in this case?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.