Introduction:
The table is created as a sequence of rows. Setting a row's attributes affects all cells in that row. But if you want to change the properties of a particular column you have to set the property for each <td> element in a row of that column which is very tedious. So to effect each cell of a particular column in a single row, we use the <colgroup> and <cols> elements. These elements are used to create some advanced features like grouping columns, sharing attributes between aggregate columns, partitioning the table into multiple partitions, etc.
What is <cols> tag?
HTML <colgroup> tag specifies group of column within an HTML table. It is used a parent container of one or more <col> elements to apply different properties in an HTML table. The <colgroup> element is always in the <table> element and usually before the first row definition. The starting tag <colgroup> is essential but the ending tag </colgroup> is optional. By using colgroup> element you can apply formatting to group of columns instead of applying formatting to each column separately.
NOTE: The <colgroup> tag must be used with <table> element after the <caption> and before <thead> or <tbody> elements.
What is <cols> tag?
Like <colgroup> element, the <col> element or tag is also used for formatting columns. This element is always used inside the <colgroup> element. It is an empty element as it contains no contents but only the attributes used for formatting purpose. It is usually used when you need to format columns in the middle differently from the rest of the columns. The attributes that can be used with <cols> element are the same as that of <colsgroup> element.
Syntax
<colgroup>
<col>
<col>
</colgroup>
Following are some details regarding the HTML <colgroup> element:
| Display | None |
|---|---|
| Start tag/End tag | Start tag and End Tag |
| Usage | HTML tables |
Now, let us consider an example of a <colsgroup> element in a table containing 3 columns.
Example
<!DOCTYPE html>
<html>
<head>
<title>Colgroup tag</title>
</head>
<body>
<h2>Example of Colgroup Tag</h2>
<table border="1">
<colgroup>
<col style="background-color: green" width="40">
<col span="2" style="background-color: #ff7256" width="80">
</colgroup>
<tr>
<th>Sr.No</th>
<th>Product</th>
<th>Price</th>
</tr>
<tr>
<td>1</td>
<td>Rice</td>
<td>85</td>
</tr>
<tr>
<td>2</td>
<td>Butter</td>
<td>260</td>
</tr>
<tr>
<td>3</td>
<td>Mango</td>
<td>125</td>
</tr>
</table>
</body>
</html>
Output:
To modify the characteristics of a specific column, there exists a range of attributes that can be applied with the <colgroup> tag. Typically, these attributes are formatted as follows:
Syntax
<colgroup attribute_name = "value">......</colgroup>
Following are the various attributes available:
| Attribute | Value | Description |
|---|---|---|
| align | leftcenterrightjustifychar | It specifies the alignment of the column content. (Not supported in HTML5). |
char |
character | It specifies the alignment of the content in a column group to the character. (Not supported in HTML5). |
| charoff | number | It sets the number of characters to offset the column data from the alignment character specified by the char attribute. (Not supported in HTML5). |
span |
number | The span attribute is used to specify the number of columns that a colgroup should span. The default value of this attribute is 1.For Example:<colgroup align = "center" span ="2">It will horizontally align the contents of the first two columns to the center. |
| valign | topmiddlebottombaseline | It specifies the vertical alignment of the column group. (Not supported in HTML5). |
| width | %Pixelsrelative_length | It specifies the width of the group of column. (Not supported in HTML5). |
| bgcolor | color_name or codeRGBHexadecimal value | The bgcolor attribute is used to set the background color of the column in the table. The can be represented either by their name or by their hexadecimal equivalent.For Example:<colgroup bgcolor = "green">On specifying this attribute to green value, the first column of the table will have green background.Similarly you can also write it as:<colgroup bgcolor = "#00FF00"> |
- left
- center
- right
- justify
- char
- middle
- bottom
- baseline
- Pixels
- relative_length
- color_name or code
- Hexadecimal value
Global Attribute
The <colgroup> tag is compatible with all Global attributes in HTML.
Event Attribute
HTML <colgroup> tag enables the utilization of all event attributes within an HTML document.
Supporting Browsers
| Element | Chrome | IE | Firefox | Opera | Safari |
|---|---|---|---|---|---|
<colgroup> |
Yes | Yes | Yes | Yes | Yes |