<caption>
The HTML <caption> element serves as a title or description for a table, appearing as the first child of the <table> element to enhance clarity and accessibility.
<caption>
Tag
The HTML <caption>
element defines the title for a table. It must always be the first child of a <table>
. Although it is positioned above the table by default, CSS can be used to style and reposition it as needed.
Syntax
index.html
<table>
<caption>Caption Text</caption>
<!-- Table content -->
</table>
<caption>
Demo
Month | Sales |
---|---|
January | $5000 |
February | $6000 |
index.html
<table>
<caption>Monthly Sales Report</caption>
<thead>
<tr>
<th>Month</th>
<th>Sales</th>
</tr>
</thead>
<tbody>
<tr>
<td>January</td>
<td>$5000</td>
</tr>
<tr>
<td>February</td>
<td>$6000</td>
</tr>
</tbody>
</table>
Definition and Usage
The <caption>
tag is used to provide a descriptive title for a table. It should always be placed immediately after the <table>
tag.
By default, a table caption is center-aligned above the table. However, you can use the CSS properties
text-align
and caption-side
to modify its alignment and position.See Also
Related Table Elements
<caption>
CSS Properties for Styling
text-align
caption-side
Conclusion
The <caption>
tag in HTML provides a title for a table, helping users understand the content it holds. It must be the first child within the <table>
element and is by default center-aligned above the table. With CSS, its alignment and position can be customized.