<caption>:表格标题元素
Baseline 广泛可用 *
试一试
<table>
<caption>
He-Man and Skeletor facts
</caption>
<tr>
<td></td>
<th scope="col" class="heman">He-Man</th>
<th scope="col" class="skeletor">Skeletor</th>
</tr>
<tr>
<th scope="row">Role</th>
<td>Hero</td>
<td>Villain</td>
</tr>
<tr>
<th scope="row">Weapon</th>
<td>Power Sword</td>
<td>Havoc Staff</td>
</tr>
<tr>
<th scope="row">Dark secret</th>
<td>Expert florist</td>
<td>Cries at romcoms</td>
</tr>
</table>
caption {
caption-side: bottom;
padding: 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;
}
tr:nth-child(even) td {
background-color: rgb(250 250 250);
}
tr:nth-child(odd) td {
background-color: rgb(240 240 240);
}
.heman {
font:
1.4rem "molot",
sans-serif;
text-shadow:
1px 1px 1px white,
2px 2px 1px black;
}
.skeletor {
font:
1.7rem "rapscallion",
fantasy;
letter-spacing: 3px;
text-shadow:
1px 1px 0 white,
0 0 9px black;
}
属性
此元素包含全局属性。
已弃用属性
以下属性已弃用,不应使用。它们仅在更新现有代码时作为参考和出于历史兴趣而在此处记录。
align
已弃用-
指定标题应显示在表格的哪一侧。可能的 枚举 值是
left
、top
、right
或bottom
。请使用caption-side
和text-align
CSS 属性,因为此属性已弃用。
用法说明
- 如果包含
<caption>
元素,它必须是其父级<table>
元素的第一个子元素。 - 当
<table>
嵌套在<figure>
中,并且是 figure 的唯一内容时,应通过<figure>
的<figcaption>
来为其添加标题,而不是在<table>
中嵌套<caption>
。 - 应用于表格的任何
background-color
都不会应用于其标题。如果您希望表格和标题具有相同的背景色,也请为<caption>
元素添加background-color
。
示例
请参阅 <table>
以获取一个完整的表格示例,其中介绍了常见的标准和最佳实践。
带标题的表格
此示例演示了一个包含标题以描述所呈现数据的基本表格。
这样的“标题”对于快速浏览页面的用户很有帮助,并且对视障用户尤其有利,它允许他们在不要求屏幕阅读器朗读大量单元格内容的情况下,快速确定表格的相关性,从而了解表格的内容。
HTML
<caption>
元素用作 <table>
的第一个子元素,其文本内容类似于标题,用于描述表格数据。在 <caption>
之后,使用 <tr>
、<th>
和 <td>
元素创建了三行,第一行为标题行,共有两列。
html
<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
使用一些基本 CSS 来对 <caption>
进行对齐和高亮显示。
css
caption {
caption-side: top;
text-align: left;
padding-bottom: 10px;
font-weight: bold;
}
结果
技术摘要
内容类别 | 无。 |
---|---|
允许内容 | 流内容. |
标签省略 | 如果元素后面紧跟着的不是 ASCII 空白符或注释,则可以省略结束标签。 |
允许父级 | 作为其第一个后代元素的 <table> 元素。 |
隐式 ARIA 角色 |
caption
|
允许的 ARIA 角色 | 不允许 role |
DOM 接口 | HTMLTableCaptionElement |
规范
规范 |
---|
HTML # the-caption-element |
浏览器兼容性
加载中…
另见
- 学习:HTML 表格基础
<col>
,<colgroup>
,<table>
,<tbody>
,<td>
,<tfoot>
,<th>
,<thead>
,<tr>
: 其他表格相关元素caption-side
:用于将<caption>
相对于其父级<table>
定位的 CSS 属性text-align
:用于水平对齐<caption>
文本内容的 CSS 属性