Using col and colgroup to Apply Attributes to a Table Column
By Stephen Bucaro
The col tag allows you to define attributes for one or more columns in a table. The
colgroup tag allows you apply attributes to a group of columns.
Line1 Line2 Line3 | Line1 |
Line1 Line2 Line3 | Line1 Line2 |
Looking at the table above, you might decide that it would be more understandable if the data
in the second column was aligned to the top of their table cells. You could accomplish this by
adding the valign="top" attribute to each cell in the column as shown below.
<table width="100" border="1" cellpadding="4">
<tr><td>Line1 Line2 Line3</td><td valign="top">Line1</td></tr>
<tr><td>Line1 Line2 Line3</td><td valign="top">Line1 Line2</td></tr>
</table>
Line1 Line2 Line3 | Line1 |
Line1 Line2 Line3 | Line1 Line2 |
However, with a large table this might be tedious, and it adds unnecessary code. Instead you
can use the col tag to define attributes for every cell in a column, as shown below.
<table width="100" border="1" cellpadding="4">
<col width="60">
<col valign="top">
<tr><td>Line1 Line2 Line3</td><td>Line1</td></tr>
<tr><td>Line1 Line2 Line3</td><td>Line1 Line2</td></tr>
</table>
Line1 Line2 Line3 | Line1 |
Line1 Line2 Line3 | Line1 Line2 |
Note: This works in Internet Explorer, however the Firefox browser does not recognize html
attributes in the col tag. It does better with style attributes.
<table width="100" border="1" cellpadding="4">
<colgroup valign="top" >
<col />
<col />
<col />
</colgroup>
<tr><td>Line1 Line2 Line3</td><td>Line1</td><td>Line1 Line2</td></tr>
<tr><td>Line1 Line2 Line3</td><td>Line1 Line2</td><td>Line1</td></tr>
</table>
Line1 Line2 Line3 | Line1 | Line1 Line2 |
Line1 Line2 Line3 | Line1 Line2 | Line1 |
I have added another column to the table above. Using the colgroup tag I have applied the
valign="top" attribute all three columns in one tag. For this to work in Internet Explorer,
you still need the col tags within the colgroup tag. And again, the Firefox browser
does not recognize html attributes in the colgroup tag.
|