Previous Contents Next
12. Creating Tables

Much of the information that appears on the Web is of a commercial or technical nature that fits naturally into tables. Here, row and columnar arrangements of data can transform them into useful information simply on the basis of their organization. Consider, for example, the use of spreadsheets, possibly the most widely used method of data presentation. Tabular arrangements, often in and of themselves, can help the viewer sort through masses of data to understand their underlying structure and substance. Although the purpose here is less dramatic, you will find the use of tables a quite convenient method, not only of presenting tabular information, but of arranging and presenting your entire web pages as well.

A Simple Table

A web table, like any other table, contains rows and columns. More specifically, within HTML, a table contains columns within rows. That is, when you define a table, you specify a row then specify the columns (better referred to as cells) that comprise that row. This structure can be seen in the following code that defines a simple 3x3 table.

<TABLE>
<TR>
   <TD>Cell 1.1</TD>
   <TD>Cell 1.2</TD>
   <TD>Cell 1.3</TD>
<TR>
   <TD>Cell 2.1</TD>
   <TD>Cell 2.2</TD>
   <TD>Cell 2.3</TD>
<TR>
   <TD>Cell 3.1</TD>
   <TD>Cell 3.2</TD>
   <TD>Cell 3.3</TD>
</TABLE>


The output from this table definitions is rendered in the browser as shown below:

Cell 1.1Cell 1.2Cell 1.3
Cell 2.1Cell 2.2Cell 2.3
Cell 3.1Cell 3.2Cell 3.3

The <TABLE >...</TABLE > tags define the extent of the table and must appear at the beginning and end of the table definitions. Within the <TABLE > tags are the table row <TR > tags, one for each row of the table. Finally, within each table row tag are coded the table data <TD> tags, one for each cell across the row.

Table Borders

It's a simple matter to add borders to a table. All that is required is to include the BORDER parameter as part of the <TABLE> tag: <TABLE BORDER> to produce the default border around the table and surrounding each cell.

Cell 1.1Cell 1.2Cell 1.3
Cell 2.1Cell 2.2Cell 2.3
Cell 3.1Cell 3.2Cell 3.3

Table Caption

Tables are captioned by using the <CAPTION> tag with its ALIGN=TOP|BOTTOM parameter (TOP is the default). The tag must appear inside the <TABLE> tag and its close tag </CAPTION> must be used. Any number of lines can comprise the caption, which is centered on the table. The following table specifications indicate a caption, but accept the default alignment at the top of the table:

<TABLE BORDER>
<CAPTION>
<FONT COLOR=GREEN>This is the Caption</FONT>
</CAPTION>
<TR><TD>Cell 1.1</TD><TD>Cell 1.2</TD><TD>Cell 1.3</TD>
<TR><TD>Cell 2.1</TD><TD>Cell 2.2</TD><TD>Cell 2.3</TD>
<TR><TD>Cell 3.1</TD><TD>Cell 3.2</TD><TD>Cell 3.3</TD>
</TABLE>

This is the Caption
Cell 1.1Cell 1.2Cell 1.3
Cell 2.1Cell 2.2Cell 2.3
Cell 3.1Cell 3.2Cell 3.3


Table Size

The table size is, by default, governed by the size of the data in its cells. That is, the width of a column or the length of a row is set by the amount of data in its largest cell. This can be clearly seen in the following table:

Cell 1.1Cell 1.2Cell 1.3
Cell 1.2 is a wide cellCell 2.2Cell 2.3
Cell 3.1Cell 3.2Cell 3.3

Later, you will learn how to override this default and specify exact sizes and formatting for rows and columns. For now, you can adjust the size of the table and provide more room within cells by using the WIDTH parameter. The width can be set to a certain number of pixels or, more likely, to a certain percentage of the browser window's width. For example, the following table has been defined with the table specification: TABLE BORDER WIDTH=75%. Note that the column with the widest text is still proportionately wider than the other columns.

Cell 1.1Cell 1.2Cell 1.3
Cell 1.2 is a wide cellCell 2.2Cell 2.3
Cell 3.1Cell 3.2Cell 3.3

BORDER Size

Let's now look as some of the parameters that control the appearance of borders and rules of a table. The table border can be set to a specific size through the BORDER=n parameter, which gives the width of the outside border in number of pixels. For example, the following table has it's border size set with <TABLE BORDER=5>:

