Page Layout Tables

CSS, divs, and stacked tables for web pages

Page 6 of 6 123456 Current

A Few Notes On Using Tables

Tables do have their little problems—well, some browsers have little problems with tables. Still, none of them are as bad as the many div float problems of IE’s broken float model. Let’s take a look at some common ones, with easy solutions.

First, we need to remember the difference between a table and a div, as containers. Without a width setting,

Since tables and cells collapse, and since the CSS directive { min-width:something; } is still not supported by all browsers, when we set a cell some width we must fill it with something that is guaranteed to push it out to that width. The old way of doing this is with that irritating <img src='spacer.gif' width='something'> gizmo. Fortunately, with a CSS-controlled cell, that isn’t necessary, because we can set a pixel width on anything we will always put in the cell—it does not have to be an invisible image—it can be a text heading, for example.

In the CSS on the example above, I deliberately set #lft h1 width to a pixel value, which effectively establishes the min-width for the cell and keeps the fluid right column from squashing it. Fortunately, it’s a simple width setting in the CSS file (once), not deprecated markup in the HTML (many times). And it doesn’t add clutter or bloat to the code.

For a fluid-width cell, do not set the width at all. Just let the cell’s contents fluff it out to fill the remaining space. Fluid-width content cells typically contain wrapped paragraphs of text. Wrapped text is “springy,” and causes it’s containing cell to push against an adjacent cell, which is why we need to nail a fixed minimum for the adjacent cell.

Floated divs may indeed create visual columns, but the columns are of different lengths, and those lengths change as textural content flows to the user’s particular browser. This becomes a problem when doing things like placing a visible left or right border in a div, as the border will simply stop where the div ends. On the other hand, a visible vertical border in a table cell will always run the full height of the row, which is the height of the tallest cell. The exception is where we want to use the full page width for content after the bottom of the shorter column. In that case, make a single cell for content, and then nest a floated table or div therein for the short column. I do that sometimes for a “Page Contents” floater—i.e., links to same-page sections that I don’t want in the main menu. You will note that the text of this article wraps the right sidebar menu. Yep, it’s a floated div, and that’s why I used it herein, instead of table cells.

Every table element may be CSS-controlled just like divs, with the added benefits of (a) no floating needed, and (b) it automatically renders a grid structure. That’s just what it is, and a table should be used for layout anytime a real grid structure is needed. Regardless of any prejudice against using tables for layout, the table is the only structure available in HTML that renders a grid structure.

Table overrides should always be specified in the CSS file prior to effectively using tables, and particularly for layout tables. One of these is border-collapse:collapse. Browsers default to the border:separate model, which puts spacing between cell borders, complicating the use of margin and padding, usually looks bad, and is totally unnecessary. Browsers also impose poor defaults for margins, borders, and padding, text-align, and vertical-align.

For example, the table defaults for a fluid layout table should be: border-collapse:collapse; width:100%; margin:0; border:0;, and then set the cells’ defaults as needed, usually adding some css padding to make the text stand off the edges some pleasing amount. Fifteen to thirty pixels looks nice in a full-screen fluid layout table. It is not necessary to insert empty rows or columns to make margins!

Annoyances

Tables have some funny behaviors that drive one crazy until one learns CSS tableology. For example,

Well, I hope this has been helpful. Someday, perhaps we’ll see a template description that reads, “Uses CSS and stacked tables, not floated divs.” Probably not.

In the next series of articles, we’ll look at some easy CSS tricks and tips.

Page 6 of 6 123456 Current

— KV5R is disabled. Please help. —

Valid HTML 5 Valid CSS WCAG 1 conformance Handmade with Ultra Edit Studio Xara Xtreme