Go to main content
Bypass navigation bar HTML Reference HTML Tags HTML Tutorials
Bypass language selection
Bypass location bar

Location: Home > HTML Reference > HTML Tags > HTML colgroup tag

Bypass main content

HTML colgroup tag

Note: If you don't know what an element/tag is and/or how you must use it we recommend you read our HTML tags and attributes tutorial, which you can find in our HTML tutorials section.

Bypass main content

Description

The HTML colgroup element is used in tables to group its columns. It must be inserted right after the starting tag of the HTML table element.

The "span" attribute is the one that allows column groupping, as it specifies the numbers of columns that will be grouped, from left to right, respecting the order. Its default value is 1.

Even when the main reason of its existence is to semantically group columns, this element can also define common attributes to the affected columns. This feature can be strengthened using this element together with the HTML col tag.

Note, that some attributes available for this tag are purely presentational (align, valign, width, char and charoff), reason for which their use is not recommended.

Examples

The following example shows a common use of the HTML colgroup element. There, the columns are grouped thematically.

Code
<table>
<colgroup></colgroup>
<colgroup span="3"></colgroup>
<colgroup span="2"></colgroup>
<tr>
<th>Country</th>
<th>Salmon</th>
<th>Shrimps</th>
<th>Oysters</th>
<th>Rice</th>
<th>Wheat</th>
</tr>
<tr>
<td>United Kingdom</td>
<td>2050</td>
<td>1545</td>
<td>1156</td>
<td>5007</td>
<td>12254</td>
</tr>
<tr>
<td>United States</td>
<td>1358</td>
<td>1884</td>
<td>784</td>
<td>10597</td>
<td>24554</td>
</tr>
<tr>
<td>Australia</td>
<td>985</td>
<td>65</td>
<td>518</td>
<td>2548</td>
<td>10548</td>
</tr>
</table>
View
CountrySalmonShrimpsOystersRiceWheat
United Kingdom 2050 1545 1156 5007 12254
United States 1358 1884 784 10597 24554
Australia 985 65 518 2548 10548

In the next example, the same table is inserted, this time, using the HTML coulgroup element together with the HTML col element to define some attributes to the columns. The semantic of the previous example remains untouched.

Code
<table>
<colgroup style="background: #BBBBFF;"></colgroup>
<colgroup span="3">
<col style="background: #AAFFAA;" />
<col style="background: #BBFFBB;" />
<col style="background: #CCFFCC;" />
</colgroup>
<colgroup span="2">
<col style="background: #FFBBBB;" />
<col style="background: #FFCCCC;" />
</colgroup>
<tr>
<th>Country</th>
<th>Salmon</th>
<th>Shrimps</th>
<th>Oysters</th>
<th>Rice</th>
<th>Wheat</th>
</tr>
<tr>
<td>United Kingdom</td>
<td>2050</td>
<td>1545</td>
<td>1156</td>
<td>5007</td>
<td>12254</td>
</tr>
<tr>
<td>United States</td>
<td>1358</td>
<td>1884</td>
<td>784</td>
<td>10597</td>
<td>24554</td>
</tr>
<tr>
<td>Australia</td>
<td>985</td>
<td>65</td>
<td>518</td>
<td>2548</td>
<td>10548</td>
</tr>
</table>
View
CountrySalmonShrimpsOystersRiceWheat
United Kingdom 2050 1545 1156 5007 12254
United States 1358 1884 784 10597 24554
Australia 985 65 518 2548 10548

Attributes

id (name)

The "id" attribute assigns an identifier to the associated element. This identifier must be unique in the document and can be used to refer to that element in other instances (e.g., from client-side scripts).

<p id="paragraph1">This is the first paragraph, named "paragraph1". To dynamically change its properties use this identifier.</p>

class (cdata)

The "class" attribute assigns a class name (or a list of class names separated by spaces) to the container element. It is used together with style sheets and tells the browser the class (or classes) to which the element is associated.

