HTML Colgroup Tag: Structure, Attributes, and Examples
What is HTML Colgroup Tag: Structure, Attributes, and Examples? The HTML (column group) element specifies formatting and styles for entire columns in a table without repeating CSS classes across hundreds of individual cell...
What is the HTML <colgroup> tag and how does it style entire table columns efficiently?
Explanation & In-Depth Details
2) Placement in Document: The <colgroup> element must be placed before any <thead>, <tbody>, <tfoot>, or <tr> elements, and directly after <caption> if present.
3) The <col> Child Tag: Inside <colgroup>, individual columns are targeted using void <col> elements. A <col> can span multiple columns using the 'span' attribute (e.g. <col span="2">).
4) Supported CSS Properties on <col>: Due to browser rendering engine constraints, only four CSS properties can be applied to <col> and <colgroup>:
- 'background' (background-color, background-image)
- 'border' (when border-collapse is collapse)
- 'width' (column width)
- 'visibility' (visibility: collapse)
5) Combining Multiple Column Groups: You can define multiple <colgroup> tags to separate customer data columns from pricing and invoice calculation columns.
Code Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Colgroup Styling Showcase</title>
<style>
table { border-collapse: collapse; width: 100%; max-width: 650px; font-family: sans-serif; }
th, td { border: 1px solid #cbd5e1; padding: 10px; text-align: left; }
/* Styling entire columns via colgroup classes */
.col-id { width: 60px; background-color: #f1f5f9; text-align: center; }
.col-featured { background-color: #eff6ff; font-weight: 600; }
</style>
</head>
<body>
<h2>User Subscription Tiers</h2>
<table>
<!-- Column Group Definition -->
<colgroup>
<col class="col-id">
<col style="width: 180px;">
<col class="col-featured">
<col style="width: 100px;">
</colgroup>
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Plan Name</th>
<th scope="col">Storage Capacity</th>
<th scope="col">Monthly Fee</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Starter</td>
<td>15 GB Cloud Storage</td>
<td>$0.00</td>
</tr>
<tr>
<td>2</td>
<td>Professional</td>
<td>500 GB Cloud Storage</td>
<td>$12.00</td>
</tr>
<tr>
<td>3</td>
<td>Enterprise</td>
<td>5 TB Cloud Storage</td>
<td>$49.00</td>
</tr>
</tbody>
</table>
</body>
</html>Interview Question & Tips
A1: According to CSS2.1 specifications, column elements do not exist in the DOM element tree as ancestors of cell elements. Browsers only permit four specific properties on columns: background, border, width, and visibility.
Q2: What is the function of the 'span' attribute on a <col> tag?
A2: The 'span' attribute defines how many consecutive columns the <col> styles apply to. For example, '<col span="3" style="background: #f8fafc;">' applies background color across the next three columns.
Q3: Can a table have multiple <colgroup> elements?
A3: Yes! Multiple <colgroup> elements can be used to demarcate major structural groups of columns (e.g. personal information columns vs billing columns).
Best Practices
This study reference for HTML Colgroup Tag: Structure, Attributes, and Examples is curated and verified against recognized curriculum benchmarks, peer-reviewed textbooks, and official technical documentation: