Tutorials /HTML /Table Colgroup

Table Colgroup

💡 Key Points on HTML Table Colgroup
  • The <colgroup> tag groups columns for styling or structural purposes.
  • The <col> tag defines individual columns or column spans within <colgroup>.
  • Use the span attribute to apply settings to multiple columns.
  • Styles like background-color or width can be applied to columns via CSS.
  • Improves table organization and simplifies column-specific styling.


HTML Table Colgroup: A Beginner's Guide

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.

What Is Colgroup?

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.

Why Use Colgroup? Colgroup streamlines column styling, reduces repetitive CSS, and improves table structure for better maintainability and readability.

Using Colgroup

Let’s explore how to use <colgroup> and <col> with examples and previews.

1. Basic Colgroup with Col

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>

Preview

Product Price
Pen $2


2. Colgroup with Span Attribute

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>

Preview

Employee Data
Name Role Salary
Alice Designer $50,000


3. Colgroup with CSS Classes

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>

Preview

Item Stock
Notebook 50


4. Colgroup with Colspan and Rowspan

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>

Preview

Weekly Schedule
Tasks
Monday 9 AM Meeting
10 AM Coding



Key Attributes and Styling for Colgroup

Key features of <colgroup> and <col>:

  • span: Defines how many columns a <col> affects (e.g., span="2").
    <col span="2" style="background-color: #f2f2f2;">
  • CSS Properties: Use width, background-color, or visibility (e.g., visibility: collapse to hide columns).
    <col style="width: 50%; background-color: #e6f0ff;">
  • Classes: Apply CSS classes to <col> for reusable styles.
    <col class="highlight">
Warning: Ensure the number of <col> elements or span values matches the table’s column count to avoid styling errors.



Best Practices for Colgroup

To make your use of <colgroup> effective:

  • Use <colgroup> to apply consistent styles to related columns.
  • Define column widths in <col> to simplify table layout.
  • Use CSS classes for reusable and maintainable styling.
  • Avoid overcomplicating with too many <col> elements; keep it simple.
  • Test column styles across devices to ensure consistent rendering.

Common Mistakes to Avoid

Beginners often make these errors:

  • Mismatching <col> or span with the table’s column count, causing styling issues.
  • Applying unsupported CSS properties (e.g., border) to <col>, as only certain properties like width and background-color are supported.
  • Not testing <colgroup> styles in different browsers, as rendering may vary.
Pro Tip: Use <colgroup> with CSS classes to apply consistent column styling and combine with :nth-child for advanced effects like vertical stripes.



Try It Yourself

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>

Preview

Course Schedule

Weekly Classes
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!

The Coding Journey provides high-quality, beginner-friendly, and advanced web development tutorials. Learn React, Next.js, JavaScript, Tailwind CSS, and more with hands-on projects. Build faster, code smarter, and level up your skills! 🚀

© 2025 All Rights Reserved | The coding journey