Go to main content
Bypass navigation bar HTML Reference HTML Tags HTML Tutorials
Bypass language selection
Bypass location bar

Location: Home > HTML Tutorials > Tables in HTML > Page 2

Bypass main content

Tables in HTML (page 2)

Pages:<12345>
Bypass main content

Header cells

Two types of cells can be defined in an HTML table. One of them is the simple cell (HTML td element), already defined in the "simple table" example, and the other one is a special type of cell (HTML th element) that contains header information for a set of specified cells.

Browsers may render the content of header cells in a special way (usually as centered bold text), attributes that can be visually achieved by using a normal cell (HTML td element) with presentational attributes (centered bold text). But header cells provide more 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).

The "scope" attribute

The cells affected by a header cell can be defined in different ways, but the most common and simple is to use the scope attribute. Set in both, the HTML td tag and the HTML th tag, this attribute can take four values: "row" and "col" affecting the containing row or column respectively, and "rowgroup" and "colgroup" affecting the containing group of rows or columns (described further in this tutorial).

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

Code
<table>
<tr>
<th scope="col">Column 1</th>
<th scope="col">Column 2</th>
<th scope="col">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>
View
Column 1 Column 2 Column 3
Cell 1 Cell 2 Cell 3
Cell 4 Cell 5 Cell 6

As we can deduct from the previous paragraph, in this example, the column 1 gives header information for the cells 1 and 4, the column 2 for the cells 2 and 5 and the column 3 for the cells 3 and 6.

Note that, as this attribute can also be defined in normal cells (HTML td tag), normal cells can act as header cells too. This is the case when a cell provides both, header information and data. In any other case header cells should be used (HTML th tag).

The "headers" attribute

Another way to associate header cells to normal cells is using the attribute "headers". This attribute is defined in a normal cell (HTML td tag) and refers to a set of cells (using their "id" attributes, separated by spaces) that provide header information for it.

There is a low (and very rare) number of cases where this attribute can't be replaced with the "scope" attribute. The next example shows an overlaid table, where there's an oblique line of cells that goes from the top left corner to the bottom right corner and serves as limit to define which cells receive the top and right headers and which the left and bottom.

Code
<table>
<tr>
<td>&nbsp;</td>
<th id="h1">H1</th>
<th id="h2">H2</th>
<th id="h3">H3</th>
<td>&nbsp;</td>
</tr>
<tr>
<th id="h4">H4</th>
<td headers="h1 h4 h7 h10">C1</td>
<td headers="h2 h7">T1</td>
<td headers="h3 h7">T2</td>
<th id="h7">H7</th>
</tr>
<tr>
<th id="h5">H5</th>
<td headers="h1 h5">B1</td>
<td headers="h2 h5 h8 h11">C2</td>
<td headers="h3 h8">T3</td>
<th id="h8">H8</th>
</tr>
<tr>
<th id="h6">H6</th>
<td headers="h1 h6">B2</td>
<td headers="h2 h6">B3</td>
<td headers="h3 h6 h9 h12">C3</td>
<th id="h9">H9</th>
</tr>
<tr>
<td>&nbsp;</td>
<th id="h10">H10</th>
<th id="h11">H11</th>
<th id="h12">H12</th>
<td>&nbsp;</td>
</tr>
</table>
View
  H1 H2 H3  
H4 C1 T1 T2 H7
H5 B1 C2 T3 H8
H6 B2 B3 C3 H9
  H10 H11 H12  

The information in this table isn't fully visual, but semantically it provides a wel defined set of headers for each cell. For example, the cell "C1" (in the oblique axis) with four headings (H1, H4, H7 and H10), and the cell T1 (on top of the axis) with two (H2 and H7).

This type of table is one of the few uses where "scope" (the simple way) wouldn't work, and is mostly used for showing scientific data.

The "axis" attribute

This attribute can be defined for header cells as well as for normal cells, and group headers and data by categories, in which the cells can be clasified. Although its use is a little complex, it gives new and expanded ways to provide header information, specially in the case of spoken browsers for blind people.

The following example illustrates the use of "axis" as a way to make the data more readable and understandable.

Code
<table>
<tr>
<th scope="col">Country</th>
<th scope="col" axis="income">Automobiles</th>
<th scope="col" axis="income">Autoparts</th>
<th scope="col" axis="outcome">Supplies</th>
<th scope="col">Balance</th>
</tr>
<tr>
<td scope="row" axis="america">United States</td>
<td>$5.432.551</td>
<td>$3.275.268</td>
<td>-$2.235,528</td>
<td>$6,472,291</td>
</tr>
<tr>
<td scope="row" axis="europe">United Kingdom</td>
<td>$3,763,221</td>
<td>$1,342,231</td>
<td>-$982,268</td>
<td>$4,123,184</td>
</tr>
<tr>
<td scope="row" axis="europe">France</td>
<td>$2.458.197</td>
<td>$2.456.841</td>
<td>-$1.315,548</td>
<td>$3,599,490</td>
</tr>
<tr>
<td scope="row" axis="america">Canada</td>
<td>$1.846.548</td>
<td>$448.556</td>
<td>-$351,559</td>
<td>$1,943,545</td>
</tr>
</table>
View
Country Automobiles Autoparts Supplies Balance
United States $5.432.551 $3.275.268 -$2.235,528 $6,472,291
United Kingdom $3,763,221 $1,342,231 -$982,268 $4,123,184
France $2.458.197 $2.456.841 -$1.315,548 $3,599,490
Canada $1.846.548 $448.556 -$351,559 $1,943,545

This example sets two type of axes or categories: a region category (Europe and America) and an income/outcome category. In a spoken web browser this non-visual information makes a lot of sense and helps undestanding the difficult table sctructure a lot better.

Pages:<12345>

Diseño y desarrollo: Latitud29.com

Links and logos|Contact|Beyond HTML|Tools and resources|Sitemap|Webmaster