HTML Table Colgroup
In this page:
Grouping Columns with colgroup
<colgroup> is a structural wrapper placed immediately after the opening <table> tag, before any rows, that contains one <col> element for every column you want to individually reference or style, without needing to touch a single row's markup.
Note: Place colgroup as the very first child of table, before thead or tbody, or browsers will ignore it.
Warning: colgroup appearing after any row content in the markup is invalid and will simply be ignored by the browser.
Example: Grouping Columns with colgroup
<table>
<colgroup>
<col>
<col>
</colgroup>
<tr>
<td>A</td>
<td>B</td>
</tr>
</table>
Sizing a Column with the col Element
Setting a width on an individual <col> element sizes that entire column in one place, instead of setting the same width on every single cell down that column across every row -- a much more maintainable approach for wide tables.
Note: Use col width settings for consistent column sizing that stays correct even as rows are added or removed later.
Warning: Only a limited set of CSS properties, primarily width, is reliably supported directly on col across all browsers.
Example: Sizing a Column with the col Element
<table>
<colgroup>
<col style="width: 70%;">
<col style="width: 30%;">
</colgroup>
<tr>
<td>Name</td>
<td>Age</td>
</tr>
</table>
Spanning Multiple Columns with span
The span attribute on a single <col> element lets it represent several consecutive columns at once, instead of writing out one <col> per column -- span="3" on one col element is equivalent to writing three plain col elements in a row.
Note: Use span to shorten colgroup markup when several consecutive columns share the exact same width or styling.
Warning: A col span value that does not match the table's actual column count will misalign column-specific styling.
Example: Spanning Multiple Columns with span
<table>
<colgroup>
<col span="3" style="width: 33%;">
</colgroup>
<tr>
<td>A</td>
<td>B</td>
<td>C</td>
</tr>
</table>
Highlighting a Column Visually
Beyond width, a col element can be targeted with CSS to apply a background color to an entire column at once, which is useful for visually highlighting a single important column, like a "Total" column, without adding a class to every individual cell in it.
Note: Highlight one key column (like a total or status column) via colgroup instead of manually classing every cell in that column.
Warning: Background-color support through col is more limited and inconsistent across browsers than width support, so always visually verify it renders as expected.
Example: Highlighting a Column Visually
<table>
<colgroup>
<col>
<col style="background-color: yellow;">
</colgroup>
<tr>
<td>Item</td>
<td>Total</td>
</tr>
</table>
When to Use colgroup vs Per-Cell Styling
colgroup is best reserved for column-wide sizing and simple background highlighting; for more complex per-column styling (like text-align, borders, or font styling), the more broadly-supported CSS :nth-child(n) selector applied to td and th cells is generally the more reliable choice.
Note: Reach for colgroup specifically for width and simple highlighting; use :nth-child selectors on cells for anything more visually complex.
Warning: Relying on colgroup for properties beyond width and background can produce inconsistent rendering across different browsers.
Example: When to Use colgroup vs Per-Cell Styling
<table>
<colgroup>
<col style="width: 50%;">
</colgroup>
<tr>
<td style="text-align: right;">Complex per-cell style</td>
<td>Simple width</td>
</tr>
</table>
- Assuming col elements can hold visible content, when they are purely structural and cannot contain text or other elements.
- Forgetting that colgroup must appear immediately after the opening table tag, before any thead, tbody, or tr elements.
- Trying to set every CSS property on col, when browsers only apply a limited set of properties (mainly width and visibility/background in some browsers) through col.
- <colgroup> groups one or more <col> elements, each representing an entire table column.
- The span attribute on <col> lets one col element apply to multiple consecutive columns at once.
- colgroup must be placed directly after the opening table tag, before any row content.
<colgroup> and <col> are supported in every browser and have been part of HTML since HTML4; only a limited set of CSS properties like width are reliably supported through them.
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- HTML Favicon
- HTML Page Title
- HTML Tables
- HTML Lists
- HTML Block & Inline
- HTML Comments
- HTML Colors
- HTML CSS
- HTML Links
- HTML Images
- HTML RGB Colors
- HTML HEX Colors
- HTML HSL Colors
- HTML Link Colors
- HTML Link Bookmarks
- HTML Background Images
- HTML Table Borders
- HTML Table Sizes
- HTML Table Headers
- HTML Table Padding & Spacing
- HTML Colspan & Rowspan
- HTML Table Styling
- HTML Table Colgroup
- HTML Unordered Lists
- HTML Ordered Lists
- HTML Other Lists / Description Lists