Cell 1.1Cell 1.2Cell 1.3
Cell 2.1Cell 2.2Cell 2.3
Cell 3.1Cell 3.2Cell 3.3

By specifying a size, you can produce a three dimensional look to the table by adding shading around its outside edges.

BORDER Color (Internet Explorer only)

There are three ways to add color to borders. The BORDERCOLOR parameter renders the border in a single color on all sides of the table (eliminating the 3D look) and the rules separating the cells. For example, the specification <TABLE BORDER=5 BORDERCOLOR=RED> produces the following table:

Cell 1.1Cell 1.2Cell 1.3
Cell 2.1Cell 2.2Cell 2.3
Cell 3.1Cell 3.2Cell 3.3

However, you can assign individual colors to the dark (right and bottom) and light (left and top) portions of the border with the BORDERCOLORDARK and BORDERCOLORLIGHT parameters. For example, the specification <TABLE BORDER=5 BORDERCOLORDARK=RED BORDERCOLORLIGHT=SILVER> produces the table:

Cell 1.1Cell 1.2Cell 1.3
Cell 2.1Cell 2.2Cell 2.3
Cell 3.1Cell 3.2Cell 3.3

Note how the colors of the rules surrounding the cells are changed also by these specifications. The default value of BORDERCOLORDARK is GRAY and the default value of BORDERCOLORLIGHT is WHITE if you do not include either of these parameters.

Background Color

A color can be specified for the background of the table using the BGCOLOR parameter. For example, the specification <TABLE BORDER=5 BGCOLOR=YELLOW> produces the following:

Cell 1.1Cell 1.2Cell 1.3
Cell 2.1Cell 2.2Cell 2.3
Cell 3.1Cell 3.2Cell 3.3

Background Picture (Internet Explorer only)

Table backgrounds may be pictures as well as colors, just as you can provide a background texture for an entire page. This technique uses the BACKGROUND parameter in the same way as it is used in the <BODY> tag for a page--by specifying a gif or jpg file: <TABLE BORDER=5 BACKGROUND=texture.jpg>:

Cell 1.1Cell 1.2Cell 1.3
Cell 2.1Cell 2.2Cell 2.3
Cell 3.1Cell 3.2Cell 3.3

Table Rules (Internet Explorer only)

In addition to the table border, you can control display of the rules between cells by using the RULES parameter in the <TABLE> tag. Three choices are: RULES=NONE to eliminate all rules surrounding cells, RULES=ROWS to display only the horizontal rules between rows, and RULES=COLS to only display the vertical rules between columns:

<TABLE RULES=NONE>
Cell 1.1Cell 1.2Cell 1.3
Cell 2.1Cell 2.2Cell 2.3
Cell 3.1Cell 3.2Cell 3.3

<TABLE RULES=ROWS>
Cell 1.1Cell 1.2Cell 1.3
Cell 2.1Cell 2.2Cell 2.3
Cell 3.1Cell 3.2Cell 3.3

<TABLE RULES=COLS>
Cell 1.1Cell 1.2Cell 1.3
Cell 2.1Cell 2.2Cell 2.3
Cell 3.1Cell 3.2Cell 3.3

Table Alignment

As with most other elements that appear on a page, a table can be positioned horizontally to align at the left of the window (default) or on the right. This specification is given by the ALIGN=LEFT|RIGHT parameter of the <TABLE >tag. The tag <TABLE ALIGN=RIGHT> produces the display you see here.
Cell 1.1Cell 1.2Cell 1.3
Cell 2.1Cell 2.2Cell 2.3
Cell 3.1Cell 3.2Cell 3.3

Note that the ALIGN parameter, just as it did with graphic images, permits text to wrap around the table. In this case, though, there is no comparable HSPACE or VSPACE parameter to control the amount of blank space surrounding the table.

You can, though, use the <BR CLEAR=RIGHT> tag following the table specification to isolate the table and avoid word wrap around it.

In order to center the table, you'll need to surround the entire table specification with the <CENTER>... </CENTER > tags.

Cell Padding

Cell padding refers to the amount of space surrounding the text within a cell. Without padding, text runs up against the rules separating the rows and columns. Cell padding is given in the <TABLE> tag by the CELLPADDING=n parameter, which denotes the number of pixels to leave between the text and the cell borders. The following tables give two examples:

Without CELLPADDING
Cell 1.1Cell 1.2Cell 1.3
Cell 2.1Cell 2.2Cell 2.3
Cell 3.1Cell 3.2Cell 3.3

