表格
用细线分隔数据行,支持可排序表头和数值对齐。
本页内容
表格用细线分隔数据行。列标题用低调的小字,配较醒目的分隔线,数据行则承载主要内容。在 <table> 上使用 table,在每个 <tr> 上使用 table-row,列标题使用 table-heading,数据单元格和行标题使用 table-cell。表格本身没有表面材质,请将它放在凸起面板或卡片中。
表格
<div class="sheet overflow-hidden"> <table class="table"> <thead> <tr class="table-row"> <th class="table-heading" scope="col">Project</th> <th class="table-heading" scope="col">Status</th> <th class="table-heading" scope="col">Region</th> <th class="table-heading table-numeric" scope="col">Requests</th> </tr> </thead> <tbody> <tr class="table-row"> <th class="table-cell table-primary" scope="row">Atlas</th> <td class="table-cell"><span class="badge badge-success badge-dot">Ready</span></td> <td class="table-cell">Tokyo</td> <td class="table-cell table-numeric">1,284,019</td> </tr> <tr class="table-row"> <th class="table-cell table-primary" scope="row">Borealis</th> <td class="table-cell"><span class="badge badge-warning badge-dot">Degraded</span></td> <td class="table-cell">Frankfurt</td> <td class="table-cell table-numeric">94,201</td> </tr> <tr class="table-row"> <th class="table-cell table-primary" scope="row">Cirrus</th> <td class="table-cell"><span class="badge badge-dot">Draft</span></td> <td class="table-cell">Virginia</td> <td class="table-cell table-numeric">0</td> </tr> </tbody> </table></div><div className="sheet overflow-hidden"> <table className="table"> <thead> <tr className="table-row"> <th className="table-heading" scope="col">Project</th> <th className="table-heading" scope="col">Status</th> <th className="table-heading" scope="col">Region</th> <th className="table-heading table-numeric" scope="col">Requests</th> </tr> </thead> <tbody> <tr className="table-row"> <th className="table-cell table-primary" scope="row">Atlas</th> <td className="table-cell"><span className="badge badge-success badge-dot">Ready</span></td> <td className="table-cell">Tokyo</td> <td className="table-cell table-numeric">1,284,019</td> </tr> <tr className="table-row"> <th className="table-cell table-primary" scope="row">Borealis</th> <td className="table-cell"> <span className="badge badge-warning badge-dot">Degraded</span> </td> <td className="table-cell">Frankfurt</td> <td className="table-cell table-numeric">94,201</td> </tr> <tr className="table-row"> <th className="table-cell table-primary" scope="row">Cirrus</th> <td className="table-cell"><span className="badge badge-dot">Draft</span></td> <td className="table-cell">Virginia</td> <td className="table-cell table-numeric">0</td> </tr> </tbody> </table></div><div class="sheet overflow-hidden"> <table class="table"> <thead> <tr class="table-row"> <th class="table-heading" scope="col">Project</th> <th class="table-heading" scope="col">Status</th> <th class="table-heading" scope="col">Region</th> <th class="table-heading table-numeric" scope="col">Requests</th> </tr> </thead> <tbody> <tr class="table-row"> <th class="table-cell table-primary" scope="row">Atlas</th> <td class="table-cell"><span class="badge badge-success badge-dot">Ready</span></td> <td class="table-cell">Tokyo</td> <td class="table-cell table-numeric">1,284,019</td> </tr> <tr class="table-row"> <th class="table-cell table-primary" scope="row">Borealis</th> <td class="table-cell"><span class="badge badge-warning badge-dot">Degraded</span></td> <td class="table-cell">Frankfurt</td> <td class="table-cell table-numeric">94,201</td> </tr> <tr class="table-row"> <th class="table-cell table-primary" scope="row">Cirrus</th> <td class="table-cell"><span class="badge badge-dot">Draft</span></td> <td class="table-cell">Virginia</td> <td class="table-cell table-numeric">0</td> </tr> </tbody> </table></div><template> <div class="sheet overflow-hidden"> <table class="table"> <thead> <tr class="table-row"> <th class="table-heading" scope="col">Project</th> <th class="table-heading" scope="col">Status</th> <th class="table-heading" scope="col">Region</th> <th class="table-heading table-numeric" scope="col">Requests</th> </tr> </thead> <tbody> <tr class="table-row"> <th class="table-cell table-primary" scope="row">Atlas</th> <td class="table-cell"><span class="badge badge-success badge-dot">Ready</span></td> <td class="table-cell">Tokyo</td> <td class="table-cell table-numeric">1,284,019</td> </tr> <tr class="table-row"> <th class="table-cell table-primary" scope="row">Borealis</th> <td class="table-cell"><span class="badge badge-warning badge-dot">Degraded</span></td> <td class="table-cell">Frankfurt</td> <td class="table-cell table-numeric">94,201</td> </tr> <tr class="table-row"> <th class="table-cell table-primary" scope="row">Cirrus</th> <td class="table-cell"><span class="badge badge-dot">Draft</span></td> <td class="table-cell">Virginia</td> <td class="table-cell table-numeric">0</td> </tr> </tbody> </table> </div></template>#单元格
单元格用作行标题时使用 <th class="table-cell" scope="row">,用作行组标题时使用 scope="rowgroup"。这两种标题均沿起始边对齐并使用 --font-weight-control;在用角色构建的网格中,带有 role="rowheader" 的 table-cell 也采用相同样式。普通单元格沿用周围文本的字重,浏览器为 <th> 默认设置的居中和粗体样式不会打乱表格的排版。
用 ARIA 角色构建表格时,同一组类名会设置表格、行和单元格的显示方式,独立版 CSS 也支持这一用法。
table-primary标记用于标识该行的单元格,并为它应用强调墨色。其余单元格沿用正文墨色,让行名称更容易找到。- 在列标题及其对应的单元格上使用
table-numeric,可将它们沿末端边缘对齐,并使用等宽数字。这样各数位上下对齐,便于按位数比较金额。
#排序
带有 aria-sort="ascending" 或 aria-sort="descending" 的列标题会使用强调墨色,并显示指向排序方向的尖角符号。在列标题内放置按钮来切换排序。样式表读取该属性,由脚本或服务器设置属性值。
排序
<div class="sheet overflow-hidden"> <table class="table table-compact"> <thead> <tr class="table-row"> <th class="table-heading" scope="col" aria-sort="ascending"> <button type="button" class="table-sort">Name</button> </th> <th class="table-heading" scope="col">Role</th> <th class="table-heading table-numeric" scope="col"> <button type="button" class="table-sort">Deployments</button> </th> </tr> </thead> <tbody> <tr class="table-row"> <td class="table-cell table-primary">Ada Lovelace</td> <td class="table-cell">Owner</td> <td class="table-cell table-numeric">412</td> </tr> <tr class="table-row"> <td class="table-cell table-primary">Alan Turing</td> <td class="table-cell">Admin</td> <td class="table-cell table-numeric">96</td> </tr> <tr class="table-row"> <td class="table-cell table-primary">Grace Hopper</td> <td class="table-cell">Member</td> <td class="table-cell table-numeric">1,204</td> </tr> </tbody> </table></div><div className="sheet overflow-hidden"> <table className="table table-compact"> <thead> <tr className="table-row"> <th className="table-heading" scope="col" aria-sort="ascending"> <button type="button" className="table-sort">Name</button> </th> <th className="table-heading" scope="col">Role</th> <th className="table-heading table-numeric" scope="col"> <button type="button" className="table-sort">Deployments</button> </th> </tr> </thead> <tbody> <tr className="table-row"> <td className="table-cell table-primary">Ada Lovelace</td> <td className="table-cell">Owner</td> <td className="table-cell table-numeric">412</td> </tr> <tr className="table-row"> <td className="table-cell table-primary">Alan Turing</td> <td className="table-cell">Admin</td> <td className="table-cell table-numeric">96</td> </tr> <tr className="table-row"> <td className="table-cell table-primary">Grace Hopper</td> <td className="table-cell">Member</td> <td className="table-cell table-numeric">1,204</td> </tr> </tbody> </table></div><div class="sheet overflow-hidden"> <table class="table table-compact"> <thead> <tr class="table-row"> <th class="table-heading" scope="col" aria-sort="ascending"> <button type="button" class="table-sort">Name</button> </th> <th class="table-heading" scope="col">Role</th> <th class="table-heading table-numeric" scope="col"> <button type="button" class="table-sort">Deployments</button> </th> </tr> </thead> <tbody> <tr class="table-row"> <td class="table-cell table-primary">Ada Lovelace</td> <td class="table-cell">Owner</td> <td class="table-cell table-numeric">412</td> </tr> <tr class="table-row"> <td class="table-cell table-primary">Alan Turing</td> <td class="table-cell">Admin</td> <td class="table-cell table-numeric">96</td> </tr> <tr class="table-row"> <td class="table-cell table-primary">Grace Hopper</td> <td class="table-cell">Member</td> <td class="table-cell table-numeric">1,204</td> </tr> </tbody> </table></div><template> <div class="sheet overflow-hidden"> <table class="table table-compact"> <thead> <tr class="table-row"> <th class="table-heading" scope="col" aria-sort="ascending"> <button type="button" class="table-sort">Name</button> </th> <th class="table-heading" scope="col">Role</th> <th class="table-heading table-numeric" scope="col"> <button type="button" class="table-sort">Deployments</button> </th> </tr> </thead> <tbody> <tr class="table-row"> <td class="table-cell table-primary">Ada Lovelace</td> <td class="table-cell">Owner</td> <td class="table-cell table-numeric">412</td> </tr> <tr class="table-row"> <td class="table-cell table-primary">Alan Turing</td> <td class="table-cell">Admin</td> <td class="table-cell table-numeric">96</td> </tr> <tr class="table-row"> <td class="table-cell table-primary">Grace Hopper</td> <td class="table-cell">Member</td> <td class="table-cell table-numeric">1,204</td> </tr> </tbody> </table> </div></template>table-compact 缩小行高,适合需要快速扫读的长列表。
#选择与可打开的行
带有 aria-selected="true" 的行会呈现为浮起一层的面板。在表格上使用 table-interactive,表体中的所有行都会像菜单项一样,在指针悬停时立即以胶囊标签色调提供反馈。两者都统一绘制在整行上,因此相邻单元格之间不会出现接缝。
选择
<div class="sheet overflow-hidden"> <table class="table table-interactive"> <thead> <tr class="table-row"> <th class="table-heading" scope="col"> <input type="checkbox" class="checkbox" aria-label="Select all"> </th> <th class="table-heading" scope="col">Domain</th> <th class="table-heading" scope="col">Certificate</th> </tr> </thead> <tbody> <tr class="table-row" aria-selected="true"> <td class="table-cell"> <input type="checkbox" class="checkbox" checked aria-label="Select atlas.dev"> </td> <td class="table-cell table-primary">atlas.dev</td> <td class="table-cell">Renews in 61 days</td> </tr> <tr class="table-row"> <td class="table-cell"> <input type="checkbox" class="checkbox" aria-label="Select docs.atlas.dev"> </td> <td class="table-cell table-primary">docs.atlas.dev</td> <td class="table-cell">Renews in 12 days</td> </tr> </tbody> </table></div><div className="sheet overflow-hidden"> <table className="table table-interactive"> <thead> <tr className="table-row"> <th className="table-heading" scope="col"> <input type="checkbox" className="checkbox" aria-label="Select all" /> </th> <th className="table-heading" scope="col">Domain</th> <th className="table-heading" scope="col">Certificate</th> </tr> </thead> <tbody> <tr className="table-row" aria-selected="true"> <td className="table-cell"> <input type="checkbox" className="checkbox" defaultChecked aria-label="Select atlas.dev" /> </td> <td className="table-cell table-primary">atlas.dev</td> <td className="table-cell">Renews in 61 days</td> </tr> <tr className="table-row"> <td className="table-cell"> <input type="checkbox" className="checkbox" aria-label="Select docs.atlas.dev" /> </td> <td className="table-cell table-primary">docs.atlas.dev</td> <td className="table-cell">Renews in 12 days</td> </tr> </tbody> </table></div><div class="sheet overflow-hidden"> <table class="table table-interactive"> <thead> <tr class="table-row"> <th class="table-heading" scope="col"> <input type="checkbox" class="checkbox" aria-label="Select all"> </th> <th class="table-heading" scope="col">Domain</th> <th class="table-heading" scope="col">Certificate</th> </tr> </thead> <tbody> <tr class="table-row" aria-selected="true"> <td class="table-cell"> <input type="checkbox" class="checkbox" checked aria-label="Select atlas.dev"> </td> <td class="table-cell table-primary">atlas.dev</td> <td class="table-cell">Renews in 61 days</td> </tr> <tr class="table-row"> <td class="table-cell"> <input type="checkbox" class="checkbox" aria-label="Select docs.atlas.dev"> </td> <td class="table-cell table-primary">docs.atlas.dev</td> <td class="table-cell">Renews in 12 days</td> </tr> </tbody> </table></div><template> <div class="sheet overflow-hidden"> <table class="table table-interactive"> <thead> <tr class="table-row"> <th class="table-heading" scope="col"> <input type="checkbox" class="checkbox" aria-label="Select all"> </th> <th class="table-heading" scope="col">Domain</th> <th class="table-heading" scope="col">Certificate</th> </tr> </thead> <tbody> <tr class="table-row" aria-selected="true"> <td class="table-cell"> <input type="checkbox" class="checkbox" checked aria-label="Select atlas.dev"> </td> <td class="table-cell table-primary">atlas.dev</td> <td class="table-cell">Renews in 61 days</td> </tr> <tr class="table-row"> <td class="table-cell"> <input type="checkbox" class="checkbox" aria-label="Select docs.atlas.dev"> </td> <td class="table-cell table-primary">docs.atlas.dev</td> <td class="table-cell">Renews in 12 days</td> </tr> </tbody> </table> </div></template>#宽表格
表格保留原有列结构,不会重排为卡片,因为重排会让人难以跨行比较同一列的数据。表格可能超出所在栏宽时,用可沿行内轴滚动的滚动区域包裹表格。仍有列延伸到视野之外的一侧会渐隐。table-sticky 让列标题在数据行从下方滚过时保持可见。
宽表格
<div class="sheet overflow-hidden"> <div class="scroll-area scroll-area-inline" tabindex="0" role="region" aria-label="Deployments"> <table class="table"> <thead> <tr class="table-row"> <th class="table-heading" scope="col">Deployment</th> <th class="table-heading" scope="col">Branch</th> <th class="table-heading" scope="col">Commit</th> <th class="table-heading" scope="col">Author</th> <th class="table-heading table-numeric" scope="col">Duration</th> </tr> </thead> <tbody> <tr class="table-row"> <td class="table-cell table-primary whitespace-nowrap">atlas-9f2c1a</td> <td class="table-cell whitespace-nowrap">main</td> <td class="table-cell whitespace-nowrap">Soften every edge</td> <td class="table-cell whitespace-nowrap">Ada Lovelace</td> <td class="table-cell table-numeric">48s</td> </tr> <tr class="table-row"> <td class="table-cell table-primary whitespace-nowrap">atlas-71be0d</td> <td class="table-cell whitespace-nowrap">feature/anchors</td> <td class="table-cell whitespace-nowrap">Glide the segmented thumb</td> <td class="table-cell whitespace-nowrap">Alan Turing</td> <td class="table-cell table-numeric">1m 02s</td> </tr> </tbody> </table> </div></div><div className="sheet overflow-hidden"> <div className="scroll-area scroll-area-inline" tabIndex={0} role="region" aria-label="Deployments"> <table className="table"> <thead> <tr className="table-row"> <th className="table-heading" scope="col">Deployment</th> <th className="table-heading" scope="col">Branch</th> <th className="table-heading" scope="col">Commit</th> <th className="table-heading" scope="col">Author</th> <th className="table-heading table-numeric" scope="col">Duration</th> </tr> </thead> <tbody> <tr className="table-row"> <td className="table-cell table-primary whitespace-nowrap">atlas-9f2c1a</td> <td className="table-cell whitespace-nowrap">main</td> <td className="table-cell whitespace-nowrap">Soften every edge</td> <td className="table-cell whitespace-nowrap">Ada Lovelace</td> <td className="table-cell table-numeric">48s</td> </tr> <tr className="table-row"> <td className="table-cell table-primary whitespace-nowrap">atlas-71be0d</td> <td className="table-cell whitespace-nowrap">feature/anchors</td> <td className="table-cell whitespace-nowrap">Glide the segmented thumb</td> <td className="table-cell whitespace-nowrap">Alan Turing</td> <td className="table-cell table-numeric">1m 02s</td> </tr> </tbody> </table> </div></div><div class="sheet overflow-hidden"> <div class="scroll-area scroll-area-inline" tabindex="0" role="region" aria-label="Deployments"> <table class="table"> <thead> <tr class="table-row"> <th class="table-heading" scope="col">Deployment</th> <th class="table-heading" scope="col">Branch</th> <th class="table-heading" scope="col">Commit</th> <th class="table-heading" scope="col">Author</th> <th class="table-heading table-numeric" scope="col">Duration</th> </tr> </thead> <tbody> <tr class="table-row"> <td class="table-cell table-primary whitespace-nowrap">atlas-9f2c1a</td> <td class="table-cell whitespace-nowrap">main</td> <td class="table-cell whitespace-nowrap">Soften every edge</td> <td class="table-cell whitespace-nowrap">Ada Lovelace</td> <td class="table-cell table-numeric">48s</td> </tr> <tr class="table-row"> <td class="table-cell table-primary whitespace-nowrap">atlas-71be0d</td> <td class="table-cell whitespace-nowrap">feature/anchors</td> <td class="table-cell whitespace-nowrap">Glide the segmented thumb</td> <td class="table-cell whitespace-nowrap">Alan Turing</td> <td class="table-cell table-numeric">1m 02s</td> </tr> </tbody> </table> </div></div><template> <div class="sheet overflow-hidden"> <div class="scroll-area scroll-area-inline" tabindex="0" role="region" aria-label="Deployments"> <table class="table"> <thead> <tr class="table-row"> <th class="table-heading" scope="col">Deployment</th> <th class="table-heading" scope="col">Branch</th> <th class="table-heading" scope="col">Commit</th> <th class="table-heading" scope="col">Author</th> <th class="table-heading table-numeric" scope="col">Duration</th> </tr> </thead> <tbody> <tr class="table-row"> <td class="table-cell table-primary whitespace-nowrap">atlas-9f2c1a</td> <td class="table-cell whitespace-nowrap">main</td> <td class="table-cell whitespace-nowrap">Soften every edge</td> <td class="table-cell whitespace-nowrap">Ada Lovelace</td> <td class="table-cell table-numeric">48s</td> </tr> <tr class="table-row"> <td class="table-cell table-primary whitespace-nowrap">atlas-71be0d</td> <td class="table-cell whitespace-nowrap">feature/anchors</td> <td class="table-cell whitespace-nowrap">Glide the segmented thumb</td> <td class="table-cell whitespace-nowrap">Alan Turing</td> <td class="table-cell table-numeric">1m 02s</td> </tr> </tbody> </table> </div> </div></template>#无障碍
- 使用原生
<table>,用<thead>和<tbody>分别包裹表头和表体,并为列标题单元格设置scope="col",以便辅助技术在读出每个单元格时一并读出对应的列标题。 - 为滚动容器设置
tabindex="0"、role="region"和标签,使其支持键盘滚动。 - 选择状态有意通过两处传达:行上的
aria-selected和该行复选框的状态。
#类名
| 类名 | 说明 |
|---|---|
table | 表格可用原生 table,也可用 ARIA 角色组成网格。 |
table-row | 一行 |
table-heading | 列标题采用低调的文字标签和一条比行分隔线更醒目的横线。 |
table-cell | 向行内起始侧对齐的数据单元格或行表头。使用原生 scope=row/rowgroup 或 ARIA role=rowheader 的行表头采用主题的控件字重,覆盖浏览器的默认粗体。 |
table-sort | 可排序表头中用于更改顺序的按钮:指针悬停时,表头文字显示在胶囊底色上,并包含已排序列的折角箭头。 |
table-title | 表格名称,用于 caption:位于表头上方、靠起始边对齐的标题。 |
table-primary | 标识该行的单元格,使用较浓的墨色与 table-cell 一同使用 |
table-numeric | 在表头或单元格中将等宽数字右对齐与 table-heading 或 table-cell 一同使用 |
table-compact | 压缩数据行的高度,便于快速扫读较长的数据列表。与 table 一同使用 |
table-interactive | 包含单元格的行响应指针悬停,并在聚焦时显示焦点环与 table 一同使用 |
table-sticky | 数据行在表头下方滚动时,表头保持可见。与 table 一同使用 |
#自定义属性
| 属性 | 默认值 | 说明 |
|---|---|---|
--table-padding-x | 1rem | 表头和单元格在行内方向的内边距。紧凑表格中为 .75rem |
--table-padding-y | .75rem | 单元格在块轴方向的内边距;紧凑表格中使用 .4375rem |