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.
<table width="100" border="1" cellpadding="4"> <colgroup valign="top" > <col /> <col bgcolor="#ccccff" /> <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 |
In the table above, I've used the colgroup tag to apply the valign="top" attribute to all three columns, and I have applied the bgcolor="#ccccff" in the col tag for the center column to give it a light-blue background color.
Note that the col and colgroup tags work only marginally in Internet Explorer and, for html attributes, not at all in Firefox. CSS (Cascading Style Sheet) rules is the preferred method to define table columns.
More HTML Code:
• Line Breaks in HTML
• Divide a Table Into Head (thead), Body (tbody), and Footer (tfoot) Sections
• How to Use a Meta Redirect
• Easy Form Design
• Changing the Size of an Image on Your Webpage
• HTML Bulleted or Unordered List
• HTML Editors
• Webpage DOCTYPE Declarations Explained
• Easy Code to Add Yahoo Site Search to Your Website
• Easiest HTML Calculator Eample Code