Tables in HTML: A Tutorial: John W. Shipman
Tables in HTML: A Tutorial: John W. Shipman
Tables in HTML: A Tutorial: John W. Shipman
tutorial
John W. Shipman
2006-05-18 14:41
Table of Contents
1. Basic table building .................................................................................................................. 1
1.1. A simple table ............................................................................................................... 1
1.2. Captioning a table ......................................................................................................... 2
1.3. Specifying dimensions ................................................................................................... 2
1.4. Specifying horizontal alignment of cells .......................................................................... 2
1.5. Specifying vertical alignment for a whole row ................................................................. 3
2. Attributes of the <table> tag .................................................................................................. 3
3. Column and row headings ....................................................................................................... 4
4. Spanned cells ........................................................................................................................... 4
5. A look ahead: HTML 4.01 tables ............................................................................................... 5
5.1. Specifying the alignment for an entire column ................................................................. 5
5.2. Column grouping .......................................................................................................... 6
5.3. Table headers and footers .............................................................................................. 6
<table>
<tr><td>New Mexico <td>Santa Fe
<tr><td>Utah <td>Salt Lake City
</table>
1
http://www.w3.org/TR/html401/
2
http://www.nmt.edu/tcc/help/pubs/htmltables/
3
http://www.nmt.edu/tcc/help/pubs/htmltables.pdf
1.2. Captioning a table
To add a title to a table, place the caption text between <caption> and </caption> tags just after the
<table> tag:
<table>
<caption>Southwestern Capitals</caption>
<tr><td>New Mexico <td>Santa Fe
<tr><td>Utah <td>Salt Lake City
</table>
You can use an align="bottom" attribute in the <caption> tag to position the caption below the
table. The default placement is at the top. For example:
1.3. Specifying dimensions
In the tags that make up a table, there are a number of attributes that allow you to specify the relative
or absolute sizes of things. For example:
<table width="75%">…</table>
The width attribute of a <table> tag specifies how wide the table is, relative to the size of the browser
window. The above example says to make the table 3/4 the width of the window.
The next example sasys to make the table five and a half inches wide:
<table width="5.5in">...</table>
Warning
Hardly any browsers support this feature at the present.
For example, in this table, all the decimal points should line up:
<table>
<tr><td>kilo <td align="char">1000.
<tr><td>deci <td align="char">0.1
<tr><td>centi <td align="char">0.01
<tr><td>milli <td align="char">0.001
You can include a char="c" attribute to specify an alignment character c to use instead of other
than a period.
For example, this would line up the colons (:) in that column:
You can also include a charoff="dimension" attribute that says where to align things relative to
the cell size. For example, to specify that the point of alignment 1/3 of the width from the left column:
Again, a <td> tag can use a different valign attribute to override the row's vertical alignment in that
one cell.
<table cellpadding="0.25in">
<table>
<tr><th>Element name<th>Atomic number
<tr><td>Hydrogen<td>1
</table>
You can also provide row headings by starting each row with a <th> tags and then using <td> tags
for the remainder of the row.
4. Spanned cells
The items in your table are not necessarily restricted to the size of one cell.
Any cell (<th> or <td> tag) can have a rowspan="integer" or colspan="integer" attribute to
“span” more than one cell of the table, that is, place that element's content into an area that would nor-
mally be occupied by two or more cells.
<table>
<tr><th> <th colspan="2" align="center">Lifespan
<tr><th>Name <th>Born <th>Died
<tr><td>Louis ``Satchmo'' Armstrong <td>1900<td>1971
...
</table>
In this table, the word “Lifespan” will appear on the first heading row, centered over the second and
third columns. The second heading row will contain the column headings “Name”, “Born”, and “Died”.
Warning
Most browsers don't support the HTML 4.01 table specification yet. Here are some features you will be
able to use once they do.
<table>
<caption>Atomic numbers</caption>
<col align="left">
<col align="right">
<tr><td>Hydrogen<td>1
<tr><td>Helium<td>2
</table>
The first <col> tag says that the first column's cells will be left-justified. The second <col> tag says to
right-justify the cells of the second column.
You can make a <col> tag apply to more than one adjacent column by using the span=n attribute to
span n columns. For example, suppose you have a table with three left-justified columns followed by
two centered columns. That table might start like this:
<table>
<col align="left" span="3">
<col align="center" span="2">
<tr>...
</table>
You can still override the alignment in a particular cell by using an align attribute in that cell's <td>
tag. That attribute will affect only that one cell.
<table>
<col width="4cm">
<col width="2cm" align="right" span="3">
<tr>...
</table>
5.2. Column grouping
If you have groups of columns with the same attribute values, you can use a <colgroup> tag before
each group of <col> tags to specify attributes for that group. For example:
<table>
<colgroup align="center">
<col width="3pi">
<col width="5pi" span="4">
<colgroup align="right">
<col width="4pi">
<col width="8pi">
...
</table>
The example above defines seven columns. The first five are all centered and the next two are right-
justified.
<table>
<col align="left">
<col align="right">
<thead>
<tr><th>Element name<th>Atomic number
<tfoot>
<tr><th span="7">Does not include the new elements made
in Germany last week.
<tbody>
<tr><td>Hydrogen<td>1
...
</table>