CSSで表の行・列を固定する方法|見出し・先頭列・合計行

HTMLの表で行や列を固定するには、対象セルに position: sticky と固定位置を指定します。見出し行には inset-block-start: 0、先頭列には inset-inline-start: 0、合計行には inset-block-end: 0 を指定します。左から右に進む横書きでは、それぞれ top、left、bottom に相当します。

表専用のスクロール領域を作る

表を overflow: auto の要素で囲みます。縦にスクロールさせるには内容より低い高さの上限が、横にスクロールさせるには囲みより広い表が必要です。固定セルは元の位置にスペースを確保しつつ、表の範囲内で指定したスクロール領域の端に沿って移動します。CSS Positioned Layout — Sticky positioning

.scroll-area {
  max-height: 320px;
  overflow: auto;
}
thead th {
  position: sticky;
  inset-block-start: 0;
}

ページ全体のスクロールと表の中のスクロールは別の操作です。近くの祖先要素に overflow: hidden があると、スクロールバーがなくてもその要素が基準になる場合があります。開発者ツールでセルから目的のスクロール領域までの祖先要素の overflow を確認してください。CSS Overflow — hidden

見出し行・先頭列・合計行を固定するサンプル

下の例を sticky-table.html として保存し、開いてください。外部ファイルやJavaScriptは不要です。表の最小幅は840px、スクロール領域の幅は最大680px、高さは最大320pxです。月別数量は練習用の架空データです。

