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

Following is the syntax for HTML <colgroup> tag −

<colgroup>...</colgroup>

Example

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>

When we execute the above code, it will generate an output consisting of a table displayed on the webpage applied with CSS.

Example

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>

On running the above code, the output window will pop up, displaying the table along with the CSS applied to the two columns mentioned using the span attribute displayed on the webpage.

Example

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>

When we execute the above code, it will generate an output consisting of the table along with CSS applied in the middle of the table by using the 1st vacant column displayed on the webpage.

Example

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>

On running the above code, the output window will pop up, consisting of the table along with some of its columns collapsed by using the visibility property displayed on the webpage.

html_tags_reference.htm
Advertisements