XHTML Guide

Introduction Document Structure Style Sheets Text Formatting Lists Links Tables Images Forms Frames Appendix - XHTML Elements Site Home

XHTML Reference Guide

Tables

Abstract

Tables can be used to arange text and graphics in multiple columns and rows. They have been traditionally used as a layout device for the whole web page, but this is better achieved by the use of style sheets.

Many of the attributes of the table elements have now been deprecated. In the sections below I have only used those attributes still supported in strict XHTML. In all instances the use of style sheets is recommended except where otherwise stated.

Top

Defining a Table

A table is defined by the <table> element. The <tr> (table row) element defines a row in the table. Each cell in a row is defined by the <td> (table data) element. There is also a <th> element similar to <td> which can be used for column headings. It works the same as the <td> element but some browsers will display the text centered and in bold. The following example defines a table with 3 rows (the first a header row) and 3 columns.

<table>
   <tr>
      <th>......</th>
      <th>......</th>
      <th>......</th>
   </tr>
   <tr>
      <td>......</td>
      <td>......</td>
      <td>......</td>
   </tr>
   <tr>
      <td>......</td>
      <td>......</td>
      <td>......</td>
   </tr>
</table>

With the exception of the <th> element which is optional, all these elements are required to define a table.

Top

Additional Table Elements

In additon to the required table elements discussed above there are other optional ones.

<caption>
As the name suggests it provides a way of giving a caption to a table. It is displayed above the actual table.
<thead>
Defines a row which can be used for table header information.
<tfoot>
Defines a row which can be used for table footer information.
<tbody>
Defines the body of the table.

It should be noted that <tfoot> must appear before the <tbody> element in the table definition.

<table summary="Demonstrates the use of all available table elements to provide maximum accessibility">
   <caption>This is the table caption</caption>
   <thead style="background-color: red;">
      <tr>
         <td>Col 1 Heading</td>
         <td>Col 2 Heading</td>
         <td>Col 3 Heading</td>
      </tr>
   </thead>
   <tfoot style="background-color: green;">
      <tr>
         <td>Col 1 Footer</td>
         <td>Col 2 Footer</td>
         <td>Col 3 Footer</td>
      </tr>
   </tfoot>
   <tbody style="background-color: yellow;">
      <tr>
         <td>Cell A1</td>
         <td>Cell B1</td>
         <td>Cell C1</td>
      </tr>
      <tr>
         <td>Cell A2</td>
         <td>Cell B2</td>
         <td>Cell C2</td>
      </tr>
   </tbody>
</table>
This is the table caption
Col 1 HeadingCol 2 HeadingCol 3 Heading
Col 1 FooterCol 2 FooterCol 3 Footer
Cell A1Cell B1Cell C1
Cell A2Cell B2Cell C3

As can be seen, these elements allow rows to be easily grouped for applying different styles to them. In terms of accessibility it is recommended that these extra elements are used. Note also the use of the summary attribute which provides a summary of the purpose of the table which is useful for speech-synthesizing/non-visual browsers.

Top

Adding Borders

By default no borders are displayed. If borders are required, you may use the border attribute of the <table> element. Any non-zero value makes a border round the table (3D effect) and gridlines around each cell. The value assigned to the attribute dictates the width of the table border in pixels. It does not affect the gridlines around the cells. For instance, by giving values of '0', '1' and '5' to the border attribute in the following example gives the results as seen after the code.

<table border="...">
   <tr>
      <th>Team</th>
      <th>League</th>
      <th>Division</th>
   </tr>
   <tr>
      <td>NEC Harlequins</td>
      <td>Zurich</td>
      <td>Premiership</td>
   </tr>
   <tr>
      <td>Worcester</td>
      <td>National</td>
      <td>One</td>
   </tr>
   <tr>
      <td>Rosslyn Park</td>
      <td>National</td>
      <td>Two</td>
   </tr>
</table>

displays in the following ways:

border="0" (default value)
TeamLeagueDivision
NEC HarlequinsZurichPremiership
WorcesterNationalOne
Rosslyn ParkNationalTwo
border="1"
TeamLeagueDivision
NEC HarlequinsZurichPremiership
WorcesterNationalOne
Rosslyn ParkNationalTwo
border="5"
TeamLeagueDivision
NEC HarlequinsZurichPremiership
WorcesterNationalOne
Rosslyn ParkNationalTwo
Top

Sizing a Table

The size of a table and its cells will, by default, automatically expand to fit the data, both in width and height. However they may be controlled by the width attribute or the CSS width and height property.

There are also another two elements which assist in the formatting of a table, <colgroup> and <col>.

