表格
用細線分隔資料列,提供可排序的欄標題與對齊的數字。
本頁內容
表格用細線分隔資料列。欄標題採用低調的小字,下方以較明顯的線分隔,讓視線集中在資料列上。將 table 加到 <table>、table-row 加到每個 <tr>、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。以 ARIA 角色組成的網格中,帶有 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-row | 一列 |
table-heading | 欄標題採用低調的小字,底線比資料列的分隔線更醒目 |
table-cell | 對齊起始側的資料儲存格或列標題。原生的 scope=row/rowgroup 或 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 |