Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h1>Fixed Table Header Demo: Responsive with Scroll Snap</h1>

<p>
Scroll snap on both axes.
</p>

<h2>Column Headers</h2>

<div role="region" aria-labelledby="HeadersRow" tabindex="0" class="colheaders">
  <table>
    <caption id="HeadersRow">Books with a Fixed Column Header Row</caption>
    <thead>
      <tr>
        <th>Author</th>
        <th>Title</th>
        <th>Year</th>
        <th>ISBN-13</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Miguel De Cervantes</td>
        <td>The Ingenious Gentleman Don Quixote of La Mancha</td>
        <td>1605</td>
        <td>9783125798502</td>
      </tr>
      <tr>
        <td>Gabrielle-Suzanne Barbot de Villeneuve</td>
        <td>La Belle et la Bête</td>
        <td>1740</td>
        <td>9781910880067</td>
      </tr>
      <tr>
        <td>Sir Isaac Newton</td>
        <td>The Method of Fluxions and Infinite Series: With Its Application to the Geometry of Curve-lines</td>
        <td>1763</td>
        <td>9781330454862</td>
      </tr>
      <tr>
        <td>Mary Shelley</td>
        <td>Frankenstein; or, The Modern Prometheus</td>
        <td>1818</td>
        <td>9781530278442</td>
      </tr>
      <tr>
        <td>Herman Melville</td>
        <td>Moby-Dick; or, The Whale</td>
        <td>1851</td>
        <td>9781530697908</td>
      </tr>
      <tr>
        <td>Emma Dorothy Eliza Nevitte Southworth</td>
        <td>The Hidden Hand</td>
        <td>1888</td>
        <td>9780813512969</td>
      </tr>
      <tr>
        <td>F. Scott Fitzgerald</td>
        <td>The Great Gatsby</td>
        <td>1925</td>
        <td>9780743273565</td>
      </tr>
      <tr>
        <td>George Orwell</td>
        <td>Nineteen Eighty-Four</td>
        <td>1948</td>
        <td>9780451524935</td>
      </tr>
      <tr>
        <td>Nnedi Okorafor</td>
        <td>Who Fears Death</td>
        <td>2010</td>
        <td>9780756406691</td>
      </tr>
    </tbody>
  </table>
</div>

<h2>Row Headers</h2>

<div role="region" aria-labelledby="HeadersCol" tabindex="0" class="rowheaders">
  <table>
    <caption id="HeadersCol">Books with a Fixed Row Header Column</caption>
    <thead>
      <tr>
        <th>Author</th>
        <th>Title</th>
        <th>Year</th>
        <th>ISBN-13</th>
        <th>ISBN-10</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <th scope="row">Miguel De Cervantes</th>
        <td>The Ingenious Gentleman Don Quixote of La Mancha</td>
        <td>1605</td>
        <td>9783125798502</td>
        <td>3125798507</td>
      </tr>
      <tr>
        <th scope="row">Gabrielle-Suzanne Barbot de Villeneuve</th>
        <td>La Belle et la Bête</td>
        <td>1740</td>
        <td>9781910880067</td>
        <td>191088006X</td>
      </tr>
      <tr>
        <th scope="row">Sir Isaac Newton</th>
        <td>The Method of Fluxions and Infinite Series: With Its Application to the Geometry of Curve-lines</td>
        <td>1763</td>
        <td>9781330454862</td>
        <td>1330454863</td>
      </tr>
      <tr>
        <th scope="row">Mary Shelley</th>
        <td>Frankenstein; or, The Modern Prometheus</td>
        <td>1818</td>
        <td>9781530278442</td>
        <td>1530278449</td>
      </tr>
      <tr>
        <th scope="row">Herman Melville</th>
        <td>Moby-Dick; or, The Whale</td>
        <td>1851</td>
        <td>9781530697908</td>
        <td>1530697905</td>
      </tr>
      <tr>
        <th scope="row">Emma Dorothy Eliza Nevitte Southworth</th>
        <td>The Hidden Hand</td>
        <td>1888</td>
        <td>9780813512969</td>
        <td>0813512964</td>
      </tr>
      <tr>
        <th scope="row">F. Scott Fitzgerald</th>
        <td>The Great Gatsby</td>
        <td>1925</td>
        <td>9780743273565</td>
        <td>0743273567</td>
      </tr>
      <tr>
        <th scope="row">George Orwell</th>
        <td>Nineteen Eighty-Four</td>
        <td>1948</td>
        <td>9780451524935</td>
        <td>0451524934</td>
      </tr>
      <tr>
        <th scope="row">Nnedi Okorafor</th>
        <td>Who Fears Death</td>
        <td>2010</td>
        <td>9780756406691</td>
        <td>0756406692</td>
      </tr>
    </tbody>
  </table>
