Versión en español




Tutorial: Tables in HTML


This tutorial explains the correct and complete way to define tables in HTML documents. This will help beginners to learn how to write tables manually in an HTML document, while defines and describes all the tools that are available for advanced (many of whom still don't know all the properties of a table).

As you probably read around the site, our intention is to provide you with "good programing methods" that will give you some extra benefits. This is why we'll choose a way to declare these tables that is compatible with the XHTML code standard.

Simple tables

An HTML table can be considered in a simple way as a group of rows where each of them contains a group of cells. Tables, as all structures in HTML documents, are defined using tags. So a simple table can be inserted in an HTML document by using three tags: the HTML table tag (main container structure), the HTML tr tag (row container) and the HTML td tag (single cell). Lets see an example:

Code View
<table border="1" summary="Simple table example.">
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
<td>Cell 3</td>
</tr>
<tr>
<td>Cell 4</td>
<td>Cell 5</td>
<td>Cell 6</td>
</tr>
</table>
Cell 1 Cell 2 Cell 3
Cell 4 Cell 5 Cell 6

Types of cells

Two types of cells can be defined in an HTML table. One of them is the simple cell (HTML td tag) already defined in the "simple table" example, and the other one is a special type of cell (HTML th tag) that contains header information for all the cells in the column (that belong to it's horizontal group). Browsers may render the content of header cells in a special way (usually as centered bold text). This type of cell is expected to be found in a header section of a table.

In the example below the same "simple table" is defined but this time with header cells:

Code View
<table border="1" summary="Simple table example with header cells.">
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
</tr>
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
<td>Cell 3</td>
</tr>
<tr>
<td>Cell 4</td>
<td>Cell 5</td>
<td>Cell 6</td>
</tr>
</table>
Column 1 Column 2 Column 3
Cell 1 Cell 2 Cell 3
Cell 4 Cell 5 Cell 6

This cell's characteristics may be visually achieved by using a normal cell with presentational attributes (centered bold text), but the header cells provides more hidden information that may be helpful for non-visual browsers (spoken browsers may speak to the user the cells content while immediately associating it to the header cell) and spiders (like search engines spiders which may help in the site promotion).

Another way to associate header cells to normal cells is using the attributes: "headers" to define a list of cells that provides header information for the actual cell (see the HTML td tag) and "scope" to define a list of cells for which the actual cell provides header information (see the HTML th tag).

Cell spanning

For some tables you may need to unify two or more cells into a single one that will take the place of those affected. These unifications may be done with the "rowspan" (for vertical spanning) and the "colspan" (for horizontal spanning) attributes, both available for cells (HTML td tag and HTML th tag).

Code View
<table border="1" summary="Simple table example with cell spanning in a row.">
<tr>
<td>Field 1</td>
<td>Field 2</td>
<td>Field 3</td>
</tr>
<tr>
<td colspan="2">Fields 4 and 5</td>
<td>Field 6</td>
</tr>
<tr>
<td>Field 7</td>
<td>Field 8</td>
<td>Field 9</td>
</tr>
</table>
Field 1 Field 2 Field 3
Fields 4 and 5 Field 6
Field 7 Field 8 Field 9

See in the example above how a cell definition that spans 2 cells, is the unified equivalent of two single cell definitions. This also works for vertical spanning with a small difference due to the nature of HTML tables. When spanning cells vertically, the cell's definitions in the rows affected by the span must be skipped. See the example, where the field 1, 4 and 7 are spanned:

Code View
<table border="1" summary="Simple table example with cell spanning in a column.">
<tr>
<td rowspan="3">Unified field</td>
<td>Field 2</td>
<td>Field 3</td>
</tr>
<tr>
<td>Field 5</td>
<td>Field 6</td>
</tr>
<tr>
<td>Field 8</td>
<td>Field 9</td>
</tr>
</table>
Unified field Field 2 Field 3
Field 5 Field 6
Field 8 Field 9

You must be careful when spanning rows and columns to don't overlap the spanned cells. This can result in undesired results.



Tutorial pages
  1  [ 2 ][ 3 ][ Next > ]

Next tutorial: Forms in HTML >>


Bypass footer options  |   Send to a friend Send to a friend  |  Post to del.icio.us Post to del.icio.us

Digg this page Digg this!  |  File on Furl File on Furl  |  Add to Yahoo! MyWeb Add to Yahoo! MyWeb

Bypass W3C declarations | 

Valid XHTML 1.0 Strict  |  Valid CSS Why should you trust us? Click the images on the left to see how seriously we write our own pages, then make your choice.

Level Triple-A conformance icon, W3C-WAI Web Content Accessibility Guidelines 1.0. This website gives its best effort to achieve the Level Triple-A Conformance, W3C-WAI Web Content Accessibility Guidelines 1.0. If you find any detail or error that we didn't see, don't hesitate and let us know.

The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect. -- Tim Berners-Lee, W3C Director and inventor of the World Wide Web.

 Link to us  |  Contact us  |  Beyond HTML  |  Tools and resources  |  Sitemap  |  Webmaster