Skip to main content

HTML Tags

HTML <colgroup> Tag

Written by Updated HTML <colgroup> Tag exercises

The colgroup tag lets you style whole columns of a table at once, instead of every cell in them. Inside it, each col tag stands for one or more columns, and span says how many.

It goes right after the table's <caption> (if there is one) and before any rows. The individual columns are described with the col tag.

Syntax

html

<table>
  <colgroup>
    <col span="number" style="property:value;">
  </colgroup>
  ...
</table>
AttributeUsed onWhat it does
spancolgroup or colHow many columns this covers. Default 1.

Global attributes such as class, id and style also work.

Example

html

<!DOCTYPE html>
<html>
<body>

<h2>The &lt;colgroup&gt; Tag Example</h2>

<table border="1">
  <colgroup>
    <col span="1" style="background-color: #ffdddd;">
    <col span="2" style="background-color: #ddffdd;">
  </colgroup>
  <tr>
    <th>Item</th>
    <th>Price</th>
    <th>Availability</th>
  </tr>
  <tr>
    <td>Laptop</td>
    <td>$1200</td>
    <td>In Stock</td>
  </tr>
  <tr>
    <td>Tablet</td>
    <td>$500</td>
    <td>Out of Stock</td>
  </tr>
</table>

</body>
</html>

The first column, "Item", has a light red background, and the next two, "Price" and "Availability", a light green one, because the second <col> spans two columns.

Only a few properties work

Columns are not real containers of the cells, so only a handful of CSS properties have any effect on them:

  • background
  • border (with border-collapse: collapse on the table)
  • width
  • visibility: collapse

Text color, font, padding and alignment do not work on columns. For those, select the cells instead, for example td:nth-child(2) { text-align: right; }.

Browser Support

Chrome
Edge
Firefox
Safari
Opera
IE9+
YesYesYesYesYesYes

Good to know

  • The total of all spans should match the number of columns in the table.
  • A cell's own background is drawn on top of the column background, so if the cells have a background color, the column color is hidden.

HTML <colgroup> Tag exercises

Add a column group

Make a table with a column group that stands for two columns, without any col, and a row with A and B.

Result

Check your answer to see it here.