Chapter 12

Tables

by Eric Ladd


CONTENTS

Tables have been around for a while, but the table tags weren't officially made part of HTML until the HTML 3.2 draft was released. Browsers like Netscape Navigator and Microsoft Internet Explorer were supporting the table tags almost as soon as they were proposed and, now that tables are standard HTML, these companies are extending the tags to produce tables that are even more full-featured.

This chapter introduces you to tables as they have been written into the HTML 3.2 specification. While tables are intended for the display of columnar data, you'll find, as you progress through this chapter, that tables are much more than that-they are a bona fide page design tool as well.

How HTML Tables Are Organized

To understand the table tags better, it helps to take a moment to consider how HTML tables are structured. The fundamental building blocks of an HTML table are cells, which can contain a data element of the table or a heading for a column of data. Related cells are logically grouped together in a row of the table. The rows, in turn, combine to make up the entire table.

If you can keep this breakdown in mind as you read the next few sections, the syntax of the table tags will make much more sense to you. Remember:

Table Basics

Before delving into the more advanced uses of tables, it's instructive to look at a table being used for the purpose tables are intended: to display columns of data. The next three sections present the tags you need to create a simple table for this purpose.

The <TABLE> Tag

All table-related tags occur between the <TABLE> and </TABLE> container tags. Any table-related tags occurring outside of these tags will be ignored.

A good habit you should get into immediately is to put the </TABLE> tag into your HTML file when you put the <TABLE> tag in. If you don't have a </TABLE> tag and you go to a browser to preview your work, the browser won't render the table. This is because browsers read through all of the code to produce a table before rendering it. It has to do this to compute how much space it needs for the table. Once the amount of space is known and allocated, the browser goes back and fills in the cells. Without a </TABLE> tag, a browser can't know that it has hit the end of a table and, therefore, won't render any of it.

