Lesson 5 of 8
Colgroup & Column Styling
The <colgroup> and <col> elements let you style entire columns at once - applying background colors, widths, or borders to all cells in a column without styling each <td> individually. This is more efficient than adding classes to every cell and makes your HTML cleaner.
Using Colgroup
Place <colgroup> right after the opening <table> tag (before <thead>):
<table>
<colgroup>
<col style="background: #f0f0f0;">
<col span="2" style="background: #e8f5e9;">
</colgroup>
...
</table>
The <col> element defines styling for one or more columns:
• <col style="background: yellow"> - Styles one column
• <col span="3" style="width: 100px"> - Styles 3 consecutive columns
Supported CSS properties are limited - mainly background, border, width, and visibility. For full column styling, use CSS nth-child selectors: td:nth-child(2) { color: blue; }
Style entire vertical columns efficiently without repeating classes on every cell
Colgroup Example
<!DOCTYPE html>
<html>
<body>
<table border="1" style="border-collapse:collapse;width:100%;max-width:500px">
<colgroup>
<col style="background:#f5f5f5;width:30%">
<col span="2" style="background:#e8f5e9;width:35%">
</colgroup>
<thead>
<tr><th>Name</th><th>Q1 Sales</th><th>Q2 Sales</th></tr>
</thead>
<tbody>
<tr><td>Alice</td><td>$50,000</td><td>$55,000</td></tr>
<tr><td>Bob</td><td>$42,000</td><td>$48,000</td></tr>
<tr><td>Charlie</td><td>$38,000</td><td>$41,000</td></tr>
</tbody>
</table>
<p><em>Notice: The name column has a grey background, while the sales columns are green - styled with <colgroup>.</em></p>
</body>
</html>Finished this lesson?