table ElementThe table element is a block-level element declared by the XHTML 1.1 Tables Module
Elements in the Tables Module are:table | caption | col | colgroup | thead | tfoot | tbody | tr | th | td
The table element is used to create a structured table of data within a document.
In its simplest form, the table element contains a number of tr elements. Each
tr element represents a single table row and contains a combination of th and/or
td elements, each of which specifies a single table cell. If a caption is required
for the table, an optional caption element may be included as the first child of
the table element.
Table header cells should be represented by th elements and table data cells by
td elements. If a cell is both data and a header (as is each of the cells containing
Element Names - e.g. Calcium - in the example below) the td element should be used in
preference to the th element, with either a scope attribute or
headers attributes being used to indicate the cells for which it is a header.
For example, here is a basic table with a caption:
<table>
<caption>Chemical Symbols and Atomic Numbers of the Alkaline Earth Metals</caption>
<tr><th scope="col" abbr="Name">Element Name</th>
<th scope="col" title="Chemical Symbol">Symbol</th>
<th scope="col">Atomic Number</th>
</tr>
<tr><td scope="row">Beryllium</td> <td>Be</td> <td> 4</td></tr>
<tr><td scope="row">Magnesium</td> <td>Mg</td> <td>12</td></tr>
<tr><td scope="row">Calcium</td> <td>Ca</td> <td>20</td></tr>
<tr><td scope="row">Strontium</td> <td>Sr</td> <td>38</td></tr>
<tr><td scope="row">Barium</td> <td>Ba</td> <td>56</td></tr>
<tr><td scope="row">Radium</td> <td>Ra</td> <td>88</td></tr>
</table>
This table renders as:
| Element Name | Symbol | Atomic Number |
|---|---|---|
| Beryllium | Be | 4 |
| Magnesium | Mg | 12 |
| Calcium | Ca | 20 |
| Strontium | Sr | 38 |
| Barium | Ba | 56 |
| Radium | Ra | 88 |
The number of columns in the table is equal to the number of columns in the
the row with the most columns. (What a sentence!)
In the example above, it is easy to see that every row has three cells
and three columns - and so the table itself has three columns.
However, any cell may be made to span more than one column (by adding a
colspan attribute) or more than one row (by adding a rowspan
attribute). These attributes must therefore be taken into account when
determining the number of columns in a given row: rowspan attributes
(of greater than "1") will affect the number of columns in subsequent rows,
whereas colspan attributes (of greater than "1") will affect the number
of columns in that row itself (and also in subsequent rows if combined
on a single cell with a rowspan of greater than "1").
Once the number of columns in the table has been calculated,
any row containing fewer columns should be padded by the user agent with
empty cells until it does have this number of columns.
These empty cells should be added to the end of
the row (i.e. at the right for left-to-right tables and at the left for
right-to-left tables - see the dir attribute).
In my own opinion, however, it is good practice to give each row the
same number of columns within the code in the first place.
Any table cell's contents may be assigned an abbreviation (using the abbr attribute) or
longer, explanatory text (using the common title attribute). The table element
itself may also be given a summary attribute containing a description of the
logical structure of the table for the benefit of non-sighted users employing
assistive technology.
More complex tables can be split into row groups (using
thead, tfoot and tbody elements) and/or
column groups (using the colgroup element). The elements
thead and tfoot are both optional but if you do use
either in a table you must also include at least one tbody element.
Any given table may not have more than one thead or tfoot
element, though - see the Content Model below.
There is also the col element,
which does not define a column group
but merely allows easier styling of one or more
columns. (The styling options available for
columns is very limited, however:
see the last point in
the list below.)
There are a number of cases in which this grouping
might be necessary or desirable for your table.
For example
scope="colgroup" or
scope="rowgroup" on a cell (unless the column
group or row group comprises all of the table's
columns or rows)align and valign attributes on both
colgroup and col elements and also by the CSS specification,
which states that the only properties allowed on columns are
width,
background, border
and visibility
(see *CSS2 Specification: Column selectors).
In fact, even border and visibility are not widely
and consistently supported.
Some browsers may allow more styling than this on columns, but this is not standard
behaviour and will not be cross-browser compatible.The example table above (with some visual styling and an extra heading added) can be written in the complex table structure as follows:
<table style="width:40em" cellspacing="2" cellpadding="3" border="1">
<caption>Chemical Symbols and Atomic Numbers of the Alkaline Earth Metals</caption>
<colgroup span="1" width="40%" style="background:#fcc"></colgroup>
<colgroup style="background:#ccf">
<col width="20%" />
<col width="40%" />
</colgroup>
<thead align="right">
<tr><th rowspan="2" scope="col" abbr="Name">Element Name</th>
<th colspan="2" scope="colgroup" align="center">Data</th>
</tr>
<tr>
<th scope="col" title="Chemical Symbol">Symbol</th>
<th scope="col">Atomic Number</th>
</tr>
</thead>
<tbody align="right">
<tr><td scope="row">Beryllium</td> <td>Be</td> <td> 4</td></tr>
<tr><td scope="row">Magnesium</td> <td>Mg</td> <td>12</td></tr>
<tr><td scope="row">Calcium</td> <td>Ca</td> <td>20</td></tr>
<tr><td scope="row">Strontium</td> <td>Sr</td> <td>38</td></tr>
<tr><td scope="row">Barium</td> <td>Ba</td> <td>56</td></tr>
<tr><td scope="row">Radium</td> <td>Ra</td> <td>88</td></tr>
</tbody>
</table>
This table renders as:
| Element Name | Data | |
|---|---|---|
| Symbol | Atomic Number | |
| Beryllium | Be | 4 |
| Magnesium | Mg | 12 |
| Calcium | Ca | 20 |
| Strontium | Sr | 38 |
| Barium | Ba | 56 |
| Radium | Ra | 88 |
The example above uses the deprecated style attribute,
for ease of demonstration, but ordinarily these styles
should be contained within either an external stylesheet or
a style element in the head of the document.
However, since I am using the style attribute on this page, I have included
a Content-Style-Type HTTP
header of "text/css".
For any given table, every row group should contain the same number of columns.
This is the number of columns in the table:
it should always be equal to the value calculated from any colgroup or
col elements (if present), taking account of any relevant span attributes.
Horizontal alignment of cell contents may be specified using the align attribute
and vertical alignment by the valign attribute. When these attributes are applied to
an individual td or th, it is the alignment of that cell alone which is affected.
When applied to tr, thead, tbody or tfoot, it is the alignment of all
cells contained within that element which is affected. When used on col or colgroup,
it is the alignment of all cells within the relevant column(s) which should be affected - however,
as mentioned above, some popular browsers (such as Firefox) do not support either of these attributes
on col or colgroup.
The visual presentation of the table as a whole may be controlled by the
width, border, rules, frame, cellspacing and
cellpadding attributes, as well as by CSS.
You can read more about tables at the W3C's *HTML4.01 table markup page.
Note: In HTML 4, if a table
contains no tbody element (such as in the first example above),
one is automatically generated for it in the DOM. It is therefore recommended, if compatibility
is desired between XML-based and HTML-based user agents, to include explicit tbody
elements in all tables - without them, certain scripts or style
sheet rules may cease to function correctly. Ref: the W3C Note (work in progress)
*XHTML Media Types - Compatibility Guidelines - Item A.19
There are no #REQUIRED attributes on the table element.
table element are listed below:border [ type Pixels ]cellpadding [ type Length ]cellspacing [ type Length ]frame [ type Enumeration (void | above | below | hsides | lhs | rhs | vsides | box | border) ]rules [ type Enumeration (none | groups | rows | cols | all) ]summary [ type Text ]width [ type Length ]table element are listed below:class [ type NMTOKENS ]id [ type ID ]style [ type CDATA ], from the Style Attribute Module (deprecated)title [ type Text ]xmlns [ type URI - #FIXED 'http://www.w3.org/1999/xhtml' ]dir [ type Enumeration (ltr | rtl) ], from the Bi-directional Text Moduletable element, this also sets the ordering of the table's columns.xml:lang [ type LanguageCode ]All attributes in the
Events Attribute Collection
are supported:onclick, ondblclick, onmousedown, onmouseup, onmouseover, onmousemove, onmouseout, onkeypress, onkeydown, onkeyup
table element is:
caption?, ( col* | colgroup* ), ( ( thead?, tfoot?, tbody+ ) | tr+ )
NOTE: This element cannot be empty
See Content Model & Nesting for information about Content Model syntax and Nesting Groups.
tabletablebodyblockquote, divdd, litd, thbutton, fieldset, formmapnoscriptdel, insobject