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 4

Bypass main content

Tables in HTML (page 4)

Pages:<12345>
Bypass main content

Vertical grouping

The other way to group cells in a table is the vertical grouping. This not only useful to group columns thematically, but also allows to assign attributes to entire columns of a table. The grouping can be done using two tags: the HTML colgroup tag and the HTML col tag. These two tags may be used separately or altogether.

In the example below we'll insert a very simple table that will define column attributes using the HTML col tag:

Code
<table>
<col style="width: 200px" />
<col style="width: 100px" span="3" />
<tr>
<th scope="row">January</th>
<td>12.8</td>
<td>42.6</td>
<td>5.2</td>
</tr>
<tr>
<th scope="row">Feubrary</th>
<td>10.5</td>
<td>30.1</td>
<td>10.4</td>
</tr>
<tr>
<th scope="row">March</th>
<td>9.5</td>
<td>31.5</td>
<td>5.4</td>
</tr>
</table>
View
January 12.8 42.6 5.2
Feubrary 10.5 30.1 10.4
March 9.5 31.5 5.4

Note the tag closing used (" />") to make our code XHTML compliant. To learn more about the attributes used in this example, please refer to this description page about the HTML col tag.

In the previous example, common attributes have been definedfor all cells in the affected columns at once, but you cannot define semantically column groups without using the HTML colgroup tag:

Code
<table>
<colgroup style="width: 150px" />
<colgroup style="width: 50px" span="3" />
<colgroup style="width: 100px" />
<tr>
<th scope="row">January</th>
<td>12.8</td>
<td>42.6</td>
<td>5.2</td>
<td>Not complete</td>
</tr>
<tr>
<th scope="row">Feubrary</th>
<td>10.5</td>
<td>30.1</td>
<td>10.4</td>
<td>Not complete</td>
</tr>
<tr>
<th scope="row">March</th>
<td>9.5</td>
<td>31.5</td>
<td>5.4</td>
<td>Completed</td>
</tr>
</table>
View
January 12.8 42.6 5.2 Not complete
Feubrary 10.5 30.1 10.4 Not complete
March 9.5 31.5 5.4 Completed

Now in this example we have three real, semantically speaking, column groups with the attributes for all cells in the group properly set.

So what is the HTML col tag good for then? There are some cases when authors need to set groups of columns (semantically) but apply different attributes to the columns in a same group. To solve this problem authors may use both tags nested:

Code
<table summary="Example for vertical grouping in an HTML table with the HTML colgroup tag and the HTML col tag.">
<colgroup style="width: 150px" />
<colgroup>
<col style="width: 50px" span="2" />
<col style="width: 75px" />
</colgroup>
<colgroup style="width: 100px" />
<tr>
<th scope="row">January</th>
<td>12.8</td>
<td>42.6</td>
<td>5.2</td>
<td>Not complete</td>
</tr>
<tr>
<th scope="row">Feubrary</th>
<td>10.5</td>
<td>30.1</td>
<td>10.4</td>
<td>Not complete</td>
</tr>
<tr>
<th scope="row">March</th>
<td>9.5</td>
<td>31.5</td>
<td>5.4</td>
<td>Completed</td>
</tr>
</table>
View
January 12.8 42.6 5.2 Not complete
Feubrary 10.5 30.1 10.4 Not complete
March 9.5 31.5 5.4 Completed

This way we're still defining three column groups but giving the columns specific characteristics no matter if they belong to the same column group or not.

Pages:<12345>

Diseño y desarrollo: Latitud29.com

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