<!doctype html>
<html lang="ja">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>表の行と列を固定する</title>
  <style>
    body { margin: 20px; font: 16px/1.6 system-ui, sans-serif; color: #203348; }
    * { box-sizing: border-box; }
    h1 { font-size: 24px; }
    .demo { --first-col: 120px; max-width: 680px; max-height: 320px;
      overflow: auto; border: 1px solid #7795a3; isolation: isolate; }
    .demo:focus-visible { outline: 3px solid #b75c00; outline-offset: 2px; }
    .jb-table-01 { width: 100%; min-width: 840px; table-layout: fixed;
      border-collapse: separate; border-spacing: 0; }
    .first-col { width: var(--first-col); }
    caption { padding: 8px 12px; text-align: left; font-weight: 700; background: white; }
    th, td { padding: 10px 12px; border-right: 1px solid #c3d1d7;
      border-inset-block-end: 1px solid #c3d1d7; text-align: right; white-space: nowrap; background: white; }
    thead th { position: sticky; inset-block-start: 0; z-index: 2; background: #176f80; color: white; }
    tbody th:first-child { position: sticky; inset-inline-start: 0; z-index: 1; background: #e8f1f5; text-align: left; white-space: normal; overflow-wrap: anywhere; }
    thead th:first-child { inset-inline-start: 0; z-index: 3; text-align: left; }
    tfoot th, tfoot td { position: sticky; inset-block-end: 0; z-index: 2;
      background: #fff2cf; font-weight: 700; }
    tfoot th:first-child { inset-inline-start: 0; z-index: 3; text-align: left; }
  </style>
</head>
<body>
<h1>表の行・列固定サンプル</h1>
<p>表の中を横方向と縦方向にスクロールしてください。</p>
<div class="jb-table-scroll demo" tabindex="0" role="region" aria-labelledby="monthly-quantities">
  <table class="jb-table-01">
    <caption id="monthly-quantities">月別数量(練習用データ)</caption>
    <colgroup><col class="first-col"><col span="4"></colgroup>
    <thead>
      <tr>
        <th scope="col">品目</th>
        <th scope="col">1月</th>
        <th scope="col">2月</th>
        <th scope="col">3月</th>
        <th scope="col">4月</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <th scope="row">品目 01</th>
        <td>11</td>
        <td>13</td>
        <td>15</td>
        <td>17</td>
      </tr>
      <tr>
        <th scope="row">品目 02</th>
        <td>12</td>
        <td>14</td>
        <td>16</td>
        <td>18</td>
      </tr>
      <tr>
        <th scope="row">品目 03</th>
        <td>13</td>
        <td>15</td>
        <td>17</td>
        <td>19</td>
      </tr>
      <tr>
        <th scope="row">品目 04</th>
        <td>14</td>
        <td>16</td>
        <td>18</td>
        <td>20</td>
      </tr>
      <tr>
        <th scope="row">品目 05</th>
        <td>15</td>
        <td>17</td>
        <td>19</td>
        <td>21</td>
      </tr>
      <tr>
        <th scope="row">品目 06</th>
        <td>16</td>
        <td>18</td>
        <td>20</td>
        <td>22</td>
      </tr>
      <tr>
        <th scope="row">品目 07</th>
        <td>17</td>
        <td>19</td>
        <td>21</td>
        <td>23</td>
      </tr>
      <tr>
        <th scope="row">品目 08</th>
        <td>18</td>
        <td>20</td>
        <td>22</td>
        <td>24</td>
      </tr>
      <tr>
        <th scope="row">品目 09</th>
        <td>19</td>
        <td>21</td>
        <td>23</td>
        <td>25</td>
      </tr>
      <tr>
        <th scope="row">品目 10</th>
        <td>20</td>
        <td>22</td>
        <td>24</td>
        <td>26</td>
      </tr>
      <tr>
        <th scope="row">品目 11</th>
        <td>21</td>
        <td>23</td>
        <td>25</td>
        <td>27</td>
      </tr>
      <tr>
        <th scope="row">品目 12</th>
        <td>22</td>
        <td>24</td>
        <td>26</td>
        <td>28</td>
      </tr>
    </tbody>
    <tfoot>
      <tr>
        <th scope="row">合計</th>
        <td>198</td>
        <td>222</td>
        <td>246</td>
        <td>270</td>
      </tr>
    </tfoot>
  </table>
</div>
<p>Tabキーで表の領域にフォーカスし、矢印キーでスクロールできます。ページ全体のスクロールとは別に操作してください。</p>
</body>
</html>

表の中を下にスクロールすると、青緑色の見出しは上に、黄色の合計行は下に残ります。右にスクロールすると、品目名と左上・左下の角のセルが表示されたままになります。左上にスクロールし直すと、固定セルの元の位置が分かります。

caption で表の内容を示します。列の見出しに scope="col"、行の見出しに scope="row" を指定し、スクロール領域のアクセシブルな名前にも同じキャプションを参照します。この構造なら固定のスタイルを外しても、見出しとデータの関係が保たれます。W3C WAI — Tables with two headers

行や列だけを固定して交差セルの重なりを調整する

本文の先頭列は z-index: 1、上下の行は2、両方向に固定する角のセルは3です。固定セルの背景を不透明にすると、後ろに流れる数値が透けません。isolation: isolate は重なり順を領域内に閉じるため、外側のメニューとの前後関係はセルの値だけを上げても変わらないことがあります。

thead th { position: sticky; inset-block-start: 0; z-index: 2; background: #176f80; color: white; }

tbody th:first-child { position: sticky; inset-inline-start: 0; z-index: 1; background: #e8f1f5; }

thead th:first-child { inset-inline-start: 0; z-index: 3; }

行だけ固定する場合は本文の先頭列のstickyを解除し、見出しと合計行の先頭セルから inset-inline-start を削除します。列だけ固定する場合は上下の行のブロック方向のオフセットを外し、各行の先頭セルと本文の先頭列のstickyおよびインライン方向のオフセットを残します。上の行だけなら合計行のstickyも不要です。

2列や複数の見出し行を固定する

2列目のインライン方向のオフセットには、1列目の実際の幅を指定します。この例では colgroup と --first-col: 120px で幅を設定しています。次の規則をサンプルのスタイルの末尾に加え、右へスクロールして2列が並んで残ることを確認します。規則を削除すると元に戻ります。

tbody td:nth-child(2) {
  position: sticky;
  inset-inline-start: var(--first-col);
  z-index: 1;
  background: #e8f1f5;
}
thead th:nth-child(2), tfoot td:nth-child(2) {
  inset-inline-start: var(--first-col);
  z-index: 3;
}

画面幅に応じて1列目の幅を変更するなら、2列目のオフセットも合わせます。複数の見出し行をセル単位で固定する場合、2行目は1行目の実際の高さだけずらします。両方を0にすると重なります。折り返しや拡大で行高が変わり、見出し行だけを固定したい場合は、個々のセルからブロック方向のオフセットを外し、thead 全体を固定する方法もあります。

固定されない場合や内容が隠れる場合

固定されないときは、position の計算値、対象軸のオフセット、領域から内容がはみ出しているかを確認します。罫線が他のセルに隠れる場合は、例と同じ border-collapse: separate と border-spacing: 0 を試してください。CSS 2 — The separated borders model

tabindex="0" を指定すると、Tabキーで領域にフォーカスし、矢印キーでスクロールして、Tabキーで外へ移動できます。フォーカス枠は残します。狭い表示や拡大時に固定列が幅の大半を占めるなら、その幅では先頭列だけ残すか列の固定を解除します。

この例は1行5セルで結合セルがありません。colspan があると2つ目のセルが実際の2列目に当たらないことがあるため、:nth-child(2) に頼らず対象セルを個別に指定します。複雑な表では見出しとデータの対応も設計してください。HTML — table