totn HTML

HTML: <caption> tag

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

Description

The HTML <caption> tag defines the title of a table in the HTML document. Browsers traditionally render the text found in the <caption> tag above the table, but you can change this behavior with the CSS caption-side property. This tag is also commonly referred to as the <caption> element.

Syntax

In HTML, the syntax for the <caption> tag is:

<body>
<table>
  <caption>Cost of Fruit</caption>
  <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 <caption> tag:

Attribute Description HTML Compatibility
align Enumerated attribute indicating how the caption is aligned in the table. It can be one of the following values:

left - means the caption appears to the left of the table
top - means the caption appears above the table
right - means the caption appears to the right of the table
bottom - means the caption appears below the table
Deprecated in HTML 4.01,
Obsolete in HTML5
(Use CSS properties such as text-align and caption-side instead)

Note

  • The HTML <caption> element is found within the <body> tag.
  • The <caption> tag is the first tag to appear after the <table> 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.
  • Most browsers will render the <caption> tag above the table but you can change this behavior with the CSS caption-side property.

Browser Compatibility

The <caption> 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 <caption> tag below, exploring examples of how to use the <caption> tag in HTML5, HTML 4.01 Transitional, XHTML 1.0 Transitional, XHTML 1.0 Strict, and XHTML 1.1.