One of the complicated things when we are doing a design is to doing it only using divs and not tables. Some of us always use tables. And why? because it’s simple? Maybe using div’s is not so dificult after all.
First, there are several reasons to use a div layout instead of a table based layout:
I will take has example webgames.zi-yu.com, a website whose design is integrally constituted by divs but, in it’s original form, it was made using a table.
Let´s start with the layout of the page:

As we can see in the image above, there are 4 main areas, each one divided in subareas:
Using tables this layout would be very simple to make:
<table> <tr> <td rowspan="2">A1</td> <td colspan="2">A2</td> </tr> <tr> <td colspan="2">A3</td> </tr> <tr> <td rowspan="2">B1</td> <td colspan="2">C1</td> </tr> <tr> <td>C2</td> <td>C3</td> </tr> <tr> <td colspan="3">D1</td> </tr> </table>
With div’s the layout would be even simpler (and more easy to read - in my opinion, semantically correct). Let’s start by the beginning. We have 4 different areas, so we will have 4 different divs:
<div id="allContent"> <div id="topMenu"></div> <div id="leftMenu"></div> <div id="mainContent"></div> <div id="footer"></div> </div>
Now that we have the sections defined, let’s take care of the sub sections:
<div id="allContent"> <div id="topMenu"> <div id="logo"></div> <div id="topMenuUp"></div> <div id="topMenuDown"></div> </div> <div id="leftMenu"></div> <div id="mainContent"> <div id="mainMenu"></div> <div id="mainContentLeft"></div> <div id="mainContentRight"></div> </div> <div id="footer"></div> </div>
At this point all the sections of the page are defined. We only need to apply the css style to adjust it’s position and size. In this style (and in all the styles i make) i always consider a resolution of 1024×768. For this resolution, the maximum size i use is 1000, because of the vertical scroll of the browser. Normally this scroll is hidden but when it appears, the size of the layout is reduced and the horizontal scroll appears (thing that we don’t want).
So, considering the width of the layout 1000 pixels, let’s start with the logo:
#logo {
width:252px;
height:110px;
float:left;
}
#topMenuUp {
height:20px;
border:0px;
}
#topMenuDown {
height:90px;
padding-left:0px;
text-align:center;
border-bottom:solid 1px black;
}
What the css above will do? Normally the 2 divs are positioned by default one below the other. So we will maintain that behavior for the div’s with the id topMenuUp and topMenuDown (only the height of them will be adjusted). Regarding the div with the id logo, we need to align it to the left, so we use float:left to obtain that result.
Next, the main content and the left menu. Generally i use the following stategy: if i have several containers in the same level, i separate them inside larger divs. So, i separated the leftMenu (area B of the picture above) and the div’s in the area C. The div’s in the area C i putted them inside a larger div in order to aligned them all at the same time into the right of the leftMenu div:
#leftMenu {
width:252px;
float:left;
display:block;
}
#mainContent {
width:745px;
float:right;
display:block;
}
Regarding the different sections inside the div mainContent, there is a top section (area C1), that will fill mainContent top, and two other div (areas C2 and C3) that will fill the rest of the div.
So regarding the div mainMenu (area C1), we will apply the following style:
#mainMenu {
height:48px;
padding:0;
margin:0;
clear:both;
}
The clear:both style is very important. It allows the div mainMenu not to have any content at it’s left or at it’s right. So, if other div (like the ones below) have a float style applied to it, that div will appear below mainMenu div.
Because the div mainMenu has the clear:both applied, the divs mainContentLeft and mainContentRight can be aligned with floats without a problem:
#mainContentLeft {
float:left;
width:584px;
}
#mainContentRight {
float:right;
width:160px;
}
Finally the footer. To this div i’ll just apply a clear:both style just to guaranty that no other div will overlap it:
#footer {
height:48px;
clear:both;
}
And that’s it, a layout that, at first sight, would be difficult to be made with divs, but, with simple css, it becomes very simple.
In conclusion, using divs, floats, clears and widths you can make almost any layout you want.
bom post
mas bastava Eu compreendo so ia dificultar a vida aos outros leitores
abraço
Nice post, but beware: there’s life beyond DIVs.
Newbie CSS-designers learn the DIV tag and suddenly it’s the only tag they use. Like when you start to class the DIVs with “contentTitle” and “contentSubtitle” when there’s already tags for that purpose (H1 and H2) that can be styled as desired.
I have a draft written on this. Perhaps it would be a good time to wrap it up and post it.
thanks Nuno
Pedro, yes, i agree with you. If you see webgames html, all the titles are inside a hX element.
And, although the hX have all the same css style, each hX has correct places to be used.
Example: only the main title of the content can be in a h1. Because is the most important title of the page and because, this way, the search crawl will give him more relevance than the other h elements in the page (SEO Optimization).
What do you mean by “almost”?
haha
Yeap, beware of divitis. It stings like a bitch.
almost means “something i have forgotten and i can’t do with divs”
(just for guaranty)
Nice post. Very useful. Thanks
Very nice
great post .. Thanks
Nuno Fernandes, dirtylove and vivek , thanks for the positive feedback
The post is very nice, very useful. But do DIV tags have relative positions/sizes? P. ex., with tables you can write width=”100px” (absolute) or you can write a relative size width=”20%” (relative).
In your example you assume that the screen resolution is 1024×768. What if it’s a different resolution, p.ex. 800×600?
I tried to access cssgarden.com, but it seems it’s not the site it should be…
Thanks
Hello Edu,
I just corrected the url. Thanks.
Yes, you can have that with divs. Although you have to have a little careful with the floats. If you do the wrong calculations, you can have a div overlapping another or two divs position themselfs below the other, when what you wanted was thhem side by side (this with absolute).
That0s why i always like to give the relative width to an element. Because this way you will always know how your content will look like in every browsers and you will not have position problems.
Regarding the screen resolution, you have to make a choice. 800×600 resolution is almost extinct. Most of the world population that uses computers have screen resolution equal or above 1024 (this is the idea i have by the google analytics information i see).
So, if eventually a user has it’s screen resolution setted to 800×600, the horizontal scroll bar will appear and he has to used it to see all the content.
[...] Divs instead of tables - to paraphrase : “Use the div, Luke.” [...]
“Bandwidth: using divs saves bandwidth because you need less xhtml to reach your objective.”
That’s relative. Only check your example again to see what I’m talking about.
Regards.
Nevertheless, this is a great post.
Thanks.
Koko.