Consent

This site uses third party services that need your consent.

Skip to content
Steven Roland
  • Colgroup Element: Grouping Table Columns

    The <colgroup> HTML element is used to group one or more <col> elements within a table, allowing developers to apply styles or attributes to entire columns rather than individual cells. This element is particularly useful for improving the readability and maintainability of code, especially when dealing with large tables. By defining column groups, you can apply consistent styling across multiple columns, making it easier to manage and update the table's appearance.

    The <colgroup> element must be a child of the <table> element and is typically placed after any <caption> elements and before any <thead>, <tbody>, <tfoot>, and <tr> elements. It can include a span attribute, which specifies the number of columns the group should span. If the span attribute is present, the <colgroup> element should not contain any <col> elements. Here is an example of how the <colgroup> tag can be used:

    <table>
      <caption>Weekly Schedule</caption>
      <colgroup span="5" class="weekdays"></colgroup>
      <colgroup span="2" class="weekend"></colgroup>
      <tr>
        <th>Mon</th>
        <th>Tue</th>
        <th>Wed</th>
        <th>Thu</th>
        <th>Fri</th>
        <th>Sat</th>
        <th>Sun</th>
      </tr>
      <tr>
        <td>Work</td>
        <td>Work</td>
        <td>Work</td>
        <td>Work</td>
        <td>Work</td>
        <td>Relax</td>
        <td>Relax</td>
      </tr>
    </table>

    Valid Attributes for <colgroup>

    element should span.
    Attribute Description
    span Specifies the number of consecutive columns the

    The <colgroup> element supports all global attributes, allowing for additional customization and interaction through CSS and JavaScript. While it does not directly affect the layout of the table, it provides a way to apply consistent styling to columns, enhancing the visual structure of tables.