NOTE
If you're using an HTML-editing program that lets you compose a table on-screen, you won't have to worry about the <TABLE> and </TABLE> tags or any other table-related tag. The program will write the code to produce the table for you. Some very useful HTML-authoring tools that will do this include: HotDog (http://www.sausage.com/), HTMLEd (http://www.ist.ca/), and Ken Nesbitt's WebEdit (http://www.nesbitt.com/).

Creating a Table Row

Tables are made up of rows, so you need to know how to define a row. The <TR> and </TR> tags are used to contain the HTML tags that define the individual cells. You can place as many <TR> and </TR> tag pairs as you need inside a table, each pair accounting for one row.

So far, then, the code for a basic HTML table with m rows looks like:

<TABLE>
   <TR> ... </TR>   <!-- Row 1 -->
   <TR> ... </TR>   <!-- Row 2 -->
   ...
   <TR> ... </TR>   <!-- Row m -->
</TABLE>

TIP
Indenting your table code helps you keep better track of individual cells and rows.

Creating a Table Cell

Table cells come in two varieties: header cells for headers that appear over a column of data and data cells for the individual entries in the table.

Header Cells  A table header cell is defined with the <TH> and </TH> tag pair. The contents of a table header cell are automatically centered and appear in boldface, so you typically don't need to format them further.

In a standard table, headers usually comprise the first row so that each column in the table has some type of heading over it. If the basic table you're developing has n columns of data, the HTML for the table would look like:

<TABLE>
   <TR>    <!-- Row 1 -->
      <TH>Header 1</TH>
      <TH>Header 2</TH>
      ...
      <TH>Header n</TH>
   </TR>
   <TR> ... </TR>   <!-- Row 2 -->
   ...
   <TR> ... </TR>   <!-- Row m -->
</TABLE>

Data Cells  Data cells usually make up the bulk of the table and are defined by <TD> and </TD> tags. Text in data cells is left justified by default. Any special formatting, like boldface or italics, has to be done by including the appropriate formatting tags inside the <TD> and </TD> pairs.

If we let data cells constitute the rest of the basic table we're constructing, we have the following HTML:

<TABLE>
   <TR>    <!-- Row 1 -->
      <TH>Header 1</TH>
      <TH>Header 2</TH>
      ...
      <TH>Header n</TH>
   </TR>
   <TR>    <!-- Row 2 -->
      <TD>Data element 1</TD>
      <TD>Data element 2</TD>
      ...
      <TD>Data element n</TD>
   </TR>
   ...
   <TR>    <!-- Row m -->
      <TD>Data element 1</TD>
      <TD>Data element 2</TD>
      ...
      <TD>Data element n</TD>
   </TR>
</TABLE>

The HTML above makes for a nice template that you can use whenever starting a table. By filling in the headers and data elements with some genuine information, we can produce a table like the one you see in Figure 12.1.

Figure 12.1 : Displaying hockey team statistics or any other type of information presented in columns is a good use of HTML tables.

<TABLE>
   <TR>    <!-- Row 1 -->
      <TH>Player</TH>
      <TH>Goals</TH>
      <TH>Assists</TH>
      <TH>Points</TH>
   </TR>
   <TR>    <!-- Row 2 -->
      <TD>Anne</TD>
      <TD>7</TD>
      <TD>12</TD>
      <TD>19</TD>
   </TR>
   <TR>    <!-- Row 3 -->
      <TD>Eric</TD>
      <TD>4</TD>
      <TD>11</TD>
      <TD>15</TD>
   </TR>
   <TR>    <!-- Row 4 -->
      <TD>Jim</TD>
      <TD>10</TD>
      <TD>14</TD>
      <TD>24</TD>
   </TR>
</TABLE>

Alignment

The beauty of HTML tables is the precise control you have over the alignment of content in individual cells and over the table itself. There are two types of alignment that you can specify:

NOTE
You cannot specify vertical alignment for an entire table because a page's length isn't fixed like its width is.

Aligning the Entire Table

You can use the ALIGN attribute in the <TABLE> tag to specify how the table should be aligned relative to the browser window. Setting ALIGN to LEFT or RIGHT floats the table in the left or right margin, respectively. Floating tables behave much like floating images in that you can wrap text around them. This is how you produce a page element like a pull quote (see Figure 12.2).

Figure 12.2 : The text of a pull quote is contained in a floating table while the rest of the body text wraps around it.

Using the CENTER value of ALIGN centers the table in the browser window, though not all browsers support this. If you can't center a table this way, you can enclose the HTML that produces the table between the <CENTER> and </CENTER> tags. This should become unnecessary, though, as browsers come into compliance with the HTML 3.2 standard.

Alignment Within a Row

If you want the vertical or the horizontal alignment to be the same for every cell in a given row, you can use the VALIGN and ALIGN attributes in the row's <TR> tag. Any alignment specified in a <TR> tag will override all default alignments.

NOTE
The default vertical alignment for both header and data cells is MIDDLE. The default horizontal alignment depends on the type of cell: Header cells have a CENTER alignment and data cells have a LEFT alignment.

Alignment Within a Cell

HTML 3.2 permits alignment control all the way down to the cell level. You can prescribe vertical or horizontal alignments in both header and data cells by using the VALIGN or ALIGN attributes in <TH> or <TD> tags. Any alignment specified at the cell level overrides any default alignments and any alignments specified in a <TR> tag.

Setting alignments in individual cells represents the finest level of control you get over table alignment. In theory, you could manually specify vertical and horizontal alignments in every single cell of your tables if you needed to. Unfortunately, it's easy to get lost among all of those VALIGN and ALIGN attributes, especially when it comes to deciding which will take precedence. If you're having trouble mastering table alignment, just remember the following hierarchy:

Controlling Other Table Attributes

In addition to tweaking alignments, you have a say in other aspects of the tables you create as well. These include:

The next five sections walk you through each of these table features and discuss the HTML tags and attributes you need to know to produce them.

Adding a Caption

To put a caption on your table, enclose the caption text between the <CAPTION> and </CAPTION> tags. Captions appear centered over the table and the text may be broken to match the table's width (see Figure 12.3). You can also use physical style tags to mark up your caption text. The HTML to produce Figure 12.3 follows:

Figure 12.3 : A caption helps give readers a context for the information in your tables.

<TABLE>
   <CAPTION><B>Team Statistics - 1996-97 Season</B></CAPTION>
   <TR>    <!-- Row 1 -->
      <TH>Player</TH>
      <TH>Goals</TH>
      <TH>Assists</TH>
      <TH>Points</TH>
   </TR>
   <TR>    <!-- Row 2 -->
      <TD>Anne</TD>
      <TD>7</TD>
      <TD>12</TD>
      <TD>19</TD>
   </TR>
   <TR>    <!-- Row 3 -->
      <TD>Eric</TD>
      <TD>4</TD>
      <TD>11</TD>
      <TD>15</TD>
   </TR>
   <TR>    <!-- Row 4 -->
      <TD>Jim</TD>
      <TD>10</TD>
      <TD>14</TD>
      <TD>24</TD>
   </TR>
</TABLE>

If you prefer your caption below the table, you can include the ALIGN=BOTTOM attribute in the <CAPTION> tag.

TIP
Put your caption immediately after the <TABLE> tag or immediately before the </TABLE> tag to prevent your caption from unintentionally being made part of a table row or cell.

Setting the Width

The WIDTH attribute of the <TABLE> tag enables you to specify how wide the table should be in the browser window. You can set WIDTH to a specific number of pixels or to a percentage of the available screen width.

WIDTH is often used to force a table to occupy the entire width of the browser window. If we change the <TABLE> tag in the HTML code in the previous section to:

<TABLE WIDTH=100%>

the table is rendered as shown in Figure 12.4. The statistics are centered in their columns for easier readability.

Figure 12.4 : You can force a browser to make a table as wide as you want by using the WIDTH attribute of the tag.


TIP
Since you can't know how every user has set his or her screen width, you should set WIDTH equal to a percentage whenever possible. The only exception to this is if the table has to be a certain number of pixels wide to accommodate an image in one of the cells or to achieve a certain layout effect.

NOTE
Some browsers support the use of the WIDTH attribute in a <TD> or <TH> tag to control the width of individual columns. This is not proper HTML, according to the HTML 3.2 DTD, and support for using WIDTH in this way is tenuous, at best.

Adding a Border

You can place a border around your table by using the BORDER attribute of the <TABLE> tag. BORDER is set to the number of pixels wide you want the border to be. A version of our hockey statistics table with a two-pixel border is shown in Figure 12.5. The modified <TABLE> tag that accomplishes this effect is:

Figure 12.5 : Using a border explicitly separates neighboring columns and makes a table more readable.

<TABLE WIDTH=100% BORDER=2>

You can also set BORDER equal to zero. This means that no border will be used and that the browser should give back any space it has reserved to put in a border.

Earlier Implementation of BORDER
When the BORDER attribute was first implemented, it was a Boolean attribute, meaning that if the word BORDER appeared in a <TABLE> tag, there would be a border. If BORDER didn't appear in the <TABLE> tag, there was no border. This created some confusion when Netscape introduced the extension by which you could set BORDER equal to a number of pixels.
The major problem occurred in <TABLE> tags like:
<TABLE BORDER=0>
Netscape Navigator would interpret this to mean "don't use a border and give me back any space you reserved for a border." A browser that knew only the original implementation of the BORDER attribute would detect the word BORDER in the tag and render the table with a border-the complete opposite of what the user intended!
The HTML 3.2 DTD supports BORDER being set equal to a number of pixels, but it also includes a dummy attribute to handle any instances of people using BORDER as a Boolean attribute.

Spacing Within a Cell

The distance between an element in a cell and the boundaries of the cell is called cell padding. The CELLPADDING attribute of the <TABLE> tag lets you modify the amount of cell padding used in your tables. Typically, Web page authors increase the cell padding from its default value of 1 to put a little extra white space between the contents and the edges of a cell (see Figure 12.6). This gives the whole table a bit more "room to breathe." The <TABLE> tag used to produce Figure 12.6 is

Figure 12.6 : You can open up your table with some extra white space by increasing the cell padding.

<TABLE WIDTH=100% BORDER=2 CELLPADDING=6>

Spacing Between Cells

You also have control over the space between cells. By increasing the value of the CELLSPACING attribute of the <TABLE> tag, you can open a table up even further (see Figure 12.7). Notice that the border used between the cells also increases. The <TABLE> tag used in Figure 12.7 is

Figure 12.7 : Spacing between adjacent cells is controlled by the CELLSPACING attribute.

<TABLE WIDTH=100% BORDER=2 CELLSPACING=6>

Spanning Multiple Rows or Columns

By default, a cell occupies or spans one row and one column. For most tables, this is usually sufficient. When you start to use tables for layout purposes, though, you'll encounter instances where you want a cell to span more than one row or column. HTML 3.2 supports attributes of the <TH> and <TD> tags that permit this effect.

Using the COLSPAN Attribute

The COLSPAN attribute inside of a <TH> or <TD> tag instructs the browser to make the cell, defined by the tag, take up more than one column. You set COLSPAN equal to the number of columns the cell is to occupy.

COLSPAN is useful when one row of the table is forcing the table to be a certain number of columns wide while the content in other rows can be accommodated in a smaller number of columns. Figure 12.8 shows a table that makes good use of the COLSPAN attribute.

Figure 12.8 : The COLSPAN attribute lets rows with fewer elements (like the row with the Name field) occupy as many columns as rows with more elements.

Using the ROWSPAN Attribute

ROWSPAN works in much the same way as COLSPAN, except that it allows a cell to take up more than one row. Figure 12.9 shows a home page for which the layout was done with a table. The large image in the center is in a single cell that spans three rows (ROWSPAN=3).

Figure 12.9 : The seal in the middle of the DTSE&E; page takes up three rows thanks to the ROWSPAN attribute.

What Elements Can Be Placed in a Table Cell

HTML tables were developed with the intent of presenting columns of information, but that information does not necessarily have to be text-based. There are many types of page elements that you can place in a given table cell:

TIP
If you're planning on embedding a table within a table, it's helpful to do a pencil-and-paper sketch first. The sketch should help you code the tables more efficiently.

Tables as a Design Tool

While tables were developed for presenting columnar data, they have evolved to the point where they can do much more than that. There are three primary driving forces behind the rise of tables as a design tool:

The remaining sections in this chapter look at different examples of how tables can be used to design great-looking Web pages.

Creating a Complex Layout

Ziff-Davis' ZDNet site uses two tables on its main page layout. The first table places the three images at the very top of the page ("Why Microsoft Internet Explorer 3.0 makes a better browser," the navigation options, and the ZDNet plug). The second table creates the three- column layout you see near the bottom of the figure (see Figure 12.10).

Figure 12.10 : Multiple column layouts are fairly simple to do when you use HTML tables.

Aligning Custom Bullet Characters

All of the unordered lists on the American Communications Services, Inc. (ACSI) site use custom bullet characters-even nested unordered lists. Figure 12.11 shows how cleanly the main and subordinate bullet images can be aligned using tables. The subordinate bullets line up directly under a list item from the primary list. Additionally, list items in the subordinate list wrap to a point right under where the list item begins rather than to a point below the bullet character. The source code that produced this table is shown in Figure 12.12.

Figure 12.11 : Even nested bulleted lists with custom bullet characters can come off well with tables.

Figure 12.12 : The HTML source code for Figure 12.11 shows how the bullet character and the list item are in adjacent cells in the same row of a table.

Aligning Form Fields

Global One, the telecommunications joint venture formed by Deutsche Telekom, France Telecom, and Sprint, allows members of the press to join a mailing list to keep apprised of happenings within the company. Press members sign up by filling out the form you see in Figure 12.13. The alignment of the prompting text and form fields is made possible by placing each in a table cell (see Figure 12.14).

Figure 12.13 : Aligning form fields produces an easy-to-read form and makes it more likely that a user will make the effort to fill it out.

Figure 12.14 : By making promoting text and form input fields elements of a given row, you can make all of your form fields line up nicely.