HTML HTML Tutorial HTML Forms HTML Graphics HTML Media HTML APIs HTML Tags



HTML Tag: colgroup

The <colgroup> tag is used to group together a set of <col> tags in a table. It allows you to apply styles to multiple columns at once, rather than having to apply the styles to each individual column.

The <colgroup> tag is a container tag, which means that it does not have any content of its own. Instead, it is used to group together a set of <col> tags that are used to define the properties of the columns in a table.

Brief Explanation of HTML Tag: colgroup

The <colgroup> tag is used to group together a set of <col> tags in a table. It is typically used to apply styles to multiple columns at once, rather than having to apply the styles to each individual column.

The <colgroup> tag is a container tag, which means that it does not have any content of its own. Instead, it is used to group together a set of <col> tags that are used to define the properties of the columns in a table.

The <colgroup> tag has several attributes that can be used to define the properties of the columns in a table. These attributes include:

  • span: Specifies the number of columns that the <colgroup> tag should span.
  • width: Specifies the width of the columns in the <colgroup> tag.
  • align: Specifies the horizontal alignment of the columns in the <colgroup> tag.
  • valign: Specifies the vertical alignment of the columns in the <colgroup> tag.

Code Examples in HTML Tags

Here are some examples of how the <colgroup> tag can be used in HTML:

Example 1: Basic Usage

In this example, we use the <colgroup> tag to group together two <col> tags that define the properties of the columns in a table:

  
    <table>
      <colgroup>
        <col style="background-color: #ccc;">
        <col style="background-color: #ddd;">
      </colgroup>
      <tr>
        <td>Column 1</td>
        <td>Column 2</td>
      </tr>
    </table>
  

In this example, the <colgroup> tag is used to group together two <col> tags that define the properties of the columns in a table. The first <col> tag sets the background color of the first column to #ccc, while the second <col> tag sets the background color of the second column to #ddd.

Example 2: Spanning Columns

In this example, we use the <colgroup> tag to span two columns in a table:

  
    <table>
      <colgroup span="2" style="background-color: #ccc;"></colgroup>
      <tr>
        <td>Column 1</td>
        <td>Column 2</td>
        <td>Column 3</td>
      </tr>
    </table>
  

In this example, the <colgroup> tag is used to span two columns in a table. The span attribute is set to 2, which means that the <colgroup> tag should span two columns. The style attribute is used to set the background color of the columns to #ccc.

Example 3: Column Widths

In this example, we use the <colgroup> tag to set the width of the columns in a table:

  
    <table>
      <colgroup>
        <col width="50%">
        <col width="50%">
      </colgroup>
      <tr>
        <td>Column 1</td>
        <td>Column 2</td>
      </tr>
    </table>
  

In this example, the <colgroup> tag is used to set the width of the columns in a table. The width attribute is set to 50%, which means that each column should take up 50% of the available width.

Example 4: Column Alignment

In this example, we use the <colgroup> tag to set the horizontal alignment of the columns in a table:

  
    <table>
      <colgroup>
        <col align="left">
        <col align="right">
      </colgroup>
      <tr>
        <td>Column 1</td>
        <td>Column 2</td>
      </tr>
    </table>
  

In this example, the <colgroup> tag is used to set the horizontal alignment of the columns in a table. The align attribute is set to "left" for the first column and "right" for the second column.

Example 5: Column Vertical Alignment

In this example, we use the <colgroup> tag to set the vertical alignment of the columns in a table:

  
    <table>
      <colgroup>
        <col valign="top">
        <col valign="bottom">
      </colgroup>
      <tr>
        <td>Column 1</td>
        <td>Column 2</td>
      </tr>
    </table>
  

In this example, the <colgroup> tag is used to set the vertical alignment of the columns in a table. The valign attribute is set to "top" for the first column and "bottom" for the second column.

References

Activity