The <colgroup> tag in HTML tables allows you to group columns for styling or structural purposes, making it easier to apply consistent formatting to specific columns. Used with the <col> tag, it defines properties like width or background color for one or more columns. This tutorial explains how to use <colgroup> and <col> with examples and previews, helping beginners organize and style tables efficiently.
The <colgroup> tag defines a group of columns within a table, placed immediately after the <caption> and before the <tr> tags. The <col> tag specifies individual columns or a range of columns (using the span attribute) within the group. This setup simplifies applying styles or attributes to multiple columns at once.
Let’s explore how to use <colgroup> and <col> with examples and previews.
A simple <colgroup> to set column widths for a table.
<table style="border: 1px solid black; border-collapse: collapse; width: 100%;">
<colgroup>
<col style="width: 60%;">
<col style="width: 40%;">
</colgroup>
<tr>
<th style="border: 1px solid black; padding: 8px;">Product</th>
<th style="border: 1px solid black; padding: 8px;">Price</th>
</tr>
<tr>
<td style="border: 1px solid black; padding: 8px;">Pen</td>
<td style="border: 1px solid black; padding: 8px;">$2</td>
</tr>
</table>
Product | Price |
---|---|
Pen | $2 |
The span attribute in <col> applies styles to multiple columns.
<table style="border: 1px solid black; border-collapse: collapse; width: 100%;">
<caption style="padding: 8px;">Employee Data</caption>
<colgroup>
<col span="2" style="background-color: #f2f2f2;">
<col style="background-color: #ffffff;">
</colgroup>
<tr>
<th style="border: 1px solid black; padding: 8px;">Name</th>
<th style="border: 1px solid black; padding: 8px;">Role</th>
<th style="border: 1px solid black; padding: 8px;">Salary</th>
</tr>
<tr>
<td style="border: 1px solid black; padding: 8px;">Alice</td>
<td style="border: 1px solid black; padding: 8px;">Designer</td>
<td style="border: 1px solid black; padding: 8px;">$50,000</td>
</tr>
</table>
Name | Role | Salary |
---|---|---|
Alice | Designer | $50,000 |
Use CSS classes to apply styles to <col> for cleaner code.
<style>
.highlight { background-color: #e6f0ff; }
.standard { background-color: #ffffff; }
</style>
<table style="border: 1px solid black; border-collapse: collapse; width: 100%;">
<colgroup>
<col class="highlight">
<col class="standard">
</colgroup>
<tr>
<th style="border: 1px solid black; padding: 8px;">Item</th>
<th style="border: 1px solid black; padding: 8px;">Stock</th>
</tr>
<tr>
<td style="border: 1px solid black; padding: 8px;">Notebook</td>
<td style="border: 1px solid black; padding: 8px;">50</td>
</tr>
</table>
Item | Stock |
---|---|
Notebook | 50 |
Combine <colgroup> with colspan and rowspan for complex layouts.
<table style="border: 1px solid black; border-collapse: collapse; width: 100%;">
<caption style="padding: 8px;">Weekly Schedule</caption>
<colgroup>
<col style="width: 30%; background-color: #f2f2f2;">
<col span="2" style="background-color: #ffffff;">
</colgroup>
<tr>
<th colspan="3" style="border: 1px solid black; padding: 8px;">Tasks</th>
</tr>
<tr>
<td rowspan="2" style="border: 1px solid black; padding: 8px;">Monday</td>
<td style="border: 1px solid black; padding: 8px;">9 AM</td>
<td style="border: 1px solid black; padding: 8px;">Meeting</td>
</tr>
<tr>
<td style="border: 1px solid black; padding: 8px;">10 AM</td>
<td style="border: 1px solid black; padding: 8px;">Coding</td>
</tr>
</table>
Tasks | ||
---|---|---|
Monday | 9 AM | Meeting |
10 AM | Coding |
Key features of <colgroup> and <col>:
span="2"
).
<col span="2" style="background-color: #f2f2f2;">
width
, background-color
, or visibility
(e.g., visibility: collapse
to hide columns).
<col style="width: 50%; background-color: #e6f0ff;">
<col class="highlight">
To make your use of <colgroup> effective:
Beginners often make these errors:
border
) to <col>, as only certain properties like width
and background-color
are supported.:nth-child
for advanced effects like vertical stripes.
Create a simple HTML file and experiment with <colgroup> and <col>. Try different widths and background colors.
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.highlight-col { background-color: #e6f0ff; }
</style>
</head>
<body>
<h3>Course Schedule</h3>
<table style="border: 1px solid navy; border-collapse: collapse; width: 100%;">
<caption style="padding: 8px; font-weight: bold;">Weekly Classes</caption>
<colgroup>
<col class="highlight-col" style="width: 40%;">
<col style="width: 60%;">
</colgroup>
<tr>
<th style="border: 1px solid navy; padding: 8px;">Day</th>
<th style="border: 1px solid navy; padding: 8px;">Class</th>
</tr>
<tr>
<td style="border: 1px solid navy; padding: 8px;">Monday</td>
<td style="border: 1px solid navy; padding: 8px;">Math</td>
</tr>
</table>
</body>
</html>
Day | Class |
---|---|
Monday | Math |
By mastering <colgroup> and <col>, you can efficiently style and organize table columns, enhancing your website’s data presentation. Practice regularly to get comfortable!