The <colgroup> tag in HTML is used to group one or more columns in a table so you can apply styles and formatting to them as a unit. It is mainly used inside a <table> element and works together with the <col> tag.
If you have ever worked with tables in HTML, you know that styling rows is easy using <tr>, but styling columns is not as direct. That is where the <colgroup> tag becomes useful. It gives you a simple way to target entire columns instead of styling each cell one by one.
This article explains what the <colgroup> tag is, how it works, when to use it, and how to use it properly.
Basic Idea of <colgroup>
The <colgroup> tag defines a group of columns in a table. Inside it, you can use <col> elements to specify individual columns.
Here is a simple structure:
<table>
<colgroup>
<col>
<col>
</colgroup>
<tr>
<td>Item</td>
<td>Price</td>
</tr>
</table>
In this example:
<colgroup>groups the columns<col>represents each column
This setup allows you to style or control columns easily.
Why Use <colgroup>
The main reason to use <colgroup> is to apply styles to columns instead of rows or individual cells.
Without <colgroup>, you would need to style every <td> or <th> manually. That can be repetitive and harder to manage.
Benefits:
- Cleaner code
- Easy column styling
- Better control over table layout
- Improves readability of your HTML
Simple Example with Styling
Here is a more practical example:
<table border="1">
<colgroup>
<col style="background-color: lightgray;">
<col style="background-color: lightblue;">
</colgroup>
<tr>
<th>Name</th>
<th>Score</th>
</tr>
<tr>
<td>John</td>
<td>85</td>
</tr>
<tr>
<td>Mary</td>
<td>92</td>
</tr>
</table>
What happens here:
- The first column has a light gray background
- The second column has a light blue background
Instead of styling each cell, the columns are styled once using <colgroup>.

The <col> Tag Explained
The <col> tag is used inside <colgroup> to define individual columns.
Example:
<colgroup>
<col>
<col>
</colgroup>
Each <col> represents one column in the table.
You can also control multiple columns with one <col> using the span attribute.
Using the span Attribute
The span attribute allows one <col> element to apply to multiple columns.
Example:
<colgroup>
<col span="2" style="background-color: lightyellow;">
</colgroup>
What this means:
- The first two columns will have a light yellow background
This is useful when multiple columns share the same style.
Full Table Example
Here is a complete example using everything together:
<table border="1">
<colgroup>
<col style="background-color: lightgreen;">
<col span="2" style="background-color: lightcoral;">
</colgroup>
<tr>
<th>Name</th>
<th>Math</th>
<th>Science</th>
</tr>
<tr>
<td>John</td>
<td>80</td>
<td>75</td>
</tr>
<tr>
<td>Jane</td>
<td>90</td>
<td>88</td>
</tr>
</table>
Result:
- First column (Name) is green
- Next two columns (Math and Science) are coral
Where to Place <colgroup>
The <colgroup> tag must be placed directly inside the <table> element, before any <tr> elements.
Correct:
<table>
<colgroup>
<col>
</colgroup>
<tr>
<td>Data</td>
</tr>
</table>
Incorrect:
<table>
<tr>
<colgroup> <!-- Wrong placement -->
</colgroup>
</tr>
</table>
Always place <colgroup> at the top of the table.
What Can You Style with <colgroup>
You can apply CSS properties to columns using <col> inside <colgroup>.
Common properties include:
background-colorwidthborder
Example:
<colgroup>
<col style="width: 200px;">
<col style="width: 100px;">
</colgroup>
This sets different widths for columns.
Important Notes
1. It Does Not Affect Content Directly
The <colgroup> tag does not change the content inside the cells. It only affects styling and layout.
2. Limited Styling Support
Not all CSS properties work on <col> elements. For example:
- You cannot apply padding directly
- You cannot control text alignment reliably
For those, you still need to style <td> or <th>.
3. Works Best with CSS
While inline styles work, it is better to use CSS:
<style>
col.first {
background-color: lightblue;
}
</style>
<table>
<colgroup>
<col class="first">
</colgroup>
</table>
This keeps your code cleaner.
<colgroup> vs Styling <td>
Here is the difference:
| Method | Use Case |
|---|---|
<colgroup> | Styling entire columns |
<td> / <th> | Styling individual cells |
Use <colgroup> when you want consistent column styling.
When to Use <colgroup>
You should use <colgroup> when:
- You have large tables
- You want consistent column styles
- You want cleaner HTML code
- You need to manage column widths easily
When Not to Use It
Avoid <colgroup> if:
- You only need to style a few cells
- You need advanced styling not supported by
<col>
Browser Support
The <colgroup> tag is supported in all modern browsers, including:
- Chrome
- Firefox
- Safari
- Edge
It has been part of HTML for a long time, so compatibility is not an issue.
Common Mistakes
Here are mistakes to avoid:
- Placing
<colgroup>in the wrong position - Forgetting to match the number of columns
- Expecting it to control text alignment or padding
- Overusing inline styles
Keeping things simple will help you avoid problems.
Best Practices
- Always define columns clearly
- Use CSS instead of inline styles
- Combine with
<col>for better control - Keep your table structure clean
Simple Real-World Example
Imagine a table showing products:
<table border="1">
<colgroup>
<col style="background-color: lightgray;">
<col style="background-color: white;">
<col style="background-color: lightyellow;">
</colgroup>
<tr>
<th>Product</th>
<th>Price</th>
<th>Status</th>
</tr>
<tr>
<td>Laptop</td>
<td>$800</td>
<td>Available</td>
</tr>
<tr>
<td>Phone</td>
<td>$500</td>
<td>Out of Stock</td>
</tr>
</table>
Each column has a clear visual style, making the table easier to read.
The <colgroup> tag is a simple but powerful tool in HTML for working with tables. It allows you to group columns and apply styles to them in a clean and efficient way.
Instead of repeating styles for each cell, you can control entire columns with just a few lines of code. This makes your HTML easier to manage, especially when dealing with large tables.
Even though it has some limitations, it is still very useful for layout and basic styling. When combined with CSS, it becomes even more effective.
If you are building tables in HTML, learning how to use <colgroup> properly will help you write better and cleaner code.