<colgroup> Tag in HTML

The <colgroup> tag in HTML is used to group one or more columns in a table and to apply common styles to those columns. It helps in improving the readability and maintainability of the HTML structure when working with tables that have multiple columns with similar properties.

Key Points on <colgroup> Tag:

Syntax of <colgroup> Tag:

Syntax Example

<colgroup>
          <col style="property:value">
          <col style="property:value">
        </colgroup>

Example of <colgroup> Tag in HTML:

This example demonstrates how to use the <colgroup> tag to group columns and apply styles to them.

Code Example


        <table>
          <colgroup>
            <col style="background-color: blue;">
            <col style="background-color: pink;">
            <col style="background-color: purple;">
          </colgroup>
          <tr>
            <td>Row 1, Column 1</td>
            <td>Row 1, Column 2</td>
            <td>Row 1, Column 3</td>
          </tr>
          <tr>
            <td>Row 2, Column 1</td>
            <td>Row 2, Column 2</td>
            <td>Row 2, Column 3</td>
          </tr>
        </table>
                

Output

Row 1, Column 1 Row 1, Column 2 Row 1, Column 3
Row 2, Column 1 Row 2, Column 2 Row 2, Column 3

The <colgroup> tag is useful when you want to apply common styles or properties (such as background colors or widths) to multiple columns in a table. Instead of applying the styles to each <col> tag individually, you can group them using the <colgroup> tag, making your HTML cleaner and more manageable.