JSFiddle - React, Tailwind, and code Playground
HTML
<table class = "wdtscroll">
<tr>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Maria Anders</td>
<td>Germany</td>
<td>Alfreds Futterkiste</td>
<td>Maria Anders</td>
<td>Germany</td>
<td>Alfreds Futterkiste</td>
<td>Maria Anders</td>
<td>Germany</td>
<td>Alfreds Futterkiste</td>
<td>Maria Anders</td>
<td>Germany</td>
</tr>
<tr>
<td>Berglunds snabbköp</td>
<td>Christina Berglund</td>
<td>Sweden</td>
<td>Berglunds snabbköp</td>
<td>Christina Berglund</td>
<td>Sweden</td>
<td>Berglunds snabbköp</td>
<td>Christina Berglund</td>
<td>Sweden</td>
<td>Berglunds snabbköp</td>
<td>Christina Berglund</td>
<td>Sweden</td>
</tr>
<tr>
<td>Centro comercial Moctezuma</td>
<td>Francisco Chang</td>
<td>Mexico</td>
<td>Centro comercial Moctezuma</td>
<td>Francisco Chang</td>
<td>Mexico</td>
<td>Berglunds snabbköp</td>
<td>Christina Berglund</td>
<td>Sweden</td>
<td>Berglunds snabbköp</td>
<td>Christina Berglund</td>
<td>Sweden</td>
</tr>
<tr>
<td>Ernst Handel</td>
<td>Roland Mendel</td>
<td>Austria</td>
<td>Ernst Handel</td>
<td>Roland Mendel</td>
<td>Austria</td>
<td>Berglunds snabbköp</td>
<td>Christina Berglund</td>
<td>Sweden</td>
<td>Berglunds snabbköp</td>
<td>Christina Berglund</td>
<td>Sweden</td>
</tr>
<tr>
<td>Island Trading</td>
<td>Helen Bennett</td>
<td>UK</td>
<td>Ernst Handel</td>
<td>Roland Mendel</td>
<td>Austria</td>
<td>Berglunds snabbköp</td>
<td>Christina Berglund</td>
<td>Sweden</td>
<td>Berglunds snabbköp</td>
<td>Christina Berglund</td>
<td>Sweden</td>
...
CSS
.wdtscroll {
display: block;
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
border-collapse: collapse;
overflow-x: auto;
max-height: 300px;
white-space: nowrap;
}
.wdtscroll td {
max-width: 20vw;
background-color: white;
overflow: hidden;
}
.wdtscroll td, .wdtscroll th {
border: 1px solid #ddd;
padding: 8px;
}
.wdtscroll tr:nth-child(even) td { background-color: #f2f2f2; }
.wdtscroll tr:hover td { background-color: #ddd; }
.wdtscroll th {
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
background-color: orange;
color: grey;
position: sticky;
top: 0px;
}
.wdtscroll td:nth-child(1) {
position: sticky;
left: -1px;
top: 0px;
transition: 1s;
}
.wdtscroll td:nth-child(2) {
position: sticky;
left: 30px;
}
.wdtscroll th:nth-child(1) {
position: sticky;
top: 0px;
left: 0px;
z-index: 5;
}
.wdtscroll th:nth-child(2) {
position: sticky;
top: 0px;
left: 30px;
z-index: 6;
}
/* styles for when the header is in sticky mode */
.wdtscroll.isSticky tr td:nth-child(1) {
background-color: salmon;
}
.wdtscroll.isSticky tr td:nth-child(2) {
background-color: pink;
}
JavaScript
// get the sticky element
const stickyElm = document.querySelector('.wdtscroll td')
const tableElm = document.querySelector('.wdtscroll')
const observer = new IntersectionObserver(
([e]) => {
tableElm.classList.toggle('isSticky', e.intersectionRatio < 1)
},
{threshold: [1]}
);
window.onload = function(){
observer.observe(stickyElm)
}