HTML table with fixed headers? HTML table with fixed headers? javascript javascript

HTML table with fixed headers?


This can be cleanly solved in four lines of code.

If you only care about modern browsers, a fixed header can be achieved much easier by using CSS transforms. Sounds odd, but works great:

  • HTML and CSS stay as-is.
  • No external JavaScript dependencies.
  • Four lines of code.
  • Works for all configurations (table-layout: fixed, etc.).
document.getElementById("wrap").addEventListener("scroll", function(){   var translate = "translate(0,"+this.scrollTop+"px)";   this.querySelector("thead").style.transform = translate;});

Support for CSS transforms is widely available except for Internet Explorer 8-.

Here is the full example for reference: