固定ヘッダーテーブル

社員名簿のような縦に長い表向けの固定ヘッダーテーブル。スクロールしてもヘッダー行と先頭列が画面に残り続け、どの行かすぐ把握できます。

#css#table

ライブデモ

コード

HTML
<!-- 固定ヘッダーテーブル: スクロールしてもヘッダー行と先頭列が残る社員名簿 -->
<section class="tb4-wrap">
  <div class="tb4-card">
    <header class="tb4-head">
      <h2 class="tb4-title">社員名簿</h2>
      <p class="tb4-hint">スクロールしてご確認ください</p>
    </header>
    <div class="tb4-scroll">
      <table class="tb4-table">
        <caption class="sr-only">氏名・部署・役職・入社日・内線番号の一覧</caption>
        <thead>
          <tr>
            <th scope="col" class="tb4-corner">氏名</th>
            <th scope="col">部署</th>
            <th scope="col">役職</th>
            <th scope="col">入社日</th>
            <th scope="col" class="tb4-num">内線</th>
          </tr>
        </thead>
        <tbody>
          <tr><th scope="row">田中 一郎</th><td>開発部</td><td>エンジニア</td><td>2021-04-01</td><td class="tb4-num">1201</td></tr>
          <tr><th scope="row">鈴木 花子</th><td>人事部</td><td>マネージャー</td><td>2018-09-15</td><td class="tb4-num">1305</td></tr>
          <tr><th scope="row">佐藤 健</th><td>営業部</td><td>営業</td><td>2022-01-10</td><td class="tb4-num">1402</td></tr>
          <tr><th scope="row">高橋 美咲</th><td>マーケティング部</td><td>リーダー</td><td>2019-07-01</td><td class="tb4-num">1108</td></tr>
          <tr><th scope="row">伊藤 大輔</th><td>開発部</td><td>シニアエンジニア</td><td>2016-03-20</td><td class="tb4-num">1202</td></tr>
          <tr><th scope="row">渡辺 さくら</th><td>経理部</td><td>経理担当</td><td>2020-11-05</td><td class="tb4-num">1509</td></tr>
          <tr><th scope="row">山本 翔太</th><td>営業部</td><td>営業</td><td>2023-04-01</td><td class="tb4-num">1403</td></tr>
          <tr><th scope="row">中村 蓮</th><td>開発部</td><td>エンジニア</td><td>2022-08-15</td><td class="tb4-num">1203</td></tr>
          <tr><th scope="row">小林 葵</th><td>カスタマーサポート部</td><td>サポート担当</td><td>2021-02-01</td><td class="tb4-num">1601</td></tr>
          <tr><th scope="row">加藤 悠斗</th><td>営業部</td><td>営業マネージャー</td><td>2015-06-01</td><td class="tb4-num">1404</td></tr>
          <tr><th scope="row">吉田 陽菜</th><td>マーケティング部</td><td>デザイナー</td><td>2020-04-01</td><td class="tb4-num">1109</td></tr>
          <tr><th scope="row">山田 拓海</th><td>開発部</td><td>インフラエンジニア</td><td>2019-10-01</td><td class="tb4-num">1204</td></tr>
          <tr><th scope="row">松本 結衣</th><td>人事部</td><td>採用担当</td><td>2022-04-01</td><td class="tb4-num">1306</td></tr>
          <tr><th scope="row">井上 直樹</th><td>経理部</td><td>経理マネージャー</td><td>2014-04-01</td><td class="tb4-num">1510</td></tr>
          <tr><th scope="row">木村 花音</th><td>カスタマーサポート部</td><td>チームリーダー</td><td>2017-09-01</td><td class="tb4-num">1602</td></tr>
          <tr><th scope="row">斎藤 蓮</th><td>開発部</td><td>QAエンジニア</td><td>2023-01-10</td><td class="tb4-num">1205</td></tr>
        </tbody>
      </table>
    </div>
  </div>
</section>
CSS
/* 固定ヘッダーテーブル: 固定高スクロール内でヘッダー行と先頭列をposition:stickyで固定 */
* { box-sizing: border-box; }
body {
  margin: 0;
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 28px 16px;
  font-family: "Segoe UI", "Hiragino Kaku Gothic ProN", system-ui, -apple-system, sans-serif;
  background: linear-gradient(160deg, #f7f8fc 0%, #eef0f7 100%);
  color: #1c2033;
}
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}

