Add a caption to a table with a <caption> element as the first child of <table>, and group columns with <colgroup> and <col> so you can style or size several columns at once. The html table caption colgroup features describe the table and its columns without changing the cell markup.
A caption gives the table a visible title that is also the accessible name for the table. Column groups give you one place to set widths, backgrounds or borders for a whole column instead of repeating a class on every cell.
The <caption> element must be the first child inside <table>. It holds the title or a short description of the table.
By default browsers centre the caption above the table. Screen readers announce it when the user reaches the table, so users can decide whether the table is relevant before reading the cells.
Use <caption> when the text names this particular table, because it is tied to the table in the markup. Use a heading such as <h2> when the text starts a section that may contain the table plus other content. A caption can also sit inside a <figure> if you prefer <figcaption>, but a table that stands alone is best served by <caption>.
The old align attribute is obsolete. Use the caption-side property instead.
The value bottom puts the caption under the table. The default is top.
Columns have no wrapper in the markup, because the table is written row by row. The <colgroup> element fills that gap. It goes after the caption and before any <thead>, <tbody> or <tfoot>, and contains one <col> per column.
<col> is a void element, so it has no closing tag. Each <col> stands for one column, from left to right.
Use span on <col> or <colgroup> when a group of neighbouring columns share the same settings.
Here the first column is plain, and the next three share the class num. A <colgroup> with a span attribute and no <col> children covers that many columns too, but you cannot mix the two forms in the same group.
Only a few properties work on <col> and <colgroup>, because the cells inherit from their rows, not their columns.
| Property | Works on columns |
|---|---|
width | Yes, sets the column width |
background | Yes, paints behind the column |
border | Yes, but only with border-collapse: collapse |
visibility: collapse | Yes, hides the column and reclaims its space |
color, font-weight, text-align | No, these do not cascade from <col> to cells |
A reliable example:
To right-align numbers you still need a rule on the cells, for example td:nth-child(n+2) { text-align: right; }. Column groups cannot do that for you.
summary on <table>; that attribute is obsolete. If the table needs a long explanation, put it in a paragraph before the table and link it with aria-describedby.<col> must still meet contrast requirements and must not be the only way to show meaning.<colgroup> has no effect on how header cells are read; use scope on <th> for that.It must be the first child of the <table> element, before <colgroup>, <thead> and everything else.
Yes, but only some properties such as width, background, border and visibility apply. Text properties do not.
No. It is obsolete in HTML. Use a <caption> and, when needed, a nearby paragraph linked with aria-describedby.
<colgroup> is a container for column definitions, and <col> describes a single column or, with span, several columns.
A <caption> names the table for everyone, including screen reader users, and must be the first child. <colgroup> and <col> follow it and let you set widths and backgrounds for whole columns, using span for several at once. Remember that only a handful of CSS properties apply to columns, and use scope on header cells for accessibility.