<colgroup>
A group of table columns that you can style in one go.
- Display
- table-column-group
- Closing tag
- Required
- Permitted content
<col>elements, or nothing whenspanis given
Example
#<style>
table { border-collapse: collapse; }
th, td { border: 1px solid #8886; padding: 4px 10px; }
.student { width: 110px; }
.quizzes { background: #38bdf826; }
</style>
<table>
<colgroup>
<col class="student">
</colgroup>
<colgroup class="quizzes" span="3"></colgroup>
<tr><th scope="col">Student</th><th scope="col">Quiz 1</th><th scope="col">Quiz 2</th><th scope="col">Quiz 3</th></tr>
<tr><th scope="row">Ola</th><td>9</td><td>10</td><td>8</td></tr>
<tr><th scope="row">Kuba</th><td>7</td><td>9</td><td>9</td></tr>
</table>Definition and usage
#The <colgroup> element defines a group of columns. You put it in the table right after <caption> and before <thead>. You describe the columns inside it with <col> elements or, if they should all look the same, with the span attribute alone.
Columns in HTML arenβt real containers: cells belong to rows, not to columns. Thatβs why through <colgroup> and <col> you can set only a few properties: background, border (with border-collapse: collapse), width, and visibility: collapse. Text color or alignment must be set on the cells.
Column groups are useful in comparison tables and schedules: you can highlight the weekend in a weekly timetable with a background, or set the width of the description column, without adding classes to every cell.
Syntax
#<colgroup>
<col>
<col span="2">
</colgroup>Attributes
#positive number (default 1)
The number of columns in the group. You use it only when the group contains no<col>elements. If there are some inside, they determine the number of columns.
The element also supports all global attributes, such as class, id, style and hidden.
Attributes in practice
#span
positive number (default 1)The number of columns in the group. You use it only when the group contains no <col> elements. If there are some inside, they determine the number of columns.
<style>
table { border-collapse: collapse; }
th, td { border: 1px solid #8886; padding: 4px 8px; text-align: center; }
.weekend { background: #f43f5e26; }
</style>
<table>
<caption>Minutes of study this week</caption>
<colgroup span="5"></colgroup>
<colgroup span="2" class="weekend"></colgroup>
<tr><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th><th>Sun</th></tr>
<tr><td>30</td><td>45</td><td>20</td><td>0</td><td>60</td><td>90</td><td>15</td></tr>
</table>Default CSS
#Most browsers display this element with these default styles:
colgroup {
display: table-column-group;
}Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- If you set
colorortext-alignon<colgroup>and nothing happens, itβs not a bug. Columns support only background, border, width, and visibility. - To highlight a single column in a group, use a
<col>with a class.
Related entries
#Spotted a mistake or missing an example? Write to us.