Table of Contents
INTRODUCTION
The <caption> element in HTML is used to add a title or description to a table. It helps provide context about the table's data, improving accessibility and clarity.
When <caption> element is used it should be the first child of table. This tag is placed immediately after the <table> tag.
Syntax:
<table>
<caption> - TEXT - </caption>
</table>
<table>
<caption>Monthly Sales Report</caption>
<tr>
<th>Month</th>
<th>Sales ($)</th>
</tr>
<tr>
<td>January</td>
<td>10,000</td>
</tr>
<tr>
<td>February</td>
<td>12,000</td>
</tr>
</table>
Month | Sales ($) |
---|---|
January | 10,000 |
February | 12,000 |
Usage Guidelines
- The <caption> element should be the first child of table. and is placed immediately after the <table> tag.
- When a <table> is the sole content inside a <figure> element, the caption should be provided using a <figcaption> for the <figure>, rather than using a <caption> inside the <table>
- Applying a background color to a table does not affect its caption. To maintain consistent styling, you need to apply the background-color separately to the caption element.
ATTRIBUTES
The <caption> element only has global attributes.
Tag Omission
The HTML <caption> element end tag can be omitted if the element is not immediately followed by ASCII whitespace or a comment.