Table Tag in HTML5

Introduction 

 
A simple HTML table consists of the table element. A table is divided into rows and each row is divided into data cells. The tr element defines a table row, the th element defines a table header, and the td element defines a table cell. A <td> tag can contain text, links, images, lists, forms, other tables, etc.
 
For Example
  1. <table border="1">  
  2.     <tr>  
  3.         <td>row 1, cell 1</td>  
  4.         <td>row 1, cell 2</td>  
  5.     </tr>  
  6.     <tr>  
  7.         <td>row 2, cell 1</td>  
  8.         <td>row 2, cell 2</td>  
  9.     </tr>  
  10.     <tr>  
  11.         <td>row 3, cell 1</td>  
  12.         <td>row 3, cell 2</td>  
  13.     </tr>  
  14. </table>  

Attributes

 
HTML tags can contain one or more attributes. Attributes are added to a tag to provide the browser with more information about how the tag should appear or behave. Attributes consist of a name and a value separated by an equals (=) sign, with the value surrounded by double-quotes.
 
There are 3 kinds of attributes that you can add to your HTML tags: Element-specific, global, and event handler content attributes.
 

Element-Specific Attributes

 
The following table shows the attributes that are specific to this tag/element.
 
Attributes Introduced by HTML5
 
Attributes Description             
none  
 

Global Attributes

 
The following attributes are standard across all HTML 5 tags.
 
HTML5Global Attributes
accesskey draggable style
class hidden tabindex
dir spellcheck  
contenteditable id title
contextmenu lang  

 

Event Handler Content Attributes

 
Here are the standard HTML 5 event handler content attributes.
 
onabort onerror* onmousewheel
onblur* onfocus* onpause
oncanplay onformchange onplay
oncanplaythrough onforminput onplaying
onchange oninput onprogress
onclick oninvalid onratechange
oncontextmenu onkeydown onreadystatechange
ondblclick onkeypress onscroll
ondrag onkeyup onseeked
ondragend onload* onseeking
ondragenter onloadeddata onselect
ondragleave onloadedmetadata onshow
ondragover onloadstart onstalled
ondragstart onmousedown onsubmit
ondrop onmousemove onsuspend
ondurationchange onmouseout ontimeupdate
onemptied onmouseover onvolumechange
onended onmouseup onwaiting
 
An HTML table may also include the below tag.
  1. HTML <caption> Tag
  2. HTML <thead> Tag
  3. HTML <tfoot> Tag
  4. HTML<tbody> Tag
  5. HTML <colgroup> Tag
  6. HTML <tr> Tag
  7. HTML <td> Tag
  8. HTML <th> Tag

1. <caption> Tag

  • The HTML <caption> tag is used forcreating table captions.
  • It is used in conjunction with the <table>tag and represents the title of the table.
  • The <caption> tag must be insertedimmediately after the <table> tag.
  • A table should have no more than onecaption.
  1. <table border="1">  
  2.     <caption>  
  3.         <strong>Table caption</strong>  
  4.     </caption>  
  5.     <tr>  
  6.         <td>row 1, cell 1</td>  
  7.         <td>row 1, cell 2</td>  
  8.     </tr>  
  9.     <tr>  
  10.         <td>row 2, cell 1</td>  
  11.         <td>row 2, cell 2</td>  
  12.     </tr>  
  13.     <tr>  
  14.         <td>row 3, cell 1</td>  
  15.         <td>row 3, cell 2</td>  
  16.     </tr>  
  17. </table>  
Internet Explorer
 
table-caption2.gif
  

HTML <thead> Tag, HTML <tfoot> Tag  and HTML<tbody> Tag

 

HTML <thead> Tag

 
The HTML <tfoot> tag is used for adding a header to a table. The <thead> tag is used in conjunction with the <tbody> tag and the <tfoot> tag in determining each part of the table (header, footer, body).
 

HTML <tfoot> Tag

 
The HTML <tfoot> tag is used for adding a footer to a table. The <tfoot> tag is used in conjunction with the <thead> tag and the <tbody> tag in determining each part of the table (header, footer, body).
 

HTML<tbody> Tag

 
The HTML <tbody> tag is used for grouping table rows. The <tbody> tag is used in conjunction with the <thead> tag and the <tfoot> tag in determining each part of the table (header, footer, body).
 
For Example
 
The below example defines the <thead>, <tfoot> and <tbody> tag.
  1. <table border = "1">  
  2.     <thead>  
  3.         <TR>  
  4.             <td >Table Header (thead)</td>  
  5.         </tr>  
  6.     </thead>  
  7.     <tfoot>  
  8.         <TR>  
  9.             <td>Table Footer (tfoot)</td>  
  10.         </tr>  
  11.     </tfoot>  
  12.     <tbody>  
  13.         <tr>  
  14.             <td>Cell 1 -  tbody</td>  
  15.             <td>Cell 2 -  tbody</td>  
  16.         </tr>  
  17.         <tr>  
  18.             <td>Cell 3 -  tbody</td>  
  19.             <td>Cell 4 -  tbody</td>  
  20.         </tr>  
  21.         <tr>  
  22.             <td>Cell 5 -  tbody</td>  
  23.             <td>Cell 6 -  tbody</td>  
  24.         </tr>  
  25.     </tbody>  
  26. </table>  
Internet Explorer
 
table-caption3.gif
  

HTML <colgroup> Tag

 
The HTML <colgroup> tag is used for specifying properties for a group of columns within a table.
  1. <table border="1">  
  2.     <colgroup span="3" style="background-color:orange"></colgroup>  
  3.     <tr>  
  4.         <td>column 1</td>  
  5.         <td>column 2</td>  
  6.         <td>column 3</td>  
  7.         <td>column 4</td>  
  8.     </tr>  
  9.     <tr>  
  10.         <td>column 1</td>  
  11.         <td>column 2</td>  
  12.         <td>column 3</td>  
  13.     </tr>  
  14.     <tr>  
  15.         <td>column 1</td>  
  16.         <td>column 2</td>  
  17.     </tr>  
  18. </table>  
Internet Explorer
 
table-caption4.gif