HTML Reference ยท Tables
The <colgroup> element
Reference for the HTML <colgroup> element: grouping table columns, using span or col children, where it goes inside a table, and technical details.
The <colgroup> element defines a group of columns in a <table>. It contains no cells. Instead, it either holds <col> elements that describe each column, or uses its own span attribute to say how many columns the group covers.
Column groups go right after the <caption> (if any) and before the table's rows and row groups.
<table> <caption>...</caption> <colgroup span="2"></colgroup> <colgroup> <col> <col class="highlight"> </colgroup> ... </table>
Try it
The first group covers the two descriptive columns with span="2". The second contains three <col> elements, and its border and background mark the numeric part of the table. Group borders show only with border-collapse: collapse.
Attributes
| Attribute | Values | Description |
|---|---|---|
span |
A positive integer (1โ1000) | The number of columns in the group. Only allowed when the element has no <col> children. Defaults to 1. |
It also accepts the global attributes. The obsolete align, valign, width, char and charoff attributes should be replaced with CSS.
Usage notes
spanor<col>, not both. If a<colgroup>has<col>children, anyspanattribute on it is ignored and the markup is invalid. Putspanon the individual<col>elements instead.- Groups give structure to headers. A
<th>withscope="colgroup"applies to every column in the group it starts. This helps screen readers with tables that have two levels of column headings. - Limited styling. Like columns, groups accept only
background,border(collapsed borders),widthandvisibility: collapse. Other properties do not reach the cells. - Several groups are fine. A table can have any number of column groups, one after another.
- Implied groups. If you write
<col>directly inside<table>, the parser creates a<colgroup>around it. Writing it yourself makes the structure clearer.
Technical summary
| Content categories | None |
| Permitted content | If the span attribute is present: nothing. Otherwise: zero or more <col> and <template> elements |
| Tag omission | The start tag can be omitted if the first thing inside is a <col> element and the group is not directly after another <colgroup> whose end tag was omitted. The end tag can be omitted if it is not immediately followed by ASCII whitespace or a comment |
| Permitted parents | A <table>, after any <caption> and before any <thead>, <tbody>, <tfoot> and <tr> elements |
| Implicit ARIA role | No corresponding role |
| DOM interface | HTMLTableColElement |
| Default display | table-column-group |
Browser support
Supported in every browser.
Related
<col>โ a single column (orspancolumns)<table>โ the table itself<th>โ thescope="colgroup"value- Lesson: Column Groups
- Lesson: Styling Tables with CSS