: The Table Caption element - HTML: HyperText Markup Language | MDN (original) (raw)
Baseline Widely available
The <caption>
HTML element specifies the caption (or title) of a table, providing the table an accessible description.
Try it
Attributes
This element includes the global attributes.
Deprecated attributes
The following attributes are deprecated and should not be used. They are documented below for reference when updating existing code and for historical interest only.
align Deprecated
Specifies on which side of the table the caption should be displayed. The possible enumerated values are left
, top
, right
, or bottom
. Use the caption-side and text-align CSS properties instead, as this attribute is deprecated.
Usage notes
- If included, the
<caption>
element must be the first child of its parentelement.
- When a
<table>
is nested within a as the figure's only content, it should be captioned via afor the <figure>
instead of as a<caption>
nested within the<table>
. - Any background-color applied to a table will not be applied to its caption. Add a
background-color
to the<caption>
element as well if you want the same color to be behind both.
Example
Table with caption
This example demonstrates a basic table that includes a caption describing the data presented.
Such a "title" is helpful for users who are quickly scanning the page, and it is especially beneficial for visually impaired users, allowing them to determine the table's relevance quickly without the need to have a screen reader read the contents of many cells just to find out what the table is about.
HTML
A <caption>
element is used as the first child of the
<caption>
.
<table>
<caption>
User login email addresses
</caption>
<tr>
<th>Login</th>
<th>Email</th>
</tr>
<tr>
<td>user1</td>
<td>user1@example.com</td>
</tr>
<tr>
<td>user2</td>
<td>user2@example.com</td>
</tr>
</table>
CSS
Some basic CSS is used to align and highlight the <caption>
.
caption {
caption-side: top;
text-align: left;
padding-bottom: 10px;
font-weight: bold;
}
table {
border-collapse: collapse;
border: 2px solid rgb(140 140 140);
font-family: sans-serif;
font-size: 0.8rem;
letter-spacing: 1px;
}
th,
td {
border: 1px solid rgb(160 160 160);
padding: 8px 10px;
}
th {
background-color: rgb(230 230 230);
}
td {
text-align: center;
}
Result
Technical summary
Content categories | None. |
---|---|
Permitted content | Flow content. |
Tag omission | The end tag can be omitted if the element is not immediately followed by ASCII whitespace or a comment. |
Permitted parents | A |
Implicit ARIA role | caption |
Permitted ARIA roles | No role permitted |
DOM interface | HTMLTableCaptionElement |
Specifications
Specification |
---|
HTML Standard # the-caption-element |
Browser compatibility
BCD tables only load in the browser
See also
- Learn: HTML tables
, , , ,
, , , , : Other table-related elements - caption-side: CSS property to position the
<caption>
relative to its parent - text-align: CSS property to horizontally align the text content of the
<caption>