HTML <col> Tag
The HTML <col>
tag is used for specifying column properties for each column within a <colgroup>
element.
The <col>
tag can be handy if you need to specify styles across one or more columns. Rather than apply the styles at the individual <td>
level, you can apply it to the <col>
element. Then all <td>
(and <th>
) elements contained within that <col>
will be styled accordingly.
Note: You can only apply 4 CSS properties to table columns. See "CSS Limitation" below for more information.
Syntax
The <col>
tag is written as <col>
(no end tag). You can use the span
attribute to specify the number of columns to span.
The tag be used within a <colgroup>
tag that doesn't have a span
attribute.
Like this:
Examples
Basic tag usage
Here we apply a different background color to the columns using the <col>
tag in conjunction with the CSS background
property.
Attributes
Attributes can be added to an HTML element to provide more information about how the element should appear or behave.
There are 3 kinds of attributes that you can add to your HTML tags: Element-specific, global, and event handler content attributes.
The <col>
element accepts the following attributes.
Element-Specific Attributes
This table shows the attributes that are specific to the <col>
tag/element.
Attribute | Description |
---|---|
span | Specifies how many columns to span. This attribute's value must be a valid non-negative integer greater than zero. |
Global Attributes
The following attributes are standard across all HTML5 elements. Therefore, you can use these attributes with the <col>
tag , as well as with all other HTML tags.
- accesskey
- class
- contenteditable
- contextmenu
- dir
- draggable
- dropzone
- hidden
- id
- inert
- itemid
- itemprop
- itemref
- itemscope
- itemtype
- lang
- spellcheck
- style
- tabindex
- title
- translate
For a full explanation of these attributes, see HTML 5 global attributes.
Event Handler Content Attributes
Event handler content attributes enable you to invoke a script from within your HTML. The script is invoked when a certain "event" occurs. Each event handler content attribute deals with a different event.
Below are the standard HTML5 event handler content attributes.
Again, you can use any of these with the <col>
element, as well as any other HTML5 element.
- onabort
- oncancel
- onblur
- oncanplay
- oncanplaythrough
- onchange
- onclick
- oncontextmenu
- ondblclick
- ondrag
- ondragend
- ondragenter
- ondragexit
- ondragleave
- ondragover
- ondragstart
- ondrop
- ondurationchange
- onemptied
- onended
- onerror
- onfocus
- onformchange
- onforminput
- oninput
- oninvalid
- onkeydown
- onkeypress
- onkeyup
- onload
- onloadeddata
- onloadedmetadata
- onloadstart
- onmousedown
- onmousemove
- onmouseout
- onmouseover
- onmouseup
- onmousewheel
- onpause
- onplay
- onplaying
- onprogress
- onratechange
- onreadystatechange
- onscroll
- onseeked
- onseeking
- onselect
- onshow
- onstalled
- onsubmit
- onsuspend
- ontimeupdate
- onvolumechange
- onwaiting
For a full explanation of these attributes, see HTML 5 event handler content attributes.
Differences Between HTML 4 & HTML 5
The width
, align
, char
, charoff
, and valign
attributes (which were supported in HTML 4) have been made obsolete in HTML5.
To see more detail on the two versions see HTML5 <col>
Tag and HTML4 <col>
Tag. Also check out the links to the official specifications below.
CSS Limitation
There are only four CSS properties that can be applied to column and column-group elements. They are as follows:
border
- The various border properties apply to columns only if
border-collapse
is set tocollapse
on the table element. In that case, borders set on columns and column groups are input to the conflict resolution algorithm that selects the border styles at every cell edge.background
- The background properties set the background for cells in the column, but only if both the cell and row have transparent backgrounds.
width
- Provides the minimum width for the column.
visibility
- If the
visibility
of a column is set tocollapse
, none of the cells in the column are rendered, and cells that span into other columns are clipped. In addition, the width of the table is diminished by the width the column would have taken up.
Taken from the CSS 2.1 specification
And in case you're interested, here's the reasoning behind why only four CSS properties can be applied to table columns.
Template
Here's a template for the <col>
tag with all available attributes for the tag (based on HTML5). These are grouped into attribute types, each type separated by a space. In many cases, you will probably only need one or two (if any) attributes. Simply remove the attributes you don't need.
For more information on attributes for this tag, see HTML5 <col>
Tag and HTML4 <col>
Tag.
Tag Details
For more details about the <col>
tag, see HTML5 <col>
Tag and HTML4 <col>
Tag.
Specifications
Here are the official specifications for the <col>
element.
- HTML5 Specification (W3C)
- HTML Living Standard (WHATWG)
- Current W3C Draft (the next version that is currently being worked on)
- HTML 4 (W3C)
What's the Difference?
W3C creates "snapshot" specifications that don't change once defined. So the HTML5 specification won't change once it becomes an official recommendation. WHATWG on the other hand, develops a "living standard" that is updated on a regular basis. In general, you will probably find that the HTML living standard will be more closely aligned to the current W3C draft than to the HTML5 specification.