How to freeze table rows and columns with CSS sticky positioning

To freeze rows or columns in an HTML table, give their cells position: sticky and an offset for the side where they should stick. Use inset-block-start: 0 for headers, inset-inline-start: 0 for the first column, and inset-block-end: 0 for totals. In horizontal left-to-right text, these are equivalent to top, left, and bottom.

Create a scrollable table region

Wrap the table in an element with overflow: auto. Vertical scrolling requires a height limit smaller than the contents. For horizontal scrolling, the table needs to be wider than the wrapper. Sticky cells still take up their usual space as they follow the designated edge of the scroll region within the bounds of the table. CSS Positioned Layout — Sticky positioning

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

Scrolling the surrounding page is different from scrolling inside the table. A nearer ancestor with overflow: hidden can become the reference even when it has no scrollbar. Inspect the ancestors between the cells and your intended scroll region to see which overflow values apply. CSS Overflow — hidden

Run the example with a sticky header, column, and footer

Save the example as sticky-table.html and open it. It needs no other files or JavaScript. The table has a minimum width of 840px. The scroll region is capped at 680px wide and 320px high. The monthly quantities are sample data.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Sticky table rows and columns</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>Sticky table demo</h1>
<p>Scroll horizontally and vertically inside the table region.</p>
<div class="jb-table-scroll demo" tabindex="0" role="region" aria-labelledby="monthly-quantities">
  <table class="jb-table-01">
    <caption id="monthly-quantities">Monthly quantities: sample data</caption>
    <colgroup><col class="first-col"><col span="4"></colgroup>
    <thead>
      <tr>
        <th scope="col">Item</th>
        <th scope="col">Jan</th>
        <th scope="col">Feb</th>
        <th scope="col">Mar</th>
        <th scope="col">Apr</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <th scope="row">Item 01</th>
        <td>11</td>
        <td>13</td>
        <td>15</td>
        <td>17</td>
      </tr>
      <tr>
        <th scope="row">Item 02</th>
        <td>12</td>
        <td>14</td>
        <td>16</td>
        <td>18</td>
      </tr>
      <tr>
        <th scope="row">Item 03</th>
        <td>13</td>
        <td>15</td>
        <td>17</td>
        <td>19</td>
      </tr>
      <tr>
        <th scope="row">Item 04</th>
        <td>14</td>
        <td>16</td>
        <td>18</td>
        <td>20</td>
      </tr>
      <tr>
        <th scope="row">Item 05</th>
        <td>15</td>
        <td>17</td>
        <td>19</td>
        <td>21</td>
      </tr>
      <tr>
        <th scope="row">Item 06</th>
        <td>16</td>
        <td>18</td>
        <td>20</td>
        <td>22</td>
      </tr>
      <tr>
        <th scope="row">Item 07</th>
        <td>17</td>
        <td>19</td>
        <td>21</td>
        <td>23</td>
      </tr>
      <tr>
        <th scope="row">Item 08</th>
        <td>18</td>
        <td>20</td>
        <td>22</td>
        <td>24</td>
      </tr>
      <tr>
        <th scope="row">Item 09</th>
        <td>19</td>
        <td>21</td>
        <td>23</td>
        <td>25</td>
      </tr>
      <tr>
        <th scope="row">Item 10</th>
        <td>20</td>
        <td>22</td>
        <td>24</td>
        <td>26</td>
      </tr>
      <tr>
        <th scope="row">Item 11</th>
        <td>21</td>
        <td>23</td>
        <td>25</td>
        <td>27</td>
      </tr>
      <tr>
        <th scope="row">Item 12</th>
        <td>22</td>
        <td>24</td>
        <td>26</td>
        <td>28</td>
      </tr>
    </tbody>
    <tfoot>
      <tr>
        <th scope="row">Total</th>
        <td>198</td>
        <td>222</td>
        <td>246</td>
        <td>270</td>
      </tr>
    </tfoot>
  </table>
</div>
<p>Press Tab to focus the table region, then use the arrow keys to scroll. The page has a separate scroll position.</p>
</body>
</html>

Scroll down inside the region. The teal headings stay at the top and the yellow totals stay at the bottom. Scroll right and the item names and the two left corner cells stay in view. Return to the top left to see each sticky cell’s starting position.

Use caption to describe what the table contains, scope="col" for column headings, and scope="row" for row headings. The scroll region takes its accessible name from the same caption. The headers remain associated with their data even if you remove the sticky styles. W3C WAI — Tables with two headers

Freeze just the rows or column and resolve overlaps

Give the first column in the body z-index: 1, the header and footer 2, and the two-axis corner cells 3. An opaque background keeps the scrolling figures from showing through. isolation: isolate keeps the stacking order local. Increasing a cell’s z-index alone may have no effect on its placement relative to a menu outside the scroll region.

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

For rows only, remove sticky positioning from the first column in the body and remove inset-inline-start from the first header and footer cells. For the column only, remove the block-axis offsets from the header and footer while keeping sticky positioning and the inline-axis offset on their first cells and the first body column. For just the top row, also remove sticky positioning from the footer.

Freeze two columns or multiple header rows

A second sticky column needs an inline offset equal to the actual width of the first. The example sets that width with colgroup and --first-col: 120px. Add the following rules at the end of the example styles and scroll right to compare the two columns. Remove them to restore the single sticky column.

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

If the first column changes width at a breakpoint, update the second offset to match. For individually sticky cells in multiple header rows, offset the second row by the actual height of the first. A zero offset on both makes them overlap. If only the header needs to stick and its rows can wrap or grow with zoom, consider sticking the whole thead after clearing the individual cells’ block-axis offsets.

Troubleshoot sticky cells and obscured content

Check the computed position, the inset on the relevant axis, and whether the contents overflow the region. If the borders appear to vanish behind other cells, compare border-collapse: separate with border-spacing: 0, as in the example. CSS 2 — The separated borders model

With tabindex="0", users can Tab into the region, use the arrow keys to scroll, and Tab out. Keep the focus outline visible. If sticky columns take up most of the width on a narrow display or at high zoom, keep just one or disable column stickiness at that width.

This example has five cells per row and no merged cells. With colspan, the second cell may not occupy the second physical column. Target the intended cells explicitly instead of relying on :nth-child(2). For a more complex header, plan the relationships between headers and data as well. HTML — table