HTML - <colgroup> Tag



HTML <colgroup> tag is used to describe the collection of one or more columns in a table for formatting. Instead of duplicating the styles for each column and each row, it is excellent for applying styles to full columns.

To declare distinct characteristics for a column use the <col> tag within the <colgroup> tag. The <colgroup> tag must be a child of the <table> element, coming after any <caption> elements and before any <thead>, <tbody>, <tfoot>, and <tr> elements.

Syntax

<colgroup>...</colgroup>

Attribute

HTML colgroup tag supports Global and Event attributes of HTML. Some Specific attributes as well which are listed bellow.

Attribute Value Description
span number Specifies the number of consecutive columns the <col> element spans.
align left
right
center
justify
Specifies the alignment of text content(Deprecated).
bgcolor color Specifies the background color of each column cell(Deprecated).
char character Specifies the alignment of the content to a character of each column cell(Deprecated).
charoff number Specifies the number of characters to offset the column cell content from the alignment character specified by the char attribute(Deprecated).
valign baseline
bottom
middle
top
Specifies the vertical alignment of each column cell(Deprecated).

Examples of HTML colgroup Tag

Bellow examples will illustrate the usage of colgroup tag. Where, when and how to use colgroup tag to offer information about columns.

Using colgroup tag with Table

Following is an example where we are going to use the <colgroup> tag in the table.

<!DOCTYPE html>
<html>
<body>
   <table border="1">
      <colgroup>
         <col style="background-color:#ABEBC6">
         <col style="background-color:#BB8FCE">
      </colgroup>
      <tr>
         <th>Roll.No</th>
         <th>Name</th>
      </tr>
      <tr>
         <td>1</td>
         <td>Maya</td>
      </tr>
      <tr>
         <td>2</td>
         <td>Surya</td>
      </tr>
      <tr>
         <td>3</td>
         <td>Ram</td>
      </tr>
   </table>
</body>
</html>

Specifying Consecutive Columns

Consider another scenario where we are going to use the span attribute with the <colgroup> tag.

<!DOCTYPE html>
<html>
   <style>
      table,
      th,
      td {
         border: 1.5px solid #DE3163;
      }
   </style>
<body>
   <table>
      <colgroup>
         <col span="2" style="background-color:#F9E79F">
      </colgroup>
      <tr>
         <th>Item</th>
         <th>Price</th>
      </tr>
      <tr>
         <td>5-Star</td>
         <td>$10</td>
      </tr>
      <tr>
         <td>Dairy-Milk</td>
         <td>$50</td>
      </tr>
      <tr>
         <td>Kitkat</td>
         <td>$20</td>
      </tr>
   </table>
</body>
</html>

Styling Colgroup Element

In the following example, we are going to apply CSS to the middle of the table by passing an empty <col> without applying the style for the column before.

<!DOCTYPE html>
<html>
   <style>
      table,
      th,
      td {
         border: 1.5px solid #85C1E9;
         border-collapse: collapse;
      }
   </style>
<body>
   <table style="width: 50%;">
      <colgroup>
         <col span="1">
         <col span="1" style="background-color: #ABEBC6 ">
      </colgroup>
      <tr>
         <th>MON</th>
         <th>TUE</th>
         <th>WED</th>
         <th>THU</th>
      </tr>
      <tr>
         <td>1</td>
         <td>2</td>
         <td>3</td>
         <td>4</td>
      </tr>
      <tr>
         <td>5</td>
         <td>6</td>
         <td>7</td>
         <td>8</td>
      </tr>
      <tr>
         <td>9</td>
         <td>10</td>
         <td>11</td>
         <td>12</td>
      </tr>
   </table>
</body>
</html>

Hiding Column

Let’s look at another example where we are going to hide the column using the visibility:collapse property.

<!DOCTYPE html>
<html>
   <style>
      table,
      th,
      td {
         border: 1.5px solid #DE3163;
         border-collapse: collapse;
      }
   </style>
<body>
   <table style="width: 100%;">
      <colgroup>
         <col span="2">
         <col span="3" style="visibility: collapse">
      </colgroup>
      <tr>
         <th>ID</th>
         <th>Employee</th>
         <th>Department</th>
         <th>Age</th>
      </tr>
      <tr>
         <td>112</td>
         <td>Rahul</td>
         <td>IT</td>
         <td>22</td>
      </tr>
      <tr>
         <td>113</td>
         <td>Ram</td>
         <td>Associate</td>
         <td>24</td>
      </tr>
      <tr>
         <td>114</td>
         <td>Maya</td>
         <td>HR</td>
         <td>30</td>
      </tr>
      <tr>
         <td>115</td>
         <td>Rahul</td>
         <td>BPO</td>
         <td>23</td>
      </tr>
   </table>
</body>
</html>

Supported Browsers

Tag Chrome Edge Firefox Safari Opera
colgroup Yes Yes Yes Yes Yes
html_tags_reference.htm
Advertisements