CELLPADDING=5
Cell 1.1Cell 1.2Cell 1.3
Cell 2.1Cell 2.2Cell 2.3
Cell 3.1Cell 3.2Cell 3.3

Cell Spacing

Cell spacing refers to the amount of space, in pixels, between the cells of a table, or between the border and a cell. It is, in effect, the size of the rules between rows and columns. The following two tables illustrate use of the CELLSPACING=n parameter of the <TABLE> tag.

Without CELLSPACING
Cell 1.1Cell 1.2Cell 1.3
Cell 2.1Cell 2.2Cell 2.3
Cell 3.1Cell 3.2Cell 3.3

CELLSPACING=5
Cell 1.1Cell 1.2Cell 1.3
Cell 2.1Cell 2.2Cell 2.3
Cell 3.1Cell 3.2Cell 3.3

Row Colors

Previous specifications have considered features of a table as a whole. Now, let's look at some of the attributes that can be applied to the individual rows of a table.

One of those attributes is color. By using the BGCOLOR parameter in a table row tag <TR>, you can set the background color of the row:


<TABLE BORDER=5 CELLSPACING=5>
<TR BGCOLOR=RED>
<TD>Cell 1.1</TD><TD>Cell 1.2</TD><TD>Cell 1.3</TD>
<TR BGCOLOR=WHITE>
<TD>Cell 2.1</TD><TD>Cell 2.2</TD><TD>Cell 2.3</TD>
<TR BGCOLOR=BLUE>
<TD>Cell 3.1</TD><TD>Cell 3.2</TD><TD>Cell 3.3</TD>
</TABLE>

Cell 1.1Cell 1.2Cell 1.3
Cell 2.1Cell 2.2Cell 2.3
Cell 3.1Cell 3.2Cell 3.3


Row Textures and Rule Colors (Internet Explorer only)

By the same token, you can apply a background image or texture to a row by using the <TR BACKGROUND=filename.ext> in the same way you do when applying a background to a page or entire table.

Or, you can apply color to only the rules surrounding the cells in a row by using the BORDERCOLOR=color parameter within the <TR> tag. The following table sets a different border color for each of the rows:

Cell 1.1Cell 1.2Cell 1.3
Cell 2.1Cell 2.2Cell 2.3
Cell 3.1Cell 3.2Cell 3.3

If you wish, you can even adjust the colors of the dark (right and bottom) and light (left and top) portions of the rules. In this case use the BORDERCOLORDARK=color and BORDERCOLORLIGHT=color parameters with the chosen colors.

Text Alignment within Rows

By default, any text that appears in a table is aligned horizontally to the left of a cell and vertically in the middle of a cell. This can be seen in the following two-cell table with different amounts of text in the columns. Notice the centered alignment in the left column (the right column is also center aligned but is not noticeable since the text fills the cell).

This is the text that appears
in the first column of the table.
This is the text that appears in
the second column of the
table. It is longer than the text
that appears in the first
column of the table.

There are row specifications, however, to permit you to control this alignment. The <TR> tag uses the VALIGN parameter to provide four methods of vertical alignment: TOP, MIDDLE (default), BOTTOM, and BASELINE. These methods are illustrated below:

<TR VALIGN=TOP>
This is the text that appears
in the first column of the table.
This is the text that appears in
the second column of the
table. It is longer than the text
that appears in the first
column of the table.

<TR VALIGN=BOTTOM>
This is the text that appears
in the first column of the table.
This is the text that appears in
the second column of the
table. It is longer than the text
that appears in the first
column of the table.

<TR VALIGN=BASELINE>
This is the text that appears
in the first column of the table.
This is the text that appears in
the second column of the
table. It is larger than the text
that appears in the first
column of the table.

The BASELINE specification comes into play when there are different font sizes across a row. This parameter aligns the text in adjoining cells so that they share a common baseline to improve their appearance.

Column and Row Headings

The <TH> (table header) tag functions like a table row <TR> tag. It defines a row in a table. At the same time, it can include specifications in the form of the COLSPAN=n parameter to combine columns along the row, primarily for the purpose of adding column headings that span more than one column. An example should clarify.

<TABLE BORDER=5 CELLSPACING=5>
<TH COLSPAN=2>Heading 1</TH><TH COLSPAN=1>Heading 2</TH>
<TR><TD>Cell 1.1</TD><TD>Cell 1.2</TD><TD>Cell 1.3</TD>
<TR><TD>Cell 2.1</TD><TD>Cell 2.2</TD><TD>Cell 2.3</TD>
<TR><TD>Cell 3.1</TD><TD>Cell 3.2</TD><TD>Cell 3.3</TD>
</TABLE>