</div>

<h2>Scrolling in Two Directions</h2>

<div role="region" aria-labelledby="HeadersRowCol" tabindex="0" class="rowheaders">
  <table>
    <caption id="HeadersRowCol">Books with a Fixed Row Header Column</caption>
    <thead>
      <tr>
        <th>Author</th>
        <th>Title</th>
        <th>Year</th>
        <th>ISBN-13</th>
        <th>ISBN-10</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <th scope="row">Miguel De Cervantes</th>
        <td>The Ingenious Gentleman Don Quixote of La Mancha</td>
        <td>1605</td>
        <td>9783125798502</td>
        <td>3125798507</td>
      </tr>
      <tr>
        <th scope="row">Gabrielle-Suzanne Barbot de Villeneuve</th>
        <td>La Belle et la Bête</td>
        <td>1740</td>
        <td>9781910880067</td>
        <td>191088006X</td>
      </tr>
      <tr>
        <th scope="row">Sir Isaac Newton</th>
        <td>The Method of Fluxions and Infinite Series: With Its Application to the Geometry of Curve-lines</td>
        <td>1763</td>
        <td>9781330454862</td>
        <td>1330454863</td>
      </tr>
      <tr>
        <th scope="row">Mary Shelley</th>
        <td>Frankenstein; or, The Modern Prometheus</td>
        <td>1818</td>
        <td>9781530278442</td>
        <td>1530278449</td>
      </tr>
      <tr>
        <th scope="row">Herman Melville</th>
        <td>Moby-Dick; or, The Whale</td>
        <td>1851</td>
        <td>9781530697908</td>
        <td>1530697905</td>
      </tr>
      <tr>
        <th scope="row">Emma Dorothy Eliza Nevitte Southworth</th>
        <td>The Hidden Hand</td>
        <td>1888</td>
        <td>9780813512969</td>
        <td>0813512964</td>
      </tr>
      <tr>
        <th scope="row">F. Scott Fitzgerald</th>
        <td>The Great Gatsby</td>
        <td>1925</td>
        <td>9780743273565</td>
        <td>0743273567</td>
      </tr>
      <tr>
        <th scope="row">George Orwell</th>
        <td>Nineteen Eighty-Four</td>
        <td>1948</td>
        <td>9780451524935</td>
        <td>0451524934</td>
      </tr>
      <tr>
        <th scope="row">Nnedi Okorafor</th>
        <td>Who Fears Death</td>
        <td>2010</td>
        <td>9780756406691</td>
        <td>0756406692</td>
      </tr>
    </tbody>
  </table>
</div>






<p>
  Used in the blog post <a href="https://adrianroselli.com/2020/01/fixed-table-headers.html"><cite>Fixed Table Headers</cite></a> and <a href="https://adrianroselli.com/2021/07/scroll-snap-challenges.html"><cite>Scroll Snap Challenges</cite></a>.
</p>
              
            
!

CSS

              
                body {
  font-family: "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto,
    Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
  line-height: 1.4;
  color: #333;
  background-color: #fff;
  padding: 0 5vw;
}

/* Standard Tables */

table {
  margin: 1em 0;
  border-collapse: collapse;
  border: 0.1em solid #d6d6d6;
}

caption {
  text-align: left;
  font-style: italic;
  padding: 0.25em 0.5em 0.5em 0.5em;
}

th,
td {
  padding: 0.25em 0.5em 0.25em 1em;
  vertical-align: text-top;
  text-align: left;
  text-indent: -0.5em;
  scroll-snap-align: start;
}

