HTML <colgroup> Tag

The HTML <colgroup> tag represents a group of one or more columns within a table in an HTML document.

It can be used to apply styles across one or more columns. This can be handy because it eliminates the need to apply the styles at the individual <td> level.

The <colgroup> tag must be used as a child of a <table> element, after any <caption> elements and before any <thead>, <tbody>, <tfoot>, and <tr> elements.

Syntax

The <colgroup> tag is written as <colgroup></colgroup>. It can have either a span attribute, which specifies how many columns to span, or it can contain one or more <col> tags, which can be used to represent different columns within the <colgroup> element.

Like this:

Or this:

Examples

Basic tag usage

In this example we use the <colgroup> tag to span the first two columns of a three column table (and we apply a background color to them).

Multiple <colgroup> Elements

You can use multiple <colgroup> tags to represent various column groups across a table. Here, we use two <colgroup> elements; the first represents the first two columns, while the second represents the third column.

The <col> Tag

If you need to apply different properties to a column within a colgroup, you can use the <col> tag within the <colgroup> tag.

Like this:

Attributes

Attributes can be added to an HTML element to provide more information about how the element should appear or behave.

There are 3 kinds of attributes that you can add to your HTML tags: Element-specific, global, and event handler content attributes.

The <colgroup> element accepts the following attributes.

Element-Specific Attributes

This table shows the attributes that are specific to the <colgroup> tag/element.

AttributeDescription
spanSpecifies how many columns to span. This attribute should not be used if there are any <col> elements within this <colspan> tag. This attribute's value must be a valid non-negative integer greater than zero.

Global Attributes

The following attributes are standard across all HTML5 elements. Therefore, you can use these attributes with the <colgroup> tag , as well as with all other HTML tags.

For a full explanation of these attributes, see HTML 5 global attributes.

Event Handler Content Attributes

Event handler content attributes enable you to invoke a script from within your HTML. The script is invoked when a certain "event" occurs. Each event handler content attribute deals with a different event.

Below are the standard HTML5 event handler content attributes.

Again, you can use any of these with the <colgroup> element, as well as any other HTML5 element.

For a full explanation of these attributes, see HTML 5 event handler content attributes.

Differences Between HTML 4 & HTML 5

The width, align, char, charoff, and valign attributes (which were supported in HTML 4) have been made obsolete in HTML5.

To see more detail on the two versions see HTML5 <colgroup> Tag and HTML4 <colgroup> Tag. Also check out the links to the official specifications below.

CSS Limitation

There are only four CSS properties that can be applied to column and column-group elements. They are as follows:

border
The various border properties apply to columns only if border-collapse is set to collapse on the table element. In that case, borders set on columns and column groups are input to the conflict resolution algorithm that selects the border styles at every cell edge.
background
The background properties set the background for cells in the column, but only if both the cell and row have transparent backgrounds.
width
Provides the minimum width for the column.
visibility
If the visibility of a column is set to collapse, none of the cells in the column are rendered, and cells that span into other columns are clipped. In addition, the width of the table is diminished by the width the column would have taken up.

Taken from the CSS 2.1 specification

And in case you're interested, here's the reasoning behind why only four CSS properties can be applied to table columns.

Template

Here's a template for the <colgroup> tag with all available attributes for the tag (based on HTML5). These are grouped into attribute types, each type separated by a space. In many cases, you will probably only need one or two (if any) attributes. Simply remove the attributes you don't need.

For more information on attributes for this tag, see HTML5 <colgroup> Tag and HTML4 <colgroup> Tag.

Tag Details

For more details about the <colgroup> tag, see HTML5 <colgroup> Tag and HTML4 <colgroup> Tag.

Specifications

Here are the official specifications for the <colgroup> element.

What's the Difference?

W3C creates "snapshot" specifications that don't change once defined. So the HTML5 specification won't change once it becomes an official recommendation. WHATWG on the other hand, develops a "living standard" that is updated on a regular basis. In general, you will probably find that the HTML living standard will be more closely aligned to the current W3C draft than to the HTML5 specification.