Easy sticky header, columns and footers
With modern browsers it is easy to do sticky tables items, no javascript needed.
by Plippie Plop
HTML
<p>
Resize screen width to allow horizontal scroll
</p>
<div class="container">
<table>
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<tr>
<td>Tiger Nixon</td>
<td>System Architect</td>
<td>Edinburgh</td>
<td>61</td>
<td>2011/04/25</td>
<td>$320,800</td>
</tr>
<tr>
<td>Garrett Winters</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>63</td>
<td>2011/07/25</td>
<td>$170,750</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>Junior Technical Author</td>
<td>San Francisco</td>
<td>66</td>
<td>2009/01/12</td>
<td>$86,000</td>
</tr>
<tr>
<td>Cedric Kelly</td>
<td>Senior Javascript Developer</td>
<td>Edinburgh</td>
<td>22</td>
<td>2012/03/29</td>
<td>$433,060</td>
</tr>
<tr>
<td>Airi Satou</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>33</td>
<td>2008/11/28</td>
<td>$162,700</td>
</tr>
<tr>
<td>Brielle Williamson</td>
<td>Integration Specialist</td>
<td>New York</td>
<td>61</td>
<td>2012/12/02</td>
<td>$372,000</td>
</tr>
<tr>
<td>Herrod Chandler</td>
<td>Sales Assistant</td>
<td>San Francisco</td>
<td>59</td>
<td>2012/08/06</td>
<td>$137,500</td>
</tr>
<tr>
<td>Rhona Davidson</td>
<td>Integration Specialist</td>
<td>Tokyo</td>
<td>55</td>
<td>2010/10/14</td>
<td>$327,900</td>
</tr>
<tr>
<td>Colleen Hurst</td>
<td>Javascript Developer</td>
<td>San Francisco</td>
<td>39</td>
<td>2009/09/15</td>
<td>$205,500</td>
</tr>
<tr>
<td>Sonya...
CSS
thead,
tfoot {
/* cell container need relative position */
position: relative;
}
/* sticky header */
thead th {
/* ios devices */
position: -webkit-sticky;
position: sticky;
left: 0;
top: 0;
/* index order is needed for the different sticky levels */
z-index: 1;
}
/* sticky first column */
thead th:first-of-type {
top: 0;
left: 0;
z-index: 3;
}
tbody td:first-of-type {
position: -webkit-sticky;
position: sticky;
left: 0;
z-index: 1;
/* sitcky TD/TH need background color */
background-color: white;
/* adding right borde / border gets hidden when scrol so use box-shadow: */
box-shadow: 2px 0 0 -1px gray;
}
/* sticky footer */
tfoot th {
position: sticky;
bottom: 0;
}
tfoot th:first-of-type {
left: 0;
z-index: 3;
}
/* default styling */
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap');
* {
box-sizing: border-box;
}
:root {
--table-heading: #5D6D7E;
--table-even: #D5D8DC;
}
body{
font-family: Roboto, Sans-Serif;
}
.container {
margin: 1em;
border: 1px solid grey;
display: flex;
max-height: 50vh;
overflow: auto;
}
table {
position: relative;
width: 100%;
overflow-x: scroll;
padding: 0;
margin: 0;
border-spacing: 0;
}
table thead th,
table tfoot th {
background-color: var(--table-heading);
color: white;
}
table td,
table th {
padding: 0.5em;
text-align: left;
margin: 0;
border: 0;
border-spacing: 0;
white-space: nowrap;
}
table tr:nth-child(even) td {
background-color: var(--table-even);
}