The <col> HTML/xHTML tag

The column tag is used at the start of a table to define a column, allowing certain styles to be applied to every cell in the column.

The really useful thing about  tags is that they let you easily assign the same properties (especially widths) to multiple tables. If you have 2 tables on a page, which contain similar data but in differing amounts, if left to its own devices a web browser will automatically resize columns to make each table as short as possible. What you want is a neat way to make the first column in both tables line up etc. That’s where the <col /> or the multiple column tag <colgroup /> come in. (The colgroup is used to assign the same properties to more than one consecutive column.)

By simply assigning the same columns to each table, you can give each column 1 the same classname, which might fix the width as a set percentage of the table width.

When to use the <col> tag (semantic use)

Use <col /> tags only to assign class or style properties to whole columns in HTML tables.

It’s valid in all current versions of HTML and xHTML, and it’s a self-closing tag, so if you are using xHTML you need to remember the trailing forward-slash />

Example of <col> tag use

See how the column widths in these 2 tables are different, which doesn’t look tidy.

Wombles
Name Favourite pudding Golf handicap
Great Uncle Bulgaria Crème brulée 2
Madame Cholet Apple tart 5
Tomsk Chocolate muffin 14
Flumps
Name Favourite pudding Golf handicap
Pootle Rhubarb and custard, with cinnamon (Doesn’t play golf)
Mrs. Flump Anything with vanilla 4

The xHTML markup

<table style=”width:30em;”>

<caption>Wombles</caption>
<thead>

<tr>

<th>Name</th>
<th>Favourite pudding</th>
<th>Golf handicap</th>

</tr>

</thead>
<tbody>

<tr>

<td>Great Uncle Bulgaria</td>
<td>Cr&egrave;me brul&eacute;e</td>
<td>2</td>

</tr>
<tr>

<td>Madame Cholet</td>
<td>Apple tart</td>
<td>5</td>

</tr>
<tr>

<td>Tomsk</td>
<td>Chocolate muffin</td>
<td>14</td>

</tr>

</tbody>

</table>

<table style=”width:30em;”>

<caption>Flumps</caption>
<thead>

<tr>

<th>Name</th>
<th>Favourite pudding</th>
<th>Golf handicap</th>

</tr>

</thead>
<tbody>

<tr>

<td>Pootle</td>
<td>Rhubarb and custard, with cinnamon</td>
<td>(Doesn’t play golf)</td>

</tr>
<tr>

<td>Mrs. Flump</td>
<td>Anything with vanilla</td>
<td>4</td>

</tr>

</tbody>

</table>

The fix, using column tags

The easiest way to align the columns is to use <col /> tags.


Wombles
Name Favourite pudding Golf handicap
Great Uncle Bulgaria Crème brulée 2
Madame Cholet Apple tart 5
Tomsk Chocolate muffin 14
Flumps
Name Favourite pudding Golf handicap
Pootle Rhubarb and custard, with cinnamon (Doesn’t play golf)
Mrs. Flump Anything with vanilla 4

The xHTML & CSS markup

<style type=”text/css”>

.col1 {width:25%;}
.col2 {width:45%;}
.col3 {width:30%;}

</style>

<table style=”width:30em;”>

<caption>Wombles</caption>
<col class=”col1″ />
<col class=”col2″ />
<col class=”col3″ />
<thead>

<tr>

<th>Name</th>
<th>Favourite pudding</th>
<th>Golf handicap</th>

</tr>

</thead>
<tbody>

<tr>

<td>Great Uncle Bulgaria</td>
<td>Cr&egrave;me brul&eacute;e</td>
<td>2</td>

</tr>
<tr>

<td>Madame Cholet</td>
<td>Apple tart</td>
<td>5</td>

</tr>
<tr>

<td>Tomsk</td>
<td>Chocolate muffin</td>
<td>14</td>

</tr>

</tbody>

</table>

<table style=”width:30em;”>

<caption>Flumps</caption>
<col class=”col1″ />
<col class=”col2″ />
<col class=”col3″ />
<thead>

<tr>

<th>Name</th>
<th>Favourite pudding</th>
<th>Golf handicap</th>

</tr>

</thead>
<tbody>

<tr>

<td>Pootle</td>
<td>Rhubarb and custard, with cinnamon</td>
<td>(Doesn’t play golf)</td>

</tr>
<tr>

<td>Mrs. Flump</td>
<td>Anything with vanilla</td>
<td>4</td>

</tr>

</tbody>

</table>

Alternatives to the <col> tag

You may also consider using:

<colgroup> tag
Use a colgroup as a shorthand when there are more than 1 consecutive columns sharing the same property.

Make Better Web Pages!

How to make your web site sell - use my secrets

Find out How

Do you love our approach to crafting simple & effective web sites that just work for people?

We'd love to hear about your web strategy.

Contact one of our team today!

Leave a comment

Articles + tutorials in Articles