th {
  vertical-align: bottom;
  background-color: #666;
  color: #fff;
}

tr:nth-child(even) th[scope=row] {
  background-color: #f2f2f2;
}

tr:nth-child(odd) th[scope=row] {
  background-color: #fff;
}

tr:nth-child(even) {
  background-color: rgba(0, 0, 0, 0.05);
}

tr:nth-child(odd) {
  background-color: rgba(255, 255, 255, 0.05);
}

td:nth-of-type(2) {
  font-style: italic;
}

th:nth-of-type(3),
td:nth-of-type(3) {
  text-align: right;
}

/* Fixed Headers */

th {
  position: -webkit-sticky;
  position: sticky;
  top: 0;
  z-index: 2;
}

th[scope=row] {
  position: -webkit-sticky;
  position: sticky;
  left: 0;
  z-index: 1;
}

th[scope=row] {
  vertical-align: top;
  color: inherit;
  background-color: inherit;
/*   background: linear-gradient(90deg, transparent 0%, transparent calc(100% - .09em), #d6d6d6 calc(100% - .09em), #d6d6d6 100%); */
}

th:not([scope=row]):first-child {
  left: 0;
  z-index: 3;
/*   background: linear-gradient(90deg, #666 0%, #666 calc(100% - .09em), #ccc calc(100% - .09em), #ccc 100%); */
}

th[scope="row"]::after {
  content: "";
  position: absolute;
/*   inset-inline: calc(100% - .1em) 0; */
  inset-inline-end: 0;
  inset-block: 0;
  border-inline-end: .05em solid #d6d6d6;
}

/* Scrolling wrapper */

div[tabindex="0"][aria-labelledby][role="region"] {
  overflow: auto;
  scroll-snap-type: both mandatory;
  max-width: 40em;
}

div[tabindex="0"][aria-labelledby][role="region"]:focus {
  box-shadow: 0 0 .5em rgba(0,0,0,.5);
  outline: .1em solid rgba(0,0,0,.1);
}

div[tabindex="0"][aria-labelledby][role="region"] table {
  margin: 0;
}

div[tabindex="0"][aria-labelledby][role="region"].rowheaders {
  background:
    linear-gradient(to right, transparent 30%, rgba(255,255,255,0)),
    linear-gradient(to right, rgba(255,255,255,0), white 70%) 0 100%,
    radial-gradient(farthest-side at 0% 50%, rgba(0,0,0,0.2), rgba(0,0,0,0)),
    radial-gradient(farthest-side at 100% 50%, rgba(0,0,0,0.2), rgba(0,0,0,0)) 0 100%;
  background-repeat: no-repeat;
  background-color: #fff;
  background-size: 4em 100%, 4em 100%, 1.4em 100%, 1.4em 100%;
  background-position: 0 0, 100%, 0 0, 100%;
  background-attachment: local, local, scroll, scroll;
}

div[tabindex="0"][aria-labelledby][role="region"].colheaders {
	background:
		linear-gradient(white 30%, rgba(255,255,255,0)),
		linear-gradient(rgba(255,255,255,0), white 70%) 0 100%,
		radial-gradient(farthest-side at 50% 0, rgba(0,0,0,.2), rgba(0,0,0,0)),
		radial-gradient(farthest-side at 50% 100%, rgba(0,0,0,.2), rgba(0,0,0,0)) 0 100%;
	background-repeat: no-repeat;
	background-color: #fff;
	background-size: 100% 4em, 100% 4em, 100% 1.4em, 100% 1.4em;
	background-attachment: local, local, scroll, scroll;
}

/* Strictly for making the scrolling happen. */

th[scope=row] {
  min-width: 40vw;
}

@media all and (min-width: 30em) {
  th[scope=row] {
    min-width: 20em;
  }
}
  
th[scope=row] + td {
  min-width: 24em;
}

div[tabindex="0"][aria-labelledby][role="region"]:nth-child(4) {
  max-height: 14em;
}

div[tabindex="0"][aria-labelledby][role="region"]:nth-child(8) {
  max-height: 14em;
  margin: 0 1em;
}
              
            
!

JS

              
                
              
            
!
999px

Console