Problem Statement
Which tag is used to add a caption or title to a table?
Explanation
The caption tag is used to add a title or caption to a table. The caption must be placed immediately after the opening table tag, before any tr tags. It provides a brief description of the table contents. By default, the caption appears centered above the table. You can change its position using CSS. The caption tag is important for accessibility because it helps screen readers announce what the table is about before reading the data. It also improves the user experience by giving context to the table. Good captions are concise and descriptive, explaining what data the table contains. Using captions is considered a best practice for creating accessible and user-friendly tables.
Code Solution
SolutionRead Only
<!-- Table with caption -->
<table>
<caption>Monthly Sales Report</caption>
<tr>
<th>Month</th>
<th>Sales</th>
</tr>
<tr>
<td>January</td>
<td>$5,000</td>
</tr>
<tr>
<td>February</td>
<td>$6,200</td>
</tr>
</table>
<!-- Caption with styling -->
<table>
<caption style="font-weight: bold; font-size: 1.2em;">Employee Directory</caption>
<tr>
<th>Name</th>
<th>Department</th>
</tr>
<tr>
<td>John Doe</td>
<td>Engineering</td>
</tr>
</table>