HTML Tables (see HTML4.01 specs) provide a robust way of organizing information on a web page. They can be used both to construct an overall grid-style page layout as well as for presenting specific information in a tabular fashion.
<table summary=""> <tr> <th>1990</th> <th>2000</th> <th>type</th> </tr> <tr> <td>5.31</td> <td>4.2</td> <td>percent increase</td> </tr> <tr> <td>$30,333</td> <td>$46,576</td> <td> hardware<br />and<br />software </td> </tr> </table>Here is how it is rendered:
| 1990 | 2000 | type |
|---|---|---|
| 5.31 | 4.2 | percent increase |
| $30,333 | $46,576 |
hardware and software |
The control for table elements are in HTML-Kit's table tab. The icon on the far left is the most basic one which generates a starter table with one row and one cell. HTML-Kit more-or-less forces you to use the summary attribute.
A table consists of cells displayed horizontally between the <tr> </tr> tags. The td and th cells have the same function the difference is their different default style features:void, above, below, box, border hsides, vsides, lhs, rhs,
none, groups, rows, cols, all
<table summary="" border="1">causes the table to look like this:
| 1990 | 2000 | type |
|---|---|---|
| 5.31 | 4.2 | percent increase |
| $30,333 | $46,576 |
hardware and software |
<table summary="" border="1" cellpadding="20px">causes the table to look like this:
| 1990 | 2000 | type |
|---|---|---|
| 5.31 | 4.2 | percent increase |
| $30,333 | $46,576 |
hardware and software |
<table summary="" border="1" cellpadding="20px" cellspacing="8px">causes the table to look like this:
| 1990 | 2000 | type |
|---|---|---|
| 5.31 | 4.2 | percent increase |
| $30,333 | $46,576 |
hardware and software |
The cellpadding and cellspacing both have default values of 1. In many circumstances it is useful to eliminate this spacing, causing the following change in appearance (both have border="1"):
|
cellpadding="0"
cellspacing="0" | cellpadding, cellspacing unspecified | ||||||||||||||||||
|
|
<table summary="" border="1" width="400px">causes the table to look like this:
| 1990 | 2000 | type |
|---|---|---|
| 5.31 | 4.2 | percent increase |
| $30,333 | $46,576 |
hardware and software |
<table summary="" border="1" width="100%">causes the table to look like this:
| 1990 | 2000 | type |
|---|---|---|
| 5.31 | 4.2 | percent increase |
| $30,333 | $46,576 |
hardware and software |
The frame and rules attributes allow you to specify variations on where the lines appear. The frame specifies the lines forming the external box around the table and the rules specify the internal lines. Here are some examples.
<table summary="" border="1" frame="border" rules="none">causes the table to look like this:
| 1990 | 2000 | type |
|---|---|---|
| 5.31 | 4.2 | percent increase |
| $30,333 | $46,576 |
hardware and software |
<table summary="" border="1" frame="void" rules="all">causes the table to look like this:
| 1990 | 2000 | type |
|---|---|---|
| 5.31 | 4.2 | percent increase |
| $30,333 | $46,576 |
hardware and software |
<table summary="" border="1" frame="hsides" rules="rows">causes the table to look like this:
| 1990 | 2000 | type |
|---|---|---|
| 5.31 | 4.2 | percent increase |
| $30,333 | $46,576 |
hardware and software |
<table summary="" border="1" frame="hsides" rules="cols">causes the table to look like this:
| 1990 | 2000 | type |
|---|---|---|
| 5.31 | 4.2 | percent increase |
| $30,333 | $46,576 |
hardware and software |
<td></td> or <td> </td>
For example, the following code:
<table summary="" cellpadding="10px" border="1"> <tr> <td>a</td> <td> </td> </tr> <tr> <td>c</td> <td>d</td> </tr> </table>creates this appearance:
| a | |
| c | d |
<td> </td>With this modification the empty cell "looks right":
| a | |
| c | d |
left, center, right, justify, charwhen char is used, also specify charOff
top, middle, bottom, baseline
related to providing tabular information in alternative modalities, such as speech synthesis. Using these, the user may be able to query the contents of the table to discover specific information.
We'll use our example table to illustrate some of these features.| 1990 | 2000 | type |
|---|---|---|
| 5.31 | 4.2 | percent increase |
| $30,333 | $46,576 |
hardware and software |
<table summary="" border="1"> <tr align="left"> <th>1990</th> <th>2000</th> <th>type</th> </tr> <tr> <td>5.31</td> <td>4.2</td> <td>percent increase</td> </tr> <tr valign="top"> <td>$30,333</td> <td>$46,576</td> <td> hardware<br />and<br />software </td> </tr> </table>To see the difference, compare the two side by side:
|
previous version
|
modified version
|
<th align="right">type</th>we get the following effect:
| 1990 | 2000 | type |
|---|---|---|
| 5.31 | 4.2 | percent increase |
| $30,333 | $46,576 |
hardware and software |
<table summary="" border="1"> <tr> <th>1990</th> <th>2000</th> <th>type</th> </tr> <tr> <td align="center" colspan="2">5.31</td> <td>percent increase</td> </tr> <tr> <td>$30,333</td> <td>$46,576</td> <td> hardware<br />and<br />software </td> </tr> </table>creates this appearance:
| 1990 | 2000 | type |
|---|---|---|
| 5.31 | percent increase | |
| $30,333 | $46,576 |
hardware and software |
We are trying to convey the information that the number "5.31" applies to both years instead of repeating it. Whenever a colspan > 1 is used, we simply reduce the number of cells within a row appropriately.
Likewise, the rowspan attribute, when set to a number > 1 means that the cell covers that one in the row where it is declared and the row below it. This is a bit trickier to use, because you have to take away a cell in the next row! Here is an example:<table summary="" border="1"> <tr> <th>1990</th> <th>2000</th> <th>type</th> </tr> <tr> <td>5.31</td> <td align="center" rowspan="2">?</td> <td>percent increase</td> </tr> <tr> <td>$30,333</td> <td> hardware<br />and<br />software </td> </tr> </table>creates this appearance:
| 1990 | 2000 | type |
|---|---|---|
| 5.31 | ? | percent increase |
| $30,333 |
hardware and software |
The elements thead, tbody and tfoot are used to create logically separate row groups. See the (relevant HTML4.01 specs). Their attributes are the same as the tr attributes but they provide a more convenient way to apply a set of attributes to a group of rows at a time. Furthermore, the table lines can be specified to respect the groups.
The thead and tfoot groups are meant to function so that whatever rows are put into them go to the top and bottom of the table, respectively. However, I wouldn't count on this actually working. In fact, you really only need the tbody element since it can be used whenver you want to create a row group.
Let's revisit our working example from the last set of notes with border and cellpadding specified:<table summary="" border="1" cellpadding="10px"> <tr> <th colspan="3">Source: RTFM bulletin</th> </tr> <tr> <th>1990</th> <th>2000</th> <th>type</th> </tr> <tr> <td>5.31</td> <td>4.2</td> <td>percent increase</td> </tr> <tr> <td>$30,333</td> <td>$46,576</td> <td>hardware<br />and<br />software</td> </tr> </table>which gives us this appearance:
| Source: RTFM bulletin | ||
|---|---|---|
| 1990 | 2000 | type |
| 5.31 | 4.2 | percent increase |
| $30,333 | $46,576 | hardware and software |
<table summary="" border="1" cellpadding="10px"> <tr> <th colspan="3">Source: RTFM bulletin</th> </tr> <tbody align="right"> <tr> <th>1990</th> <th>2000</th> <th>type</th> </tr> <tr> <td>5.31</td> <td>4.2</td> <td>percent increase</td> </tr> <tr> <td>$30,333</td> <td>$46,576</td> <td>hardware<br />and<br />software</td> </tr> </tbody> </table>and here is its appearance:
| Source: RTFM bulletin | ||
|---|---|---|
| 1990 | 2000 | type |
| 5.31 | 4.2 | percent increase |
| $30,333 | $46,576 | hardware and software |
<table summary="" border="1" frame="box" rules="groups" cellpadding="10px"> <tr> <th colspan="3">Source: RTFM bulletin</th> </tr> <tr> <th>1990</th> <th>2000</th> <th>type</th> </tr> <tbody> <tr> <td>5.31</td> <td>4.2</td> <td>percent increase</td> </tr> <tr> <td>$30,333</td> <td>$46,576</td> <td>hardware<br />and<br />software</td> </tr> </tbody> </table>and here is its appearance:
| Source: RTFM bulletin | ||
|---|---|---|
| 1990 | 2000 | type |
| 5.31 | 4.2 | percent increase |
| $30,333 | $46,576 | hardware and software |
Unfortunately, the col and colgroup elements may not work uniformly across all browsers and using the width in td and th elements, although deprecated, often creates more portable HTML code.
Here is an example using the col element:<table summary="" border="1" cellpadding="10px"> <col span="2" width="200" ></col> <tr> <th colspan="3">Source: RTFM bulletin</th> </tr> <tr> <th>1990</th> <th>2000</th> <th>type</th> </tr> <tr> <td>5.31</td> <td>4.2</td> <td>percent increase</td> </tr> <tr> <td>$30,333</td> <td>$46,576</td> <td>hardware<br />and<br />software</td> </tr> </table>and here is its appearance:
| Source: RTFM bulletin | ||
|---|---|---|
| 1990 | 2000 | type |
| 5.31 | 4.2 | percent increase |
| $30,333 | $46,576 | hardware and software |
The colgroup elements constructs a column group much as the tbody elements construct row groups.
<table summary="" border="1" frame="box" rules="groups" cellpadding="10px"> <colgroup span="2"></colgroup> <tr> <th colspan="3">Source: RTFM bulletin</th> </tr> <tr> <th>1990</th> <th>2000</th> <th>type</th> </tr> <tr> <td>5.31</td> <td>4.2</td> <td>percent increase</td> </tr> <tr> <td>$30,333</td> <td>$46,576</td> <td>hardware<br />and<br />software</td> </tr> </table>has this appearance
| Source: RTFM bulletin | ||
|---|---|---|
| 1990 | 2000 | type |
| 5.31 | 4.2 | percent increase |
| $30,333 | $46,576 | hardware and software |
<table summary="" border="1" frame="box" rules="groups" cellpadding="10px"> <colgroup span="2"></colgroup> <tbody> <tr> <th colspan="3">Source: RTFM bulletin</th> </tr> </tbody> <tr> <th>1990</th> <th>2000</th> <th>type</th> </tr> <tbody valign="top"> <tr> <td>5.31</td> <td>4.2</td> <td>percent increase</td> </tr> <tr> <td>$30,333</td> <td>$46,576</td> <td>hardware<br />and<br />software</td> </tr> </tbody> </table>which looks like this:
| Source: RTFM bulletin | ||
|---|---|---|
| 1990 | 2000 | type |
| 5.31 | 4.2 | percent increase |
| $30,333 | $46,576 | hardware and software |
| 1990 | 2000 | type |
|---|---|---|
| 5.31 | 4.2 | percent increase |
| $30,333 | $46,576 |
hardware and software |
<table class="special" summary="" border="1">in conjunction with the rules:
table.special {
width: 400px;
}
table.special th {
text-align: left;
}
table.special td {
padding: 0px 10px 0px 10px;
vertical-align: bottom;
}
we get
| 1990 | 2000 | type |
|---|---|---|
| 5.31 | 4.2 | percent increase |
| $30,333 | $46,576 |
hardware and software |
table.redlines { border: solid 1px red; }
table.redlines td {
border: solid 1px red;
padding: 10px;
}
When applied to this table:
<table class="redlines"> <tr><td colspan="2">A</td></tr> <tr> <td>B</td> <td>C</td> </tr> </table>we get this result:
| A | |
| B | C |
<table cellspacing="0" class="redlines"> <tr><td colspan="2">A</td></tr> <tr> <td>B</td> <td>C</td> </tr> </table>yields the result:
| A | |
| B | C |
table.redlines1 {
border-top: solid 1px red;
border-right: solid 1px red;
}
table.redlines1 td {
border-left: solid 1px red;
border-bottom: solid 1px red;
padding: 10px;
}
When applied to this table:
<table cellspacing="0" class="redlines1"> <tr><td colspan="2">A</td></tr> <tr> <td>B</td> <td>C</td> </tr> </table>we get this result:
| A | |
| B | C |