table {
position: relative;
table-layout: auto;
background-color: #aaa;
overflow: hidden;
border-collapse: collapse;
display:block;
width: 100%;
}
/*thead*/
thead {
position: relative;
display: block; /*seperates the header from the body allowing it to be positioned*/
overflow: visible;
}
thead th {
background-color: #99a;
min-width: 120px;
height: 32px;
border: 1px solid #222;
max-width:120px;
}
thead th:nth-child(1) {/*first cell in the header*/
position: relative;
background-color: #88b;
}
thead th:nth-child(2) {/*second cell in the header*/
position: relative;
background-color: #88b;
}
thead th:nth-child(3) {/*third cell in the header*/
position: relative;
background-color: #88b;
}
thead th:nth-child(4) {/*third cell in the header*/
position: relative;
background-color: #88b;
}
/*tbody*/
tbody {
position: relative;
display: block; /*seperates the tbody from the header*/
height: 239px;
overflow: scroll;
}
tbody td {
background-color: #bbc;
min-width: 120px;
border: 1px solid #222;
max-width:120px;
}
tbody tr td:nth-child(1) { /*the first cell in each tr*/
position: relative;
height: 40px;
background-color: #99a;
}
tbody tr td:nth-child(2) { /*the second cell in each tr*/
position: relative;
height: 40px;
background-color: #99a;
}
tbody tr td:nth-child(3) { /*the third cell in each tr*/
position: relative;
height: 40px;
background-color: #99a;
}
tbody tr td:nth-child(4) { /*the third cell in each tr*/
position: relative;
height: 40px;
background-color: #99a;
}
JavaScript
down vote
$(document).ready(function() {
$('tbody').scroll(function(e) { //detect a scroll event on the tbody
/*
Setting the thead left value to the negative valule of tbody.scrollLeft will make it track the movement
of the tbody element. Setting an elements left value to that of the tbody.scrollLeft left makes it maintain it's relative position at the left of the table.
*/
$('thead').css("left", -$("tbody").scrollLeft()); //fix the thead relative to the body scrolling
$('thead th:nth-child(1)').css("left", $("tbody").scrollLeft()); //fix the first cell of the header
$('tbody td:nth-child(1)').css("left", $("tbody").scrollLeft()); //fix the first column of tdbody
$('thead th:nth-child(2)').css("left", $("tbody").scrollLeft()); //fix the second cell of the header
$('tbody td:nth-child(2)').css("left", $("tbody").scrollLeft()); //fix the second column of tdbody
$('thead th:nth-child(3)').css("left", $("tbody").scrollLeft()); //fix the third cell of the header
$('tbody td:nth-child(3)').css("left", $("tbody").scrollLeft()); //fix the third column of tdbody
$('thead th:nth-child(4)').css("left", $("tbody").scrollLeft()); //fix the third cell of the header
$('tbody td:nth-child(4)').css("left", $("tbody").scrollLeft()); //fix the third column of tdbody
});
});
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.