The <colgroup> element defines groups of table columns and allows them to be grouped for formatting. The <col> element is contained within the <colgroup> element and is used when you want to specify different attribute values to a column within a colgroup.

The width attribute is available for the <table>, <colgroup> and the <col> elements and the CSS width property can be applied to all these as well as the <col> element. The values of the width attribute are expressed as a percentage or a pixel value.

<table width="50%">
<col width="50">

A percentage value will expand the table width to the given percentage of the available space. i.e. 100% will expand the table as far as it can, and 50% will only expand it half way across the available space. If column widths are expressed as a percentage, they will be that percentage of the table width. Note that it will only do this as far as the enclosed text or image permits. If the smaller cell contains more text than the larger cell the percentage used will be approximated to the requested values.

The height of cells can be controlled by applying the CSS height property to the <table>, <tr> or <td>elements. Any height value applied to a single cell will of course also affect the other cells in that row as will the width value affect the other cells in that column.

My personal preference is to define coloumn widths in a <colgroup> element and row heights in the <tr> element.

<table width="500">
   <colgroup>
      <col width="20%" />
      <col width="80%" />
   </colgroup>
   <tr>
      <td>narrow cell</td>
      <td >wide cell</td>
   </tr>
   <tr style="height: 75px">
      <td>a taller narrow cell</td>
      <td >a taller wide cell</td>
   </tr>
</table>

displays as:

narrow cell wide cell
a taller narrow cell a taller wide cell

If absolute pixel values are used instead of %ages, the sum of the cell widths should equal any width specified in the <table> element.

<col width="100" />
<col width="400" />

If there are more columns than <col> tags, the remaining columns will divide the remaining browser window space equally amongst themselves.

Note that the width attribute for the <td> element has been deprecated.

Top

Aligning Data

Data within a cell may aligned both horizontally and vertically using the align and valign attributes. Note that the align attribute used in the <table> element has been deprecated.

<table border="1" width="650">
   <colgroup>
      <col width="25%"/>
      <col width="25%" />
      <col width="50%"/>
   </colgroup>
   <tr style="height: 100px">
      <td valign="middle" align="center">This text is centered horizontally and vertically.</td>
      <td >No alignment, defaults to top/left.</td>
      <td valign="bottom" align="right">This text is aligned to the bottom and right.</td>
   </tr>
</table>

displays as:

This text is centered horizontally and vertically. No alignment, defaults to top/left. This text is aligned to the bottom and right.

Note that the centering value for the vertical alignment attribute is "middle" not "center".

Top

Spanning Cells

A cell can span more than one column by specifying <td colspan="2">. It has the effect of merging cells. This can also be applied to a header cell. More than one row can also be spanned by using the rowspan attribute.

<table border="1">
   <tr>
      <th>Column A</th>
      <th colspan="2">Columns B & C</th>>
   </tr>
   <tr>
      <td>A1</td>
      <td>B1</td>
      <td>C1</td>
   </tr>
   <tr>
      <td>A2</td>
      <td>B2</td>
      <td rowspan="2">C2</td>
   </tr>
   <tr>
      <td >A3</td>
      <td>B3</td>
   </tr>
   <tr>
      <td colspan="2" >A4</td>
      <td>C4</td>
   </tr>
</table>

displays as:

Column AColumns B & C
A1B1C1
A2B2C2
A3B3
A4C4
Top

Miscellaneous Formatting

Space around and within the table cells can be controlled by using the cellpadding and cellspacing attributes. cellspacing sets the width of the border between cells. It does not change the outside border defined by the border attribute, it just controls the space between the outline border and outer cells and the inner gridlines.

Column AColumn BColumn C
A1B1C1
A2B2C2
A3B3C3
A4B4C4

In the above example the table tag is <table border="1" cellspacing="1">. The effect of changing the cellspacing to 5 is shown below.

Column AColumn BColumn C
A1B1C1
A2B2C2
A3B3C3
A4B4C4

Note that the border value has not changed. If we now set that value to 5 the result is seen below.

Column AColumn BColumn C
A1B1C1
A2B2C2
A3B3C3
A4B4C4

cellpadding controls the amount of space between the cell border and text.

Column AColumn BColumn C
A1B1C1
A2B2C2
A3B3C3
A4B4C4

In this example the table tag is <table border="1" cellpadding="5">. This is slightly useful for providing leading and trailing space to text in a cell, but does also change both the horizontal and vertical space and so a high value can give the impression of text floating in the middle of a cell. The use of the CSS padding property can overcome this problem.

The attribute for setting the background colour of a table or cell is now deprecated so the CSS property background-color should be used instead. This may be applied to the whole table, parts of the table or individual cells. There is an example of its use in the Additonal Elements section above.

Top

Valid XHTML 1.0! Valid CSS2