用 CSS sticky 固定表格的行和列

要固定 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="zh">
<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。

固定两列或多行表头

第二列的行内轴偏移应等于第一列的实际宽度。示例用 colgroup 和 --first-col: 120px 定义这一宽度。将以下规则加到样式末尾,向右滚动以比较两列。删掉这些规则即可恢复为只固定首列。

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;
}

如果第一列在某个宽度下发生变化,第二列的偏移也要调整。如果分别固定多行表头中的单元格,第二行的偏移应等于第一行的实际高度。两个偏移都是 0 时会互相遮挡。如果只需固定表头,并且其行高会随换行或缩放改变,可以去掉各单元格的块轴偏移,尝试固定整个 thead。

排查固定失效和内容被遮挡

如果固定失效,检查 position 的计算值、相应轴的偏移以及内容是否超出区域。如果边框被其他单元格遮挡,可尝试示例中的 border-collapse: separate 和 border-spacing: 0。CSS 2 — The separated borders model

设置 tabindex="0" 后,可以用 Tab 进入区域,用方向键滚动,再用 Tab 离开。请保留焦点轮廓。如果在窄屏或放大时固定列几乎占满宽度,可只固定一列,也可以在该宽度下取消列固定。

示例每行有五格,没有合并单元格。使用 colspan 后,第二个单元格不一定在第二列。请明确选择要固定的单元格,而不要直接套用 :nth-child(2)。表头更复杂时,还需设计表头和数据的对应关系。HTML — table