CSS / リファレンス / caption-side:表のキャプションの位置を指定する

CSSのcaption-sideは、HTMLの表題である<caption>をデータ部分の上または下に配置します。初期値はtopです。下に表示する場合はbottomを指定します。HTMLでは、どちらの場合も<table>の最初の子要素にキャプションを置きます。

表の上または下にキャプションを配置する

横書きの場合、topは列見出しとデータ行の上、bottomは最後の行の下に表示します。このプロパティはtable-captionボックスに適用され、継承されます。表に指定した値をキャプションに引き継がせることもできます。詳しくはキャプションの位置を定めるCSS仕様を参照してください。

table.sales { caption-side: bottom; }
table.sales > caption { text-align: start; padding-block: .45em; }

.salesで対象の表を指定すると、ページ内のほかの表をbottomにすることを避けられます。キャプション内の文字の配置や余白は個別に指定します。

ページ上で2つの位置を比べる

2つの表のデータとキャプションは同じです。最初の表では列見出しの上、2つ目では最後の行の下にキャプションがあります。キャプションを動かしても月の順序と数量は変わりません。

caption-side: top

月別の販売数量
月 数量
1月 12
2月 18

caption-side: bottom

月別の販売数量
月 数量
1月 12
2月 18

実行例でHTMLの構造を確認する

以下の文書全体をUTF-8でcaption-side-example.htmlに保存し、ブラウザーで開きます。外部スタイルシートは不要です。1つ目は初期位置を使い、2つ目には.belowクラスを付けています。

<!doctype html>
<html lang="ja">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>CSS caption-sideの比較</title>
  <style>
    body { margin: 1rem; color: #20242b; background: #fff;
      font: 1rem/1.5 system-ui, sans-serif; }
    main { max-width: 42rem; margin-inline: auto; }
    .examples { display: grid; gap: 1rem;
      grid-template-columns: repeat(auto-fit, minmax(min(100%, 13rem), 1fr)); }
    table.sales { width: 100%; border-collapse: collapse; }
    table.sales.below { caption-side: bottom; }
    table.sales > caption { font-weight: 650; padding-block: .45em;
      text-align: start; overflow-wrap: anywhere; }
    td, th { border: 1px solid #647681; padding: .45rem .6rem; }
    th { text-align: start; }
    td { text-align: end; }
    thead { background: #e6f1f7; }
  </style>
</head>
<body>
  <main>
    <h1>CSS caption-sideの比較</h1>
    <div class="examples">
      <div>
        <p><code>top</code></p>
        <table class="sales above">
          <caption>月別の販売数量</caption>
          <thead><tr><th scope="col">月</th><th scope="col">数量</th></tr></thead>
          <tbody>
            <tr><th scope="row">1月</th><td>12</td></tr>
            <tr><th scope="row">2月</th><td>18</td></tr>
          </tbody>
        </table>
      </div>
      <div>
        <p><code>bottom</code></p>
        <table class="sales below compact">
          <caption>月別の販売数量</caption>
          <thead><tr><th scope="col">月</th><th scope="col">数量</th></tr></thead>
          <tbody>
            <tr><th scope="row">1月</th><td>12</td></tr>
            <tr><th scope="row">2月</th><td>18</td></tr>
          </tbody>
        </table>
      </div>
    </div>
  </main>
</body>
</html>

table.sales.belowのbottomだけをtopに変更して再読み込みします。2つ目のキャプションが表の上に移動し、行の順序は変わりません。確認したらbottomに戻します。

<caption>には、表の内容が分かる短い表題を入れます。下に表示する場合も、ソースではtheadの前に置きます。これで表題を表に結び付けられます。最初の子要素に置く規則はcaption要素のHTML仕様を参照してください。

文字の配置、余白、折り返しを調整する

キャプションを置く位置を決めたら、内部の文字をtext-alignで整列します。横書きではstartが書字方向の始点寄せ、centerが中央揃えです。上下の間隔はpadding-blockで調整できます。tdやthの整列はセルの内容に適用されます。

キャプションを長くしてウィンドウを狭めると、折り返す様子を確認できます。区切りのない長い文字列を収めるには、例のようにoverflow-wrap: anywhereを使います。縦書きでは実際の書字モードとブラウザーで位置を確認してください。表と列の幅はCSSで表と列の幅を調整する方法で解説しています。

キャプションの位置が変わらない原因を調べる

開発者ツールでcaptionを選び、caption-sideとdisplayの計算値を確認します。このプロパティにはtable-captionボックスが必要です。表にbottomを指定しても上に残る場合は、キャプションに直接適用される宣言を探します。

table.sales.compact > caption {
  caption-side: top;
  text-align: center;
}

上のルールを例のスタイルシートの末尾に追加します。直接指定したtopが、2つ目の表から継承したbottomより優先されます。text-align: centerを削除して変更を分けて確かめましょう。キャプションは上に残り、文字だけが始点寄せに戻ります。