Heading 1Heading 2
Cell 1.1Cell 1.2Cell 1.3
Cell 2.1Cell 2.2Cell 2.3
Cell 3.1Cell 3.2Cell 3.3


Notice in the above code that four rows have been defined: the <TR> tags define the three rows of the body of the table, each containing three columns (TD); above them is a line of <TH> (heading) tags defining the initial row of the table. Each of the <TH> tags specifies the number of columns to span in order to provide room for a common heading over those columns, along with the heading text itself. As a result, you get a heading over the first two columns (COLSPAN=2) and a second heading over the last column (COLSPAN=1). The <TH> tag automatically emphasizes (bolds) the text of the heading.

Likewise, you can span rows to provide room for row headings with the same <TR> tag, although this time you use the ROWSPAN=n parameter:


<TABLE BORDER=5 CELLSPACING=5>
<TR><TH ROWSPAN=2>Heading 1</TH><TD>Cell 1.1</TD><TD>Cell 1.2</TD><TD>Cell 1.3</TD>
<TR><TD>Cell 2.1</TD><TD>Cell 2.2</TD><TD>Cell 2.3</TD>
<TR><TH ROWSPAN=1>Heading 2</TH><TD>Cell 3.1</TD><TD>Cell 3.2</TD><TD>Cell 3.3</TD>
</TABLE>

Heading 1Cell 1.1Cell 1.2Cell 1.3
Cell 2.1Cell 2.2Cell 2.3
HeadingCell 3.1Cell 3.2Cell 3.3


Incidentally, you can you the ALIGN parameter in the <TH> tag (along with the COLSPAN and ROWSPAN parameters) to align the heading to the LEFT, to the RIGHT, or in the CENTER (default). If you do not need for column or row headings to span cells, then it is just as easy to place headings within additional columns or rows in the table.

If you're the adventuresome sort, you might want to try spanning individual cells within a table. You can add COLSPAN and ROWSPAN attributes to the <TD> tags to produce some interesting table structures:


<TABLE BORDER=5 CELLSPACING=5>
<TR><TD COLSPAN=2>Cell 1.1</TD><TD>Cell 1.2</TD><TD ROWSPAN=2>Cell 1.3</TD>
<TR><TD>Cell 2.1</TD><TD ROWSPAN=2>Cell 2.2</TD><TD>Cell 2.3</TD>
<TR><TD>Cell 3.1</TD><TD>Cell 3.2</TD><TD>Cell 3.3</TD>
</TABLE>

Cell 1.1Cell 1.2Cell 1.3
Cell 2.1Cell 2.2Cell 2.3
Cell 3.1Cell 3.2Cell 3.3


Tables Within Tables

HTML also handles tables within tables for occasions calling for them. This doesn't involve any particular complications--you simply insert a table in place of a cell:

Cell 1.1 Cell 1.2 Cell 1.3
Cell 2.1 Cell 2.2
Cell A.1 Cell A.2
Cell B.1 Cell B.2
Cell 3.1 Cell 3.2 Cell 3.3

The following code produced the above table. Notice at the location of Cell 2.3 that a table itself has become the table data (TD) item.


<TABLE BORDER=3 CELLSPACING=5>
<TR>
<TD>Cell 1.1</TD>
<TD>Cell 1.2</TD>
<TD>Cell 1.3</TD>
</TR>
<TR>
<TD>Cell 2.1</TD>
<TD>Cell 2.2</TD>
-------------------------------------------------
<TD>(Instead of Cell 2.3)
<TABLE BORDER=5 CELLSPACING=5>
<TR>
<TD>Cell A.1</TD>
<TD>Cell A.2</TD></TR>
<TD>Cell B.1</TD>
<TD>Cell B2</TD></TR>
</TABLE>
</TD>
--------------------------------------------------
<TR>
<TD>Cell 3.1</TD>
<TD>Cell 3.2</TD>
<TD>Cell 3.3</TD></TR>
</TABLE>


This section has covered a lot of details about tables and their use in presenting tabular data. However, it cannot possibly discuss all the different variations in the use of the available tags. You will just have to experiment with table arrangements to discover which combinations of tags works best for your presentation.


Previous Contents Next