.tb4-wrap { width: 100%; display: grid; place-items: center; }
.tb4-card {
  width: min(760px, 100%);
  background: #fff;
  border-radius: 18px;
  box-shadow: 0 24px 60px rgba(24, 28, 55, .10), 0 2px 8px rgba(24, 28, 55, .04);
  overflow: hidden;
}
.tb4-head {
  display: flex; align-items: baseline; justify-content: space-between; gap: 12px;
  padding: 20px 24px; border-bottom: 1px solid #eef0f6;
}
.tb4-title { margin: 0; font-size: 17px; font-weight: 800; }
.tb4-hint { margin: 0; font-size: 12px; color: #8b91a7; }

.tb4-scroll { width: 100%; max-height: 320px; overflow: auto; }
.tb4-table { width: 100%; min-width: 520px; border-collapse: separate; border-spacing: 0; font-size: 13.5px; }

.tb4-table thead th {
  position: sticky; top: 0; z-index: 2;
  text-align: left; padding: 12px 20px; background: #12172b; color: #fff; font-weight: 700;
  font-size: 12.5px; letter-spacing: .02em; white-space: nowrap; border-bottom: 1px solid #12172b;
}
.tb4-table thead th.tb4-num { text-align: right; }
.tb4-table thead th.tb4-corner { left: 0; z-index: 3; }

.tb4-table tbody th, .tb4-table tbody td {
  padding: 12px 20px; border-bottom: 1px solid #eef0f6; white-space: nowrap; text-align: left;
  font-weight: 400; background: #fff;
}
.tb4-table tbody th {
  position: sticky; left: 0; z-index: 1;
  font-weight: 700; color: #1c2033; box-shadow: 1px 0 0 #eef0f6;
}
.tb4-table tbody tr:hover th, .tb4-table tbody tr:hover td { background: #f7f8ff; }
.tb4-table tbody tr:last-child th, .tb4-table tbody tr:last-child td { border-bottom: none; }
.tb4-num { text-align: right; font-variant-numeric: tabular-nums; }
JavaScript
// 固定ヘッダーテーブル: position:stickyのみで実現するため追加のJSは不要

🤖 AIエージェント用プロンプト

このままコピーしてAIに貼り付け「追加する場所」だけ書き換えればOK
あなたは熟練のフロントエンドエンジニアです。私のWebサイトに「固定ヘッダーテーブル」の効果を追加してください。

# 追加してほしい効果
固定ヘッダーテーブル(カード & テーブル)
社員名簿のような縦に長い表向けの固定ヘッダーテーブル。スクロールしてもヘッダー行と先頭列が画面に残り続け、どの行かすぐ把握できます。

# 追加する場所
👉【ここに対象箇所を記入:例「トップのヒーローセクション」「お問い合わせボタン」「記事カードの一覧」など】

# 参考実装(この見た目・挙動を再現してください)
【HTML】
<!-- 固定ヘッダーテーブル: スクロールしてもヘッダー行と先頭列が残る社員名簿 -->
<section class="tb4-wrap">
  <div class="tb4-card">
    <header class="tb4-head">
      <h2 class="tb4-title">社員名簿</h2>
      <p class="tb4-hint">スクロールしてご確認ください</p>
    </header>
    <div class="tb4-scroll">
      <table class="tb4-table">
        <caption class="sr-only">氏名・部署・役職・入社日・内線番号の一覧</caption>
        <thead>
          <tr>
            <th scope="col" class="tb4-corner">氏名</th>
            <th scope="col">部署</th>
            <th scope="col">役職</th>
            <th scope="col">入社日</th>
            <th scope="col" class="tb4-num">内線</th>
          </tr>
        </thead>
        <tbody>
          <tr><th scope="row">田中 一郎</th><td>開発部</td><td>エンジニア</td><td>2021-04-01</td><td class="tb4-num">1201</td></tr>
          <tr><th scope="row">鈴木 花子</th><td>人事部</td><td>マネージャー</td><td>2018-09-15</td><td class="tb4-num">1305</td></tr>
          <tr><th scope="row">佐藤 健</th><td>営業部</td><td>営業</td><td>2022-01-10</td><td class="tb4-num">1402</td></tr>
          <tr><th scope="row">高橋 美咲</th><td>マーケティング部</td><td>リーダー</td><td>2019-07-01</td><td class="tb4-num">1108</td></tr>
          <tr><th scope="row">伊藤 大輔</th><td>開発部</td><td>シニアエンジニア</td><td>2016-03-20</td><td class="tb4-num">1202</td></tr>
          <tr><th scope="row">渡辺 さくら</th><td>経理部</td><td>経理担当</td><td>2020-11-05</td><td class="tb4-num">1509</td></tr>
          <tr><th scope="row">山本 翔太</th><td>営業部</td><td>営業</td><td>2023-04-01</td><td class="tb4-num">1403</td></tr>
          <tr><th scope="row">中村 蓮</th><td>開発部</td><td>エンジニア</td><td>2022-08-15</td><td class="tb4-num">1203</td></tr>
          <tr><th scope="row">小林 葵</th><td>カスタマーサポート部</td><td>サポート担当</td><td>2021-02-01</td><td class="tb4-num">1601</td></tr>
          <tr><th scope="row">加藤 悠斗</th><td>営業部</td><td>営業マネージャー</td><td>2015-06-01</td><td class="tb4-num">1404</td></tr>
          <tr><th scope="row">吉田 陽菜</th><td>マーケティング部</td><td>デザイナー</td><td>2020-04-01</td><td class="tb4-num">1109</td></tr>
          <tr><th scope="row">山田 拓海</th><td>開発部</td><td>インフラエンジニア</td><td>2019-10-01</td><td class="tb4-num">1204</td></tr>
          <tr><th scope="row">松本 結衣</th><td>人事部</td><td>採用担当</td><td>2022-04-01</td><td class="tb4-num">1306</td></tr>
          <tr><th scope="row">井上 直樹</th><td>経理部</td><td>経理マネージャー</td><td>2014-04-01</td><td class="tb4-num">1510</td></tr>
          <tr><th scope="row">木村 花音</th><td>カスタマーサポート部</td><td>チームリーダー</td><td>2017-09-01</td><td class="tb4-num">1602</td></tr>
          <tr><th scope="row">斎藤 蓮</th><td>開発部</td><td>QAエンジニア</td><td>2023-01-10</td><td class="tb4-num">1205</td></tr>
        </tbody>
      </table>
    </div>
  </div>
</section>

【CSS】
/* 固定ヘッダーテーブル: 固定高スクロール内でヘッダー行と先頭列をposition:stickyで固定 */
* { box-sizing: border-box; }
body {
  margin: 0;
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 28px 16px;
  font-family: "Segoe UI", "Hiragino Kaku Gothic ProN", system-ui, -apple-system, sans-serif;
  background: linear-gradient(160deg, #f7f8fc 0%, #eef0f7 100%);
  color: #1c2033;
}
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}

.tb4-wrap { width: 100%; display: grid; place-items: center; }
.tb4-card {
  width: min(760px, 100%);
  background: #fff;
  border-radius: 18px;
  box-shadow: 0 24px 60px rgba(24, 28, 55, .10), 0 2px 8px rgba(24, 28, 55, .04);
  overflow: hidden;
}
.tb4-head {
  display: flex; align-items: baseline; justify-content: space-between; gap: 12px;
  padding: 20px 24px; border-bottom: 1px solid #eef0f6;
}
.tb4-title { margin: 0; font-size: 17px; font-weight: 800; }
.tb4-hint { margin: 0; font-size: 12px; color: #8b91a7; }

.tb4-scroll { width: 100%; max-height: 320px; overflow: auto; }
.tb4-table { width: 100%; min-width: 520px; border-collapse: separate; border-spacing: 0; font-size: 13.5px; }

.tb4-table thead th {
  position: sticky; top: 0; z-index: 2;
  text-align: left; padding: 12px 20px; background: #12172b; color: #fff; font-weight: 700;
  font-size: 12.5px; letter-spacing: .02em; white-space: nowrap; border-bottom: 1px solid #12172b;
}
.tb4-table thead th.tb4-num { text-align: right; }
.tb4-table thead th.tb4-corner { left: 0; z-index: 3; }

.tb4-table tbody th, .tb4-table tbody td {
  padding: 12px 20px; border-bottom: 1px solid #eef0f6; white-space: nowrap; text-align: left;
  font-weight: 400; background: #fff;
}
.tb4-table tbody th {
  position: sticky; left: 0; z-index: 1;
  font-weight: 700; color: #1c2033; box-shadow: 1px 0 0 #eef0f6;
}
.tb4-table tbody tr:hover th, .tb4-table tbody tr:hover td { background: #f7f8ff; }
.tb4-table tbody tr:last-child th, .tb4-table tbody tr:last-child td { border-bottom: none; }
.tb4-num { text-align: right; font-variant-numeric: tabular-nums; }

【JavaScript】
// 固定ヘッダーテーブル: position:stickyのみで実現するため追加のJSは不要

# 外部ライブラリ
なし(追加ライブラリ不要)

# 守ってほしいこと
- 既存のHTML構造・レイアウト・デザインを壊さないこと。必要に応じてクラス名・色・サイズを私のサイトに合わせて調整してよい。
- クラス名やidが既存と衝突しないよう、必要なら接頭辞で名前空間を分けること。
- レスポンシブ対応と prefers-reduced-motion への配慮を入れること。
- 私のサイトのフレームワーク(React / Vue / 素のHTML など)に合わせて実装すること。不明な場合は素のHTML/CSS/JSで提示し、組み込み手順も説明すること。
- 変更後の確認手順も簡潔に教えてください。