Code has been added to clipboard!

HTML Columns

Reading time 2 min
Published Mar 19, 2019
Updated Oct 1, 2019

HTML Columns: Main Tips

  • The <colgroup> element sets a group of columns in HTML tables.
  • This element is a parent of <col>.
  • The <table> element is a parent of <colgroup> in HTML.

Use and Purpose of colgroup

The <colgroup> element specifies a common formatting style for a group of HTML columns.

Note: a <col> element can be used within <colgroup> to define a different property for a single column in the group.

Example
<table>
  <colgroup>
    <col span="1" style="background-color: cornsilk;">
    <col style="background-color: bisque;">
  </colgroup>
  <tr>
    <th>Price</th>
    <th>Item</th>
    <th>Quantity</th>
  </tr>
  <tr>
    <td>100</td>
    <td>Peanut Butter</td>
    <td>2</td>
  </tr>
</table>

The <table> tag needs to be the parent of the <colgroup> element. The <colgroup> tag must be positioned before any <thead>, <tbody>, <tfoot>, <tr> tags and after <caption> tags.

span

The span attribute specifies a number of columns, indicating the span of a column group.

Example
<table>
  <colgroup span="2" style="background-color: darkgray;"></colgroup>
  <tr>
    <th>Name</th>
    <th>City</th>
  </tr>
  <tr>
    <td>James</td>
    <td>New York</td>
  </tr>
  <tr>
    <td>Laura</td>
    <td>Chicago</td>
  </tr>
</table>

Remember: always use <colgroup> with span when there are no <col> elements.

Deprecated Attributes

align

It defined the alignment of contents in a column group. Not supported in HTML5: apply the CSS text-align property instead.

Example
<table style="width: 100%;">
  <colgroup align="right"></colgroup>
  <tr>
    <th>Name</th>
  </tr>
  <tr>
    <td>James</td> 
  </tr>
</table>

char

It set the alignment of contents in a column group to a character. Not supported in HTML5.

Example
<table style="width: 100%;">
  <colgroup align="left"></colgroup>
  <colgroup align="char" char="."></colgroup>
  <tr>
    <th>Name</th>
    <th>Earning</th>
  </tr>
  <tr>
    <td>James</td>
    <td>$100.00</td>
  </tr>
</table>

charoff

It specified the number of characters to offset the content of a column group from the alignment character. Not supported in HTML5.

Example
<table style="width: 100%;">
  <colgroup align="left"></colgroup>
  <colgroup align="char" char="." charoff="2"></colgroup>
  <tr>
    <th>Name</th>
    <th>Earnings</th>
  </tr>
  <tr>
    <td>James</td>
    <td>$100.00</td>
  </tr>
  <tr>
    <td>Laura</td>
    <td>$10.00</td>
  </tr>
</table>

valign

It set the vertical alignment of a column group. Not supported in HTML5 - apply vertical-align as an alternative.

Example
<table style="height: 200px;">
  <colgroup valign="top">
  <colgroup valign="bottom">
  <tr>
    <th>Name</th>
    <th>Designation</th>
  </tr>
  <tr>
    <td>James</td>
    <td>Engineer</td>
  </tr>
</table>

width

It defined the width of a column group (usually in pixels). Since it is also not supported in HTML5, you should use the CSS width property.

Example
<table>
  <colgroup width="200">
  <tr>
    <th>Name</th>
    <th>Department</th>
  </tr>
  <tr>
    <td>James</td>
    <td>Construction</td>
  </tr>
</table>

Browser support

Chrome
1+
Edge
All
Firefox
1+
IE
All
Opera
All
Safari
All

Mobile browser support

Chrome
All
Firefox
4+
Opera
All
Safari
All
Basics
Introduction
Syntax
Editors
Basic Examples
Head Section
<!DOCTYPE>
Tags and Elements
Semantic Elements
Tags Reference
Attributes
Comments
Block and Inline Elements
Forms
Form Elements
Input
Responsive Web Design
Inline Scripts
Uniform Resource Locator
Redirect
XHTML
Geolocation
Drag and Drop
Local Storage
Web Workers
Server-Sent Events
Character Encoding
Text Formatting
Quotation and Citation Elements
Headings
Paragraphs
Links
Tables
Lists
Symbols
Space
Tab
Styles
Computer Code
Layout
Classes
Colors
Images
iframes
Audio Player
Video Player
YouTube Videos
Multimedia
Canvas
SVG
<!-- -->
<a>
<abbr>
<acronym> DEPRECATED
<address>
<applet> DEPRECATED
<article>
<aside>
<audio>
<b>
<base>
<basefont> DEPRECATED
<bdi>
<bdo>
<big> DEPRECATED
<blink> DEPRECATED
<blockquote>
<body>
<br>
<button>
<canvas>
<caption>
<center> DEPRECATED
<cite>
<code>
<col>
<colgroup>
<datalist>
<dd>
<del>
<details>
<dfn>
<dialog>
<dir> DEPRECATED
<div>
<dl>
<dt>
<em>
<embed>
<fieldset>
<figcaption>
<figure>
<font> DEPRECATED
<footer>
<form>
<frame> DEPRECATED
<frameset> DEPRECATED
<h1> – <h6>
<head>
<header>
<hr>
<html>
<i>
<iframe>
<img>
<input>
<ins>
<kbd>
<keygen> DEPRECATED
<label>
<legend>
<li>
<link>
<main>
<map>
<mark>
<menu>
<menuitem> DEPRECATED
<meta>
<meter>
<nav>
<noframes> DEPRECATED
<noscript>
<object>
<ol>
<optgroup>
<option>
<output>
<p>
<param>
<pre>
<progress>
<q>
<rp>
<rt>
<ruby>
<s>
<samp>
<script>
<section>
<select>
<small>
<source>
<span>
<strike> DEPRECATED
<strong>
<style>
<sub>
<summary>
<sup>
<table>
<tbody>
<td>
<tfoot>
<th>
<thead>
<time>
<title>
<tr>
<track>
<tt> DEPRECATED
<u>
<ul>
<var>
<video>
<wbr>