HTML <table> Tag

The HTML <table> tag is used for defining a table in an HTML document. The <table> tag contains other tags that define the structure of the table.

Tables consist of the <table> element as well as other table-related elements. These other elements are nested inside the <table> tags to determine how the table is constructed.

Syntax

The <table> tag is written as <table></table> with the various table elements nested between the start and end tags.

Each <tr> element represents a row in the table. A row can have one or more <td> or <th> elements, which determine the columns in the table. Specifically, <td> represents table data and <th> represents a table header.

Like this:

<table>
 <tr>
  <th>Table Header 1</th>
  <th>Table Header 2</th>
 </tr>
 <tr>
  <td>Cell 1</td>
  <td>Cell 2</td>
 </tr>
 <tr>
  <td>Cell 3</td>
  <td>Cell 4</td>
 </tr>
</table>

Examples

Basic tag usage

The following table includes table rows (<tr>), table headers (<th>) and table data cells (<td>).

You can use the border attribute to determine whether the table should have a border or not (however, this may not be fully supported - see below under "Differences Between HTML 4 & HTML 5" for more info). Using border="1" specifies that a border should be presented, border="0" specifies that a border should not be presented.

Also, W3C states that the border attribute is used to explicitly indicate that the table element is not being used for layout purposes.

Captions

You can use the <caption> element to provide a caption/title for your table.

Using <tbody>, <thead>, and <tfoot> Tags

You can use the <tbody>, <thead>, and <tfoot> tags to group table rows, and to add a table header and/or footer.

Here's an example:

Styles

You can use CSS to apply styles to your tables. You can apply styles against the whole table, the individual elements inside the table, or both.

In particular, you can drop the border attribute completely and use CSS to define any borders.

Here's a quick example of applying some styles to a table:

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 <table> element accepts the following attributes.

Element-Specific Attributes

This table shows the attributes that are specific to the <table> tag/element.

AttributeDescription
borderSpecifies whether the table cells should have borders or not.

Possible values:

  • [Empty String]
  • 1

Note that the WHATWG HTML Living Standard does not support the border attribute. However, the W3C HTML5 and HTML 5.1 specifications both support the border attribute.

Also, W3C states that the border attribute is used to explicitly indicate that the table element is not being used for layout purposes.

Generally, unless you have a specific reason to use the border attribute, use the CSS border property instead (or any other border-related CSS property).

sortableSpecifies that the data in the table can be sorted.

This is a boolean attribute. If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace (i.e. either sortable or sortable="sortable").

Possible values:

  • [Empty string]
  • sortable

Also see the sorted attribute of the <th> element.

Global Attributes

The following attributes are standard across all HTML5 elements. Therefore, you can use these attributes with the <table> 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 <table> 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

HTML5 does not support the following attributes, which are supported in HTML 4:

  • summary
  • width
  • frame
  • rules
  • cellspacing
  • cellpadding
  • align (deprecated in HTML 4)
  • bgcolor

Also note that the WHATWG HTML Living Standard does not support the border attribute. However, the W3C HTML5 and HTML 5.1 specifications both support the border attribute. Generally, unless you have a specific reason to use the border attribute, use the CSS border property instead (or any other border-related CSS property).

Also note that both the WHATWG HTML Living Standard and HTML 5.1 support a new attribute called sortable. This attribute enables a sorting interface for the table.

To see more detail on the two versions see HTML5 <table> Tag and HTML4 <table> Tag. Also check out the links to the official specifications below.

Template

Here's a template for the <table> 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 <table> Tag and HTML4 <table> Tag.

<table  
 border=""
 sortable*=""
 
 accesskey=""
 class=""
 contenteditable=""
 contextmenu=""
 dir=""
 draggable=""
 dropzone=""
 hidden=""
 id=""
 itemid=""
 itemprop=""
 itemref=""
 itemscope=""
 itemtype=""
 lang=""
 spellcheck=""
 style=""
 tabindex=""
 title=""
 translate=""

 onabort=""
 onautocomplete=""
 onautocompleteerror=""
 onblur=""
 oncancel=""
 oncanplay=""
 oncanplaythrough=""
 onchange=""
 onclick=""
 onclose=""
 oncontextmenu=""
 oncuechange=""
 ondblclick=""
 ondrag=""
 ondragend=""
 ondragenter=""
 ondragexit=""
 ondragleave=""
 ondragover=""
 ondragstart=""
 ondrop=""
 ondurationchange=""
 onemptied=""
 onended=""
 onerror=""
 onfocus=""
 oninput=""
 oninvalid=""
 onkeydown=""
 onkeypress=""
 onkeyup=""
 onload=""
 onloadeddata=""
 onloadedmetadata=""
 onloadstart=""
 onmousedown=""
 onmouseenter=""
 onmouseleave=""
 onmousemove=""
 onmouseout=""
 onmouseover=""
 onmouseup=""
 onmousewheel=""
 onpause=""
 onplay=""
 onplaying=""
 onprogress=""
 onratechange=""
 onreset=""
 onresize=""
 onscroll=""
 onseeked=""
 onseeking=""
 onselect=""
 onshow=""
 onsort=""
 onstalled=""
 onsubmit=""
 onsuspend=""
 ontimeupdate=""
 ontoggle=""
 onvolumechange=""
 onwaiting=""
	>
</table>

* The sortable attribute is supported by the WHATWG HTML Living Standard and HTML 5.1 (but not HTML 5.0).

Tag Details

For more details about the <table> tag, see HTML5 <table> Tag and HTML4 <table> Tag.

Specifications

Here are the official specifications for the <table> element.

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.