HTML <tfoot> table foot Tag Reference
The tfoot HTML tag means table foot, and defines the foot section of a table.
HTML <thead> table head Tag Reference
The thead HTML tag means table head, and is an optional tag which can enclose the top row of a table to define the cells as column headers.
HTML <applet> Applet Tag Reference
The applet HTML tag means Applet, and it defines an applet embedded in a webpage.
HTML <strike> Strikethrough Tag Reference
The s or strike HTML tags mean Strikeout, and they are used to display text with a solid line through it.
HTML <big> Big Tag Reference
The big HTML tag means Big, and it is used to do display text in a bigger font size than the default.
HTML <a> anchor Tag Reference
The <a> HTML tag creates an anchor in your HTML code, most commonly providing a link to another site, page, or point on a page.
HTML <small> Small Tag Reference
The small HTML tag means Small, and it is used to display text in a smaller font size than the default.
HTML <dir> Directory List Tag Reference
The dir HTML tag means Directory List, and it defines a directory list.
HTML <span> inline span Tag Reference
The span HTML tag means inline span, and is used to apply style to a section of text without breaking the flow of the document.
HTML <tbody> table body Tag Reference
The tbody HTML tag means table body, and contains the rows of data that make up a table.
HTML <kbd> Keyboard Input Tag Reference
The kbd HTML tag means Keyboard Input, and it defines sample code to be input by the user.
HTML <ul> Unordered List Tag Reference
The ul HTML tag creates an unordered list of items.
HTML <blink> Blink Tag Reference
The blink HTML tag means Blink, and it makes text blink on and off repeatedly.
HTML <p> Paragraph Tag Reference
The p HTML tag means paragraph, and it denotes a paragraph of text.
HTML <> Italic Tag Reference
The i HTML tag means Italic, and renders text in italics.
HTML <var> computer code variable Tag Reference
The var HTML tag means variable, and it denotes variable references within computer programs etc.
HTML <dl> Definition List Tag Reference
The dl HTML tag means "definition list" and is used to create a list of name/description pairs.
HTML <menu> Menu List Tag Reference
The menu HTML tag means Menu List, and it defines a list as a menu list.
HTML <basefont> Basefont Tag Reference
The basefont HTML tag means Basefont, and it defines a default font style.
HTML <div> division Tag Reference
The div HTML tag means division, and is basically a general-purpose box that takes up the full width of the space it's in.
HTML <td> table data cell Tag Reference
The td HTML tag means table data cell, and it's the building block of a table.
HTML <samp> Sample Output Tag Reference
The samp HTML tag means Sample Output, and it defines text as sample output from scripts or programs.
HTML <dt> Definition Term Tag Reference
The dt HTML tag stands for "definition term", used as part of HTML definition lists, together with definitions.
HTML <head> Head Tag Reference
The head tag contains the non-visible information about your HTML document.
HTML <tr> table row Tag Reference
The tr HTML tag means table row, and defines a row of data within a table
HTML <u> underline Tag Reference
The underline HTML tag means underline, and it underlines text.
HTML <caption> Table Caption Tag Reference
The caption HTML tag creates a caption for a table, which is like a heading for a table.
HTML <li> List Item Tag Reference
The li HTML tag means "list item". It indicates an elements in a list
HTML <dd> Definition Definition Tag Reference
The dd HTML tag stands for "definition definition"
HTML <col> column Tag Reference
The col HTML tag means column, and it is used to apply certain styles to a whole column in a table.
HTML <ol> Ordered List Tag Reference
The ol HTML tag means "Ordered List"
HTML <th> table header cell Tag Reference
The th HTML tag means table header cell, and defines a column or row header cell.
HTML <br> Line Break Tag Reference
The br HTML tag means line-break, and it inserts a break into a line of text.
HTML <center> Center Tag Reference
The center HTML tag means Center, and it centres its enclosed content.
HTML <table> table Tag Reference
The table HTML tag means table, and it divides content up into rows and columns.
HTML <b> Bold Tag Reference
The b HTML tag means Bold, and renders text as bold.
HTML <font> Font Tag Reference
The font HTML tag means Font, and it specifies the font style of any text it encloses.
HTML <address> Address Tag Reference
The address HTML tag means Address, and is used to define contact details.
HTML <isindex> Is Index Tag Reference
The isindex HTML tag means Is Index, and it displays a single-line textbox, which prompts the user to enter keywords, which are then used to perform a search.
HTML <colgroup> Column group Tag Reference
The colgroup HTML tag represents a column group, and it's used to apply certain styles (usually using CSS classes) to a sequential group of columns in an HTML table.
HTML <marquee> Marquee Tag Reference
The marquee HTML tag means Marquee, and it makes text scroll across the page from right to left.
A-Z List of HTML Tags
Complete HTML/xHTML Tag Reference (39/88 tags documented)
HTML Heading Tag Reference
The h1 - h6 HTML tags mean Heading 1 to Heading 6, and they denote section headings.
Complete HTML/xHTML Tag Reference
© Scratchmedia Limited, 2006-2010
Floor 3, 111 Buckingham Palace Road, London, SW1W 0WQ, UK
+44 (0)207 1600 989