A class gives presentational attributes to elements (read more at the Cascading Style Sheets tutorial).

<p class="references">This article is based on the book "Wind in the trees" by Jhon L. Brooks</p>
<p class="references important">This article is based on the book "Wind in the trees" by Jhon L. Brooks... and is more important than the one before.</p>

style (style)

This attribute is used to define presentational attributes for the containing element, and its value should be composed by style sheets properties. Although in some cases it can become useful, a better practice is to place presentational attributes in external files, relating them to elements with the "class" attribute. This way you keep the semantic and presentational parts of your document separated.

You can find more information about presentational attributes at the Cascading Style Sheets tutorial.

<p style="color: #0000FF; font-size: 12pt">This is a paragraph with a defined style.</p>
<p>And this is another text without style.</p>

title (text)

The purpose of this attribute is to provide a title for the element. Its value must be a short and accurate description of the element. Browsers usually render it as a "tool tip" when the user puts the mouse pointer over the element for a short period of time.

Code
<a title="HTMLQuick.com" href="http://www.htmlquick.com">HTML code</a>
View

lang (langcode)

Specifies the language of an element's content. The default value is "unknown".

When writing XHTML 1.0 documents, the attribute used to specify the language of an elements is "xml:lang". For forward and backward compatibility both attributes can be used simultaneously as in the example below. Note, that in XHTML 1.1 the "lang" attribute has been completely replaced by "xml:lang" and its use is no longer valid.

<p lang="en" xml:lang="en">This is a paragraph in English.</p>
<p lang="es" xml:lang="es">Este es un párrafo en español.</p>

dir

This attribute indicates the direction in which the texts of the element must be read. This includes content, attribute values and tables. It has two possible values that are case-insensitive:

  • RTL: Right to left.
  • LTR: Left to right.
<q lang="he" dir="rtl">...an Hebrew quotation...</q>

span (number)

This attribute specifies the number of consecutive columns "spanned" or affected by the element or its attributes.

<colgroup span="2" style="border: none;"></colgroup>
<colgroup span="5" style="background: red;"></colgroup>

width (multi-length)

This attribute specifies the width for all the cells in the affected columns.

<colgroup width="50"></colgroup>

align

Sets the alignment for the text inside a cell in the affected columns. It can take one of the following case-insensitive values:

  • left: Data is aligned to the left. This is the default value for common cells (HTML td tag).
  • center: Data is centered. This is the default value for table headers (HTML th tag).
  • right: Data is aligned to the right.
  • justify: Data is justified, i.e. aligned to both margins.
  • char: The text is aligned to a specific character. It is used together with the "char" attribute.
<colgroup align="center"></colgroup>

valign

Sets the vertical alignment for the text inside a cell in the affected columns. It can take one of the following case-insensitive values:

  • top: Data is aligned to the top.
  • middle: Data is vertically centered.
  • bottom: Data is aligned to the bottom.
  • baseline: All the cells in a row with this alignment should have their first text line on a common baseline.
<colgroup valign="top"></colgroup>

char (character)

Specifies a single character that will act as axis for text alignment. For its value to be considered, the "align" attribute must be set to "char".

Note that browsers aren't required to support this attribute. This, added to its presentational nature, constitute a good reason not to rely on it.

<colgroup align="char" char="."></colgroup>

charoff (length)

Specifies an offset for the alignment character (first occurrence) on each line. Text direction, set with the "dir" attribute, must be taken into account to decide which the first occurrence will be.

Note that browsers aren't required to support this attribute. This, added to its presentational nature, constitute a good reason not to rely on it.

<colgroup align="char" char="." charoff="1"></colgroup>

Events

  • onclick
  • ondblclick
  • onmousedown
  • onmouseup
  • onmouseover
  • onmousemove
  • onmouseout
  • onkeypress
  • onkeydown
  • onkeyup

See a complete list and information about events in HTML

Diseño y desarrollo: Latitud29.com

Links and logos|Contact|Beyond HTML|Tools and resources|Sitemap|Webmaster|Donate