Denotes a table data cell. | HTML 3.2, 4, 4.01, 5 |
HTML Syntax
<td
colspan = num_columns
headers = header_cells
rowspan = num_rows
>
A <td
> element’s end tag can be omitted if the <td
> element is immediately followed by a <td
> or <th
> element, or if there is no more content in the parent element.
The <td
> element specifies a table cell which contain the table data.
<td
> elements can only be used inside table rows, or within a <tr> element.
Each <td
> cell normally occupies only one column in one row, though authors can specify how many columns and rows the cell spans by use of the colspan and rowspan attributes.
cellspacing, or the distance between cells, and cellpadding, or the distance between the boundaries of each cell and its contents, are set at the <table> element level. As such, all cells in a table have the same padding and spacing.
A cell can be empty, that is, the <td
> element has no content. However, authors must still provide the <td
> element for any cell that is empty, so as not to collapse the table structure.
An empty cell can be made to behave like other cells in the table, having the same background color, border or other style attribute, by having a non-breaking space for its content, as in <td> </td>. If not, the space occupied by the cell will be completely empty: no content, no background color, and no border.
Although the W3C specifies that <td
> end tags are optional and may be omitted, it is safer to always close table data cells, as not all browsers support this recommendation, and may produce unexpected results.
The <td>
element supports the following attributes, in addition to global attributes common to all HTML elements.
colspan | number | Specifies the number of columns a cell should span |
headers | header_id | Specifies one or more header cells a cell is related to |
rowspan | number | Sets the number of rows a cell should span |
The following example shows how the <td
> element may be used.
<table table align="center" border>
<thead>
<tr>
<th>Heading 1</th>
<th>Heading 2</th>
</tr>
<tbody>
<tr>
<td>Row 1, Column 1 text.</td>
<td>Row 1, Column 2 text.</td>
</tr>
<tr>
<td>Row 2, Column 1 text.</td>
<td>Row 2, Column 2 text.</td>
</tr>
</table>
which would render on a Web page as follows:
Heading 1 | Heading 2 |
Row 1, Column 1 text. | Row 1, Column 2 text. |
Row 2, Column 1 text. | Row 2, Column 2 text. |
TABLE TBODY TFOOT THEAD TH TR Using Tables