totn HTML

HTML: <table> tag

This HTML tutorial explains how to use the HTML element called the <table> tag with syntax and examples.

Description

The HTML <table> tag defines a table in the HTML document. This tag is also commonly referred to as the <table> element.

Syntax

In HTML, the syntax for the <table> tag is: (example of a table with 2 columns and 3 rows)

<body>
<table>
  <tr>
    <th>Item</th>
    <th>Amount</th>
  </tr>
  <tr>
    <td>Bananas</td>
    <td>$5</td>
  </tr>
  <tr>
    <td>Oranges</td>
    <td>$17</td>
  </tr>
</table>
</body>

Sample Output


Attributes

In addition to the Global Attributes, the following is a list of attributes that are specific to the <table> tag:

Attribute Description HTML Compatibility
align Alignment of the table. It can be one of the following values: left, center, right Deprecated, use CSS
bgcolor Background color of the table Deprecated, use CSS
border Size of the frame surrounding table (in pixels) Deprecated, use CSS
cellpadding Space between the content of a cell and the border (in pixels) Deprecated, use CSS
cellspacing Size of the space between cells (in pixels) Deprecated, use CSS
frame Side of the table frame is displayed. It can be one of the following values: above, hsides, lhs, border, void, below, vsides, rhs, box Deprecated, use CSS
rules Lines that should be displayed. It can be one of the following values: none, groups, rows, columns, all Deprecated, use CSS
summary Alternative text displayed when table can not be displayed Deprecated, use CSS
width Width of the table Deprecated, use CSS

Note

  • The HTML <table> element is found within the <body> tag.
  • The <table> tag is made up of <tr>, <th>, and <td> tags.
  • The <tr> tag defines the table rows. There must be at least one row in the table.
  • The <th> tag defines the header cells in the table which are displayed as bold, center-aligned text.
  • The <td> tag defines the standard cells in the table which are displayed as normal-weight, left-aligned text.

Browser Compatibility

The <table> tag is compatible with the following browsers:

  • Chrome
  • Android
  • Firefox (Gecko)
  • Firefox Mobile (Gecko)
  • Internet Explorer (IE)
  • Edge Mobile
  • Opera
  • Opera Mobile
  • Safari (WebKit)
  • Safari Mobile

Example

We will discuss the <table> tag below, exploring examples of how to use the <table> tag in HTML5, HTML 4.01 Transitional, XHTML 1.0 Transitional, XHTML 1.0 Strict, and XHTML 1.1.