HTML - Tables

This page demonstrates the use of tables. See Dynamic vs. Fixed for an article on setting up tables.


Tables are composed of rows and columns. The intersection of a row and column is called a cell.

Features
Anything can be put into a cell that can be put into the BODY of a HTML document - images, links, <HR>, or another table
The table and/or individual cells can be aligned
The size of borders, margins, and the color of cells and/or the table can be specified.

Sample Code

This code
<TABLE WIDTH=50% BORDER=5 BGCOLOR="fcac66">
<CAPTION ALIGN=BOTTOM> Example Table</CAPTION>
<TR>
<TD>Row 1, col 1</TD><TD>Row 1, col 2</TD> </TR>
<TR>
<TD>Row 2, col 1</TD><TD>Row 2, col 2</TD> </TR>
</TABLE>
generates the table:

Example Table
Row 1, col 1Row 1, col 2
Row 2, col 1Row 2, col 2

Attributes

The basic terms: Table layout

<TABLE>
BGCOLORThis specifies the color of the table
WIDTHThis specifies the table width in pixels or as % of the page
ALIGNThis allows text to flow around the table; default is CENTER; other options are LEFT or RIGHT
BORDERThis sets the thickness of the border. The default is 1 pixel; using BORDER=0 makes the border invisible.
CELLSPACINGThis specifies the number of pixels between cells and between cells and the border. The default is 2.
CELLPADDINGThis specifies the number of pixels between cell data and the cell border. The default is 1.
HSPACEThis specifies the number of pixels to the left and right of the table. This attribute is not recognized unless ALIGN is used.
VSPACEThis specifies the number of pixels above and below the table. This attribute is used with ALIGN.
VALIGNThis attribute sets the vertical alignment of all cells in the table.
<TR>
ALIGNThis determines the horizontal alignment of data in the cells within this row. The options are LEFT, RIGHT, or CENTER (default).
BGCOLORThis specifies the color of the cells in the row.
VALIGNThis attribute aligns data vertically within cells of the row. The options are TOP, BOTTOM, BASELINE, and CENTER (default).
<TD>, <TH>
ALIGNThis determines the horizontal alignment of data within this cell. The options are LEFT, RIGHT, or CENTER (default).
BGCOLORThis specifies the color of this cell.
VALIGNThis attribute aligns data vertically within this cell. The options are TOP, BOTTOM, BASELINE, and CENTER (default).
WIDTHThis specifies the width of the cell. One can use either absolute pixels or a % of the table.
COLSPANThis allows the cell to span more than one column.
ROWSPANThis allows a cell to span more than one row.
<CAPTION>
ALIGNNetscape uses this attribute to place caption above or below the table. The default is above; use BOTTOM to place below. Internet Explorer uses the ALIGN attribute to horizontially position the caption: the options are LEFT, RIGHT, or CENTER.
VALIGNInternet Explorer uses this attribute with values TOP (default) or BOTTOM to place the caption above or below the table.
Important Note
The order of precedence of attribute is that cells have precedence over row attributes and row attributes have precedence over table attributes.

Example of multiple spanning rows and columns

The table
This spans 2 rows1 Col wideThis spans 2 cols
Row 2, col 2Row 2, col 3Row 2, col 4
is generated by the source code:
<TABLE WIDTH=50% BORDER >
<TR>
<TD ROWSPAN=2>This spans 2 rows</TD>
<TD>1 Col wide</TD>
<TD COLSPAN=2>This spans 2 cols</TD>
</TR>
<TR>
<TD>Row 2, col 2</TD>
<TD>Row 2, col 3</TD>
<TD>Row 2, col 4</TD> </TR>
</TABLE>

Making an image with a table

Large images can be loaded by chopping into pieces which are placed into the cells of a table. As a simple example the following code produces an image built from a table with two rows.
<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 >
<TR><TD ><img src="media/ch_top.jpg"></TD></TR>
<TR><TD ><img src="media/ch_bot.jpg"></TD></TR>
</TABLE>

View the result.