<colgroup>
Definition
The HTML colgroup element allows you to group together columns in a table for formatting purposes. By using this element, you can apply styles or attributes to a group of columns instead of individual columns. This can make it easier to manage the formatting of your table and make it more consistent across all the columns.
The colgroup element must be used inside a table element and must contain one or more col elements, each representing a column in the group. You can use the span attribute to specify the number of columns that the colgroup should apply to.
You can also use the width attribute to specify the width of each column in the group, either as a percentage or in pixels. This can be useful for ensuring that all the columns in the group have the same width.
Examples
<table>
<colgroup>
<col span="3" class="highlighted" />
</colgroup>
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>Row 1, Cell 1</td>
<td>Row 1, Cell 2</td>
<td>Row 1, Cell 3</td>
</tr>
<tr>
<td>Row 2, Cell 1</td>
<td>Row 2, Cell 2</td>
<td>Row 2, Cell 3</td>
</tr>
</tbody>
</table>
In this example, the colgroup element is used to group the first three columns of the table. The span attribute is used to specify that the colgroup should apply to three columns, and the class attribute is used to apply the "highlighted" class to the columns in the colgroup, which can be used in a style sheet to highlight the text within those columns.
Attributes
| Attribute | Description | Deprecated |
|---|---|---|
span | Specifies the number of columns that the colgroup element should apply to. | No |
class | Specifies one or more class names for the colgroup element, which can be used to refer to the element in a style sheet. | No |
width | Specifies the width of each column in the colgroup. | No |
style | Specifies an inline style for the colgroup element. | No |
align | Specifies the horizontal alignment of the content within the columns of the colgroup. This attribute is deprecated in HTML5. | Yes |
char | Specifies the alignment character for the content within the columns of the colgroup. This attribute is deprecated in HTML5. | Yes |
charoff | Specifies the number of characters by which the content within the columns of the colgroup should be offset from the alignment character. This attribute is deprecated in HTML5. | Yes |
Best Practices
- Use the
colgroupelement to group columns in a table for formatting purposes. - Use the
spanattribute to specify the number of columns that thecolgroupshould apply to. - Use the
classattribute to apply styles to the columns in thecolgroupusing CSS. - Use the
widthattribute to specify the width of each column in thecolgroup. - Place the
colgroupelement inside thetableelement, before anythead,tbody, ortfootelements. - Use the
colelement within acolgroupto apply styles or attributes to individual columns. - Use the
styleattribute to apply inline styles to the columns within acolgroup, though these styles are better separate in a stylesheet.
Accessibility Considerations
Using the colgroup element can make it easier to apply styles to a table in a way that improves its accessibility. For example, using the class attribute to apply a CSS style that increases the font size or contrast of the text within the columns of a table can make the table easier to read for users with visual impairments.
Additionally, using the width attribute to specify the width of each column can help ensure that the table is properly rendered on a variety of devices and screen sizes, which can improve the accessibility of the table for users with mobility impairments or users who are accessing the table on a small screen.
Browser Compatibility
| Chrome | Firefox | Safari | Internet Explorer | Microsoft Edge | Opera |
|---|---|---|---|---|---|
| Yes | Yes | Yes | Yes | Yes | Yes |