Skip to main content

HTML Table Colgroup

The <colgroup> element styles specific columns of a table. It is a container for column specifications, and each group is set with a <col> element. On a <col>, the span attribute says how many columns to style and the style attribute gives the style.

Note: Only a very limited set of CSS properties work on colgroups. The <colgroup> must be a child of <table> and come before any rows or cells, but after a <caption> if there is one.

Example

The first two columns of a calendar styled with a colgroup.

Prompt
A calendar table (MON to SUN headers, four weeks of dates) where the first two columns are styled with a light blue background using a colgroup containing one col with span=2.
Output: complete HTML page

Legal CSS Properties

Only a few CSS properties are allowed on a colgroup: the width property, the visibility property, the background properties, and the border properties. All other CSS properties have no effect on the table.


Multiple Col Elements

To style several columns differently, use more than one <col> element inside the <colgroup>.

Example

Two column groups with different background colors.

Prompt
A calendar table where the first two columns are light blue and the next three are pink, using a colgroup with two col elements (span=2 and span=3) with different background colors.
Output: complete HTML page

Empty Colgroups

To style columns in the middle of a table, add an empty <col> element (with no style) for the columns before them.

Example

An empty col used to skip the first columns before styling later ones.

Prompt
A calendar table where columns in the middle are styled by first adding an empty col with span=3 (no style), then a col with span=2 and a pink background.
Output: complete HTML page

Hide Columns

You can hide columns with the visibility: collapse property on a <col>.

Example

The last three columns hidden with visibility: collapse.

Prompt
A calendar table where the last three columns are hidden using a col with span=3 and visibility: collapse, after a col with span=2.
Output: complete HTML page