HTML <table> Tag
<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.
<table> tag is written as
</table> with the various table elements nested between the start and end tags.
<tr> element represents a row in the table. A row can have one or more
<th> elements, which determine the columns in the table. Specifically,
<td> represents table data and
<th> represents a table header.
Basic tag usage
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.
You can use the
<caption> element to provide a caption/title for your table.
Here's an example:
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 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.
<table> element accepts the following attributes.
This table shows the attributes that are specific to the
|border||Specifies whether the table cells should have borders or not.
Note that the WHATWG HTML Living Standard does not support the
Also, W3C states that the
Generally, unless you have a specific reason to use the
|sortable||Specifies 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
Also see the
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.
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.
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:
align(deprecated in HTML 4)
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.
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.
sortable attribute is supported by the WHATWG HTML Living Standard and HTML 5.1 (but not HTML 5.0).
Here are the official specifications for the
- 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.