表
行と列で数字を見比べる部品です。数字は右揃え・等幅の数字にし、単位は見出しに書きます。
見本で種類と状態を確かめる
行の高さは 48、縞は付けません。見出しは label の文字で下に line-strong、合計の行は太字にせず上に line-strong を引きます。横にはみ出す表は包みの中でスクロールし、続きのある側の端にだけ薄い影が出ます。
選択の行と合計
| 期間 | 収入(円) | 支出(円) | 収支(円) |
|---|---|---|---|
| 2026年9月 | 298,540 | 311,870 | −13,330 |
| 2026年10月 | 301,200 | 287,450 | 13,750 |
| 2026年11月 | 286,000 | 238,340 | 47,660 |
| 3か月の合計 | 885,740 | 837,660 | 48,080 |
横にスクロール
| 費目 | 5月 | 6月 | 7月 | 8月 | 9月 | 10月 | 11月 |
|---|---|---|---|---|---|---|---|
| 食費 | 48,210 | 51,380 | 46,920 | 49,870 | 47,650 | 50,240 | 45,310 |
| 日用品 | 8,420 | 6,130 | 9,870 | 7,240 | 6,980 | 8,150 | 7,560 |
| 交通 | 11,200 | 9,860 | 12,400 | 10,750 | 11,980 | 10,320 | 9,640 |
コードを写す
HTML
<div class="mz-table-wrap" data-sp="list" role="region" aria-labelledby="cap-light" tabindex="0">
<table class="mz-table">
<caption id="cap-light">月ごとに貯金は増えているか</caption>
<thead>
<tr>
<th scope="col" aria-sort="ascending"><button class="mz-table__sort" type="button"><span>期間</span><svg class="mz-table__sort-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M12 19V5"/><path d="m5 12 7-7 7 7"/></svg></button></th>
<th scope="col" data-numeric><button class="mz-table__sort" type="button"><span>収入(円)</span><svg class="mz-table__sort-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m7 15 5 5 5-5"/><path d="m7 9 5-5 5 5"/></svg></button></th>
<th scope="col" data-numeric><button class="mz-table__sort" type="button"><span>支出(円)</span><svg class="mz-table__sort-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m7 15 5 5 5-5"/><path d="m7 9 5-5 5 5"/></svg></button></th>
<th scope="col" data-numeric>収支(円)</th>
<th scope="col" data-numeric>貯金の残高(円)</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row"><a href="/months/2026-08">2026年8月</a></th>
<td data-numeric>412,300</td>
<td data-numeric>356,120</td>
<td data-numeric>56,180</td>
<td data-numeric>2,184,600</td>
</tr>
<tr>
<th scope="row"><a href="/months/2026-09">2026年9月</a></th>
<td data-numeric>298,540</td>
<td data-numeric>311,870</td>
<td data-numeric data-tone="danger">−13,330</td>
<td data-numeric>2,171,270</td>
</tr>
<tr>
<th scope="row"><a href="/months/2026-10">2026年10月</a></th>
<td data-numeric>301,200</td>
<td data-numeric>287,450</td>
<td data-numeric>13,750</td>
<td data-numeric>2,185,020</td>
</tr>
<tr aria-current="true">
<th scope="row"><a href="/months/2026-11">2026年11月</a></th>
<td data-numeric>286,000</td>
<td data-numeric>238,340</td>
<td data-numeric>47,660</td>
<td data-numeric>2,232,680</td>
</tr>
</tbody>
<tfoot>
<tr>
<th scope="row">4か月の合計</th>
<td data-numeric>1,298,040</td>
<td data-numeric>1,193,780</td>
<td data-numeric>104,260</td>
<td data-numeric>—</td>
</tr>
</tfoot>
</table>
</div>React の呼び出し方
<Table caption="月ごとに貯金は増えているか">
<thead>
<tr>
<th scope="col">期間</th>
<th scope="col" data-numeric>収入(円)</th>
<th scope="col" data-numeric>支出(円)</th>
<th scope="col" data-numeric>収支(円)</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">2026年10月</th>
<td data-numeric>301,200</td>
<td data-numeric>287,450</td>
<td data-numeric>13,750</td>
</tr>
<tr>
<th scope="row">2026年11月</th>
<td data-numeric>286,000</td>
<td data-numeric>238,340</td>
<td data-numeric>47,660</td>
</tr>
</tbody>
</Table>実装を見る(react/Table.tsx)
React の実装(react/Table.tsx)
/**
* Table — 表(docs/spec/components.md「table」)
* 見た目は css/components/table.css だけが持つ。
* 包み(mz-table-wrap)を横スクロールの領域にし、キーボードでも動かせるよう tabIndex={0} と、caption を指す名前を付ける。
* 数字の列は <th data-numeric>・<td data-numeric>。並べ替えは <th aria-sort> > TableSortButton。
*/
import { forwardRef, useId, type ButtonHTMLAttributes, type ReactNode, type TableHTMLAttributes } from 'react';
export interface TableProps extends TableHTMLAttributes<HTMLTableElement> {
/** 見える見出し(caption) */
caption: ReactNode;
/** 'list':640 未満で表を隠す(組み替えた一覧とその見出しには data-table-sp を付ける) */
sp?: 'list';
}
export const Table = forwardRef<HTMLTableElement, TableProps>(function Table(
{ caption, sp, className, children, ...rest },
ref,
) {
const id = useId();
return (
<div className="mz-table-wrap" data-sp={sp} role="region" aria-labelledby={id} tabIndex={0}>
<table ref={ref} className={className ? `mz-table ${className}` : 'mz-table'} {...rest}>
<caption id={id}>{caption}</caption>
{children}
</table>
</div>
);
});
export interface TableSortButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
/** 親の <th aria-sort> と同じ値。降順の矢印の向きは CSS が aria-sort から決める */
sort?: 'ascending' | 'descending' | 'none';
}
export const TableSortButton = forwardRef<HTMLButtonElement, TableSortButtonProps>(function TableSortButton(
{ sort = 'none', className, children, type = 'button', ...rest },
ref,
) {
return (
<button ref={ref} type={type} className={className ? `mz-table__sort ${className}` : 'mz-table__sort'} {...rest}>
<span>{children}</span>
<svg
className="mz-table__sort-icon"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="1.5"
strokeLinecap="round"
strokeLinejoin="round"
aria-hidden="true"
>
{sort === 'none' ? (
<>
<path d="m7 15 5 5 5-5" />
<path d="m7 9 5-5 5 5" />
</>
) : (
<>
<path d="M12 19V5" />
<path d="m5 12 7-7 7 7" />
</>
)}
</svg>
</button>
);
});見出しで並べ替えるときは、<th aria-sort> の中に <button class="mz-table__sort"> を置きます。全体の見本は demo/table.html にあります。
いつ使うか
- 月ごと・費目ごとなど、行と列の両方で数字を見比べるときに使います。行を見るだけなら一覧にします。
- 単位は見出しに書き、セルは数字だけにします。
- 640 未満の幅で列が 4 つを超える表は、一覧に組み替えた版を用意します。包みに
data-sp="list"を付けると 640 未満で隠れ、data-table-spを付けた一覧がそのときだけ出ます。
読み上げとキーボードにどう応えるか
<caption>に見える見出しを書きます。包みはrole="region"とaria-labelledbyで caption と結び、tabindex="0"でキーボードから横にスクロールできるようにします。- 列の見出しは
scope="col"、行の見出しはscope="row"にします。 - スクロールバーは隠しません。
よい例と悪い例
| 家賃 | 78,000 |
|---|---|
| 通信 | 6,480 |
| 家賃 | 78,000円 |
|---|---|
| 通信 | 6,480円 |
更新日