HTML Tutorial

HTML 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

Following are some specifications about the HTML <colgroup> tag

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

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

Following are the various attributes available:

Attribute Value Description
align
  • left
  • center
  • right
  • justify
  • char
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
  • 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

Element chrome browser Chrome ie browser IE firefox browser Firefox opera browser Opera safari browser Safari
<colgroup> Yes Yes Yes Yes Yes

Next TopicHTML data Tag

 
 

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also
Close
Back to top button