Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

HTML element: colgroup

The column-group element combines consecutive table columns into a range that can receive shared presentation.

Version: WHATWG HTML Living Standard; index snapshot date recorded separately

Syntax

<table><colgroup><col></colgroup><colgroup span="2"></colgroup>…</table>

colgroup is a direct child of table, placed after an optional caption and before thead, tbody, tfoot, or direct tr rows.

A group uses either its own span attribute or child col elements; when span is present, its content model is empty and columns must not be added as children.

A span value covers 1 through 1000 columns. Without it, coverage is the sum of child col elements; an empty colgroup then contributes the table-model default span of one.

Grouping does not create data headers, so th and scope remain necessary. The example uses querySelectorAll() to compare one explicit-column group with a two-column span group.

Examples

<!doctype html>
<meta charset="utf-8">
<table id="report">
  <caption>Measurements</caption>
  <colgroup id="identity"><col></colgroup>
  <colgroup id="metrics" span="2"></colgroup>
  <tbody><tr><td>Item</td><td>Min</td><td>Max</td></tr></tbody>
</table>
<output id="result"></output>
<script>
const report = document.querySelector("#report");
const groups = [...report.querySelectorAll(":scope > colgroup")];
const identity = document.querySelector("#identity");
const metrics = document.querySelector("#metrics");
document.querySelector("#result").textContent = `groups=${groups.length}; afterCaption=${report.caption.nextElementSibling.tagName}; explicitCols=${identity.querySelectorAll("col").length}; metricsSpan=${metrics.span}; metricsChildren=${metrics.children.length}`;
</script>

Result

groups=2; afterCaption=COLGROUP; explicitCols=1; metricsSpan=2; metricsChildren=0

The result verifies two groups after the caption, one explicit column, and an empty span group; it does not assess final column styling.

Sources