Course contentsShow
← Web Development
Lesson 116 of 2,9822. HTML FundamentalsFree lesson

Column Groups: <colgroup> and <col>

Apply styles and attributes to entire columns efficiently.

Column Groups: <colgroup> and <col>

You'll learn how to apply styles and attributes to entire table columns at once, making your HTML tables cleaner and more maintainable.

What Are Column Groups?

When working with tables, you might want to style or configure entire columns the same way—perhaps making all columns in one section have the same width or background color. Instead of repeating attributes on every single <td> or <th> in a column, HTML gives you <colgroup> and <col> elements to define properties for entire columns in one place.

Think of it like formatting columns in a spreadsheet: rather than selecting every individual cell in column B, you just click the column header once and apply formatting to the whole column.

How They Work

The <colgroup> element wraps one or more <col> elements and appears directly after the <caption> (if present) but before any table content like <thead>, <tbody>, or <tr>.

Each <col> element represents one column in your table. You can add styling attributes or classes to these elements, and they'll affect the entire column:

<table>
  <caption>Sales Report</caption>
  <colgroup>
    <col style="background-color: #f0f0f0;">
    <col style="background-color: #e0e0e0;">
    <col>
  </colgroup>
  <thead>
    <tr>
      <th>Product</th>
      <th>Q1</th>
      <th>Q2</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Widgets</td>
      <td>100</td>
      <td>150</td>
    </tr>
  </tbody>
</table>

Here, the first column gets a light gray background, the second gets a slightly darker gray, and the third has no special styling—all without touching individual cells.

Key Takeaway: Use <colgroup> and <col> to efficiently apply styles and attributes to entire table columns, keeping your code DRY (Don't Repeat Yourself) and easier to maintain.