<caption> Tag in HTML
The <caption> tag is used to specify a title or a description for a <table>
element. It is typically placed directly after the opening <table>
tag and before any <thead>
, <tbody>
, or other table sections.
Key Points on <caption> Tag:
- The
<caption>
tag provides a title or a description for a table. - It is positioned above the table content (by default).
- It is an optional tag, but when used, it can enhance the accessibility of the table.
- You can apply styles to the
<caption>
to change the appearance of the table title.
Syntax of <caption> Tag:
Syntax Example
<table>
<caption>Table Title</caption>
</table>
Example of <caption> Tag in HTML:
This example demonstrates how the <caption>
tag is used within a table to provide a title or description.
Code Example
<table border="1">
<caption>Employee Information</caption>
<tr>
<th>Name</th>
<th>Position</th>
</tr>
<tr>
<td>John Doe</td>
<td>Manager</td>
</tr>
<tr>
<td>Jane Smith</td>
<td>Developer</td>
</tr>
</table>
Output
Name | Position |
---|---|
John Doe | Manager |
Jane Smith | Developer |
The <caption>
tag is mainly used for giving context to the table, especially for accessibility purposes. It helps screen readers identify the purpose of the table, improving the overall accessibility of the webpage.