Special thanks to John Govsky for this tutorial.
*Note that nothing is truly "bulletproof" when it comes to Web browsers and HTML.
This example demonstrates the technique commonly used to force Web browsers to consistently render HTML tables. This trick, or "kludge," keeps tables from collapsing, regardless of the contents of the table. Here we will use pixel shims to fix the width of the table data cells, and thus of the whole table.
You will need a transparent pixel GIF. If you don't already have an invisible
GIF, you can grab one from:
cleardot.gif

When all is said and done, this row will be only one pixel tall, and will be invisible.

Use vertical lines to define all possible columns, even if a line cuts through a row (as these lines cut through the row containing the headline).

Use horizontal lines to define all possible rows, even if a line cuts through a column (as a line cuts through the text block on the left).

First decide how wide the entire table is to be. (In this example we will set the table width at 600 pixels, a size that fits within most monitors.) Then decide on all column widths, which of course when added up must equal the table width, exactly. Remember that each column must be at least as wide as any images that will be placed into a cell in that column.

Note that each column in the top row will become a cell containing an invisible pixel. Now erase the lines that cut through, or span, a cell.

Now we can clearly see the underlying table structure.

Note that this table is four rows by three columns, and that the first row,
reserved for the invisible pixels, will become only one pixel tall. Also note
that the table data cells in this top row will not contain any rowspan
or colspan attributes.
The width of the table data cells is set by using the width attribute
of the invisible pixel <img> tags, not by
using the width attribute of the <td> tags. The border,
cellspacing, and cellpadding attributes of the <table>
must be set to zero.
Here is the code for the above table:
<table border="0" cellspacing="0"
cellpadding="0"> |
Note that in this example the width of the table is fixed, not its height. You are usually much more concerned with the width than the height. To fix the height instead of the width, you can use this technique, but with an added column for pixel shims (with a width equal to one pixel) instead of an added row. If you wish to fix both the table's width and height you will need to be very careful to allow enough room for text in the cells to get larger without breaking the table structure.
Also note that for invisible pixels used as spacers or shims, the empty alt
value (nothing between the quotes, as in <img alt=""
src="cleardot.gif"> ) is used.