HTML Colgroup Tag - Javatpoint

next → ← prevHTML <colgroup> tag

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 <colsgroup> 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 specifications about the HTML <colgroup> tag

DisplayNone
Start tag/End tagStart tag and End Tag
UsageHTML 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> Test it Now

Output:

HTML colgroup tag

In order to change the properties of a particular column, a list of attributes are available that can use with <colgroup> element. These are generally written as:

Syntax

<colgroup attribute_name = "value">......</colgroup>

Following are the various attributes available:

AttributeValueDescription
align
  • left
  • center
  • right
  • justify
  • char
It specifies the alignment of the column content. (Not supported in HTML5).
charcharacterIt specifies the alignment of the content in a column group to the character. (Not supported in HTML5).
charoffnumberIt sets the number of characters to offset the column data from the alignment character specified by the char attribute. (Not supported in HTML5).
spannumberThe 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
  • top
  • middle
  • bottom
  • baseline
It specifies the vertical alignment of the column group. (Not supported in HTML5).
width
  • %
  • Pixels
  • relative_length
It specifies the width of the group of column. (Not supported in HTML5).
bgcolor
  • color_name or code
  • RGB
  • Hexadecimal 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">

Global Attribute

HTML <colgroup> tag supports all Global attributes in HTML.

Event Attribute

HTML <colgroup> tag supports all event attributes in HTML.

Supporting Browsers

Elementchrome browser Chromeie browser IEfirefox browser Firefoxopera browser Operasafari browser Safari
<colgroup>YesYesYesYesYes
Next TopicHTML data Tag← prev next →

Từ khóa » Html Table Hide Column Colgroup