Creating DIV-based CSS Tables - Using CSS tables to create a two-column web document layout
(Page 3 of 4 )
Put in a simple way, building web page layouts by using CSS tables is a procedure that relies on specifying that the divs that comprise the pertinent web document must behave as typical table elements. It’s that simple.
The crucial question that comes up here is: how can this be achieved with plain CSS code? Well, it’s possible to use the “display” property that you’ve probably utilized hundreds of times before to indicate to the browser that some selected divs must be laid out as table components.
In this case, since I’m planning to construct a layout comprised of a main column along with a side bar, the set of CSS styles that would render this design would be the following:
#maincol, #sidebar{
display: table-cell;
}
#tablewrapper{
border-collapse: collapse;
display: table;
table-layout: fixed;
}
#sidebar{
width: 20%;
padding: 10px;
background: #eee;
}
#maincol{
width: 80%;
padding: 10px;
}
As shown above, here’s where the magic of CSS tables really happens! As you can see, I defined two selectors identified as “sidebar” and “maincol” respectively, which obviously are the principal containers of this two-column layout. However, you should pay attention to the code that instructs these divs to behave as table cells. The following CSS declaration:
#maincol, #sidebar{
display: table-cell;
}
tells the browser that the corresponding divs should be rendered as table cells, instead of being shown as regular containers. Besides, it’s very important to note that I created an additional “tablewrapper” div, which will be displayed as a regular HTML table, since it’s been assigned the following CSS styles:
#tablewrapper{
border-collapse: collapse;
display: table;
table-layout: fixed;
}
It’s hard to believe, but that’s all the CSS code required to create a simple two-column web page layout by using CSS tables. Of course, this approach doesn’t demand the use floating divs, and indeed the CSS styles are much easier to read and code. Unfortunately, Internet Explorer doesn’t completely support this technique, but as I mentioned in the introduction, it seems that the engine included into its eighth version will be completely compliant with it.
So far, so good. At this point, you've hopefully learned the basics of using CSS tables to construct a two-column web page layout. However, this introductory example looks rather disjointed in its current incarnation, since it requires us to tie the previous CSS styles to the corresponding structural markup.
Thus, in the section to come I’m going to put all of these elements into one single file, so you can see more clearly how they link with each other.
Now, jump ahead and read the new few lines. We’re almost finished!
Next: Structural markup for a two-column web document layout >>
More Style Sheets Articles
More By Alejandro Gervasio