Tips & Tricks
Using Tables

Tables are a common addition to a web site. The are often used for more things than just displaying spreadsheets or organizing tabular data. A table can be used to create a certain look or presentation to a web page. You can even have tables embedded inside other tables. The page you're looking at now contains several tables for instance.

Table Basics
<table>
<tr>
<th> </th>
<td> </td>
</tr>
</table>

A table is made up of 4 parts.

  1. A tag that starts and ends the table.
  2. A tag that starts and ends a table row.
  3. A tag that starts and ends a table header cell.
  4. A tag that starts and ends a table data cell.

 A Table Cell

The table to the left is a simple 1 cell table with a border and centered text. To create this simple table, here's what you need. Look for the 4 tags described above in this example. Find the start and end of the table. In the start tag, you'll see the width and height of the entire table. You can alter the width of the border by increasing the border value from "1" to "2" for instance. You can set the spacing between cells, making a more pronounced spacing between the columns and rows. You can increase the padding inside each individual cell as well, which help in viewing the text within your table.

The table tag defines the entire table, where the <TD> or Table Data Cell tag defines the attributes of a specific cell. In this example there's not much to define, just the alignment of the text within the cell.

<TABLE WIDTH="108" HEIGHT="23" BORDER="1" CELLSPACING="2" CELLPADDING="0">
<TR> <TD ALIGN="CENTER">
A Table Cell</TD></TR>
</TABLE>


Table Header
 Table Cell

This table utilized the table header tag. The <TH> tag automatically tells the browser to center and bold the text within the cell. But to further separate this row, I've added a background color to the cell and altered the color of the text to make it stand out. Here again you'll see the <table> tag. The <tr> tags to start 2 different rows. In the first row, you'll see the <th> to start the table header, but you'll also see the parameter setting the back ground color of this row. Within the table header, you'll see a <font> tag and the color parameter setting the Table Header text to white. The remaining row on the table is like the one described above.

<TABLE WIDTH="118" HEIGHT="23" BORDER="1" CELLSPACING="2" CELLPADDING="0">
<TR> <TH "BGCOLOR="#3d3d7a">
<FONT COLOR="#ffffff">Table Header</FONT><</TH></TR>
<TR> <TD ALIGN="CENTER">
Table Cell</TD></TR>
</TABLE>


Table Header
 Table Cell
You can also set the border color of your table by adding the Bordercolor attribute. The attribut works like all the other 'color' attributes, just choose your color and insert the proper color code.
<TABLE WIDTH="118" HEIGHT="23" BORDER="2" CELLSPACING="2" CELLPADDING="0" BORDERCOLOR="#336699">
<TR> <TH "BGCOLOR="#3d3d7a">
<FONT COLOR="#ffffff">Table Header</FONT><</TH></TR>
<TR> <TD ALIGN="CENTER">
Table Cell</TD></TR>
</TABLE>

Making Columns Work For You.

The example to the left is exactly like the table used to format this page. The only difference is, the height and width of the two columns. Setting these parameters can be a very helpful tool when you don't want to code frames for your web site. Although frames are easy to create and easy to manage, many visitors on the web don't like frames. You can use the table tags to give the same effect without the hassles.

Example
To appear like I'm using frames on this page, I created a double column table. The left side of the table is just big enough to hold the navigation graphics on top of the dark blue background. While the right side contains the larger space for the main text of the page.

The small table to the left is an example of this effect with the border turned on. The following is the code needed to create this table. The Width parameters within the TD tag, tells the browser how large to make the columns. The TD width parameters are defined in percentages, instead of the pixel setting you'll see in the Table tag. The first column will take up 19% of the tables 125 pixels, the second column will take up the remainder 81%.

<TABLE WIDTH="125" HEIGHT="23" BORDER="1" CELLSPACING="2" CELLPADDING="0">
 <TR> <TD WIDTH="19%"><BASEFONT SIZE=2>
 <IMG SRC="../img/btnsm.gif" WIDTH="10" HEIGHT="10" ALIGN="BOTTOM"></TD>
 <TD WIDTH="81%"><BASEFONT SIZE=2>
 Making Columns <BR>
 Work for you.</TD></TR>
 </TABLE>


Nested Tables

Going Step by Step:
1. Step #1
2. Step #2


Here's the same table above, with the outer tables border turned off by setting the border to "0".

Going Step by Step:
1. Step #1
2. Step #2
 

Placing a table inside a table can help you further format your text within your web page. To nest a table, you simply place the same tags described here inside a single table data cell. This method of nested tables is what I've used to help format the text within these Tips & Tricks pages.

This allows us to place a bulleted item with a heading above a formatted set of text as depicted to the left. You'll see an additional parameter within the TD tag called VALIGN. This parameter tells the browser that the contents of the data cell are vertically aligned at the top.

<TABLE WIDTH="133" HEIGHT="37" BORDER="1"
CELLSPACING="2" CELLPADDING="0">
<TR>
<TD WIDTH="18%" VALIGN="TOP">
<IMG SRC="../img/btnsm.gif" WIDTH="10" HEIGHT="10" ALIGN="BOTTOM"></TD>
<TD WIDTH="82%" VALIGN="TOP"><BASEFONT SIZE=2>
Going Step by Step:<br>
<TABLE WIDTH="91" HEIGHT="65" BORDER="1"
CELLSPACING="2" CELLPADDING="0">
<TR>
<TD WIDTH="19%" VALIGN="TOP">1.</TD>
<TD WIDTH="81%" VALIGN="TOP">Step #1</TD></TR>
<TR>
<TD WIDTH="19%" VALIGN="TOP">2.</TD>
<TD WIDTH="81%" VALIGN="TOP">Step #2</TD></TR>
</TABLE>
</TD></TR>
</TABLE>

Personally I think this type of nesting is more effective when the table border is set to "0". The second table on the left is a duplicate of the first, but the table border of the outer table is set to 0.


One of the biggest disadvantages to using a lot of tables is the amount of time a browser spends trying to load it. A clean table, that's a table without a lot of extra code, can load fairly quickly. But a table with extra HTML tags can take longer, and can even hangup some browsers.

If you're using native HTML to create a table, you can generate the desired result with the least required HTML commands, because you are controlling the code. But if you're using any of the new HTML Editors, such as HotMetal, HotDog, PageMill, or FrontPage, the editor will add tags, whether you need them or not. The browser will then be forced to "read" each tag, even if it's not necessary for the data you're displaying. The browser still has to read each tag inside each individual cell. This is where the problem with loading time occurs.

If you don't know HTML, how can you avoid this type of problem? That's hard, but it is possible. First use an HTML editor that doesn't just assume it knows what you want to do inside your code. Today, Adobe's PageMill does add some tags you don't necessarily need, it adds the least compared to FrontPage and HotMetal.


 Next: Using Email & Forms