HTML <colgroup> Tag
<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
<colgroup> tag is written as
</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
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).
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.
If you need to apply different properties to a column within a colgroup, you can use the
<col> tag within the
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.
<colgroup> element accepts the following attributes.
This table shows the attributes that are specific to the
|span||Specifies how many columns to span. This attribute should not be used if there are any
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
valign attributes (which were supported in HTML 4) have been made obsolete in HTML5.
There are only four CSS properties that can be applied to column and column-group elements. They are as follows:
- The various border properties apply to columns only if
border-collapseis set to
collapseon 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.
- The background properties set the background for cells in the column, but only if both the cell and row have transparent backgrounds.
- Provides the minimum width for the column.
- If the
visibilityof 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.
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.
Here are the official specifications for the
- HTML5 Specification (W3C)
- HTML Living Standard (WHATWG)
- Current W3C Draft (the next version that is currently being worked on)
- HTML 4 (W3C)
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.