Is there a cross-browser CSS/JavaScript technique to display a long HTML table such that the column headers stay fixed on-screen and do not scroll with the table body. Think of the "freeze panes" effect in Microsoft Excel.
I want to be able to scroll through the contents of the table, but to always be able to see the column headers at the top.
I've just completed putting together a jQuery plugin that will take valid single table using valid HTML (have to have a thead and tbody) and will output a table that has fixed headers, optional fixed footer that can either be a cloned header or any content you chose (pagination, etc.). If you want to take advantage of larger monitors it will also resize the table when the browser is resized. Another added feature is being able to side scroll if the table columns can not all fit in view.
http://fixedheadertable.com/
on github: http://markmalek.github.com/Fixed-Header-Table/
It's extremely easy to setup and you can create your own custom styles for it. It also uses rounded corners in all browsers. Keep in mind I just released it, so it's still technically beta and there are very few minor issues I'm ironing out.
It works in Internet Explorer 7, Internet Explorer 8, Safari, Firefox and Chrome.
The CSS property
position: sticky
has great support in most modern browsers (I had issues with Edge, see below).This lets us solve the problem of fixed headers quite easily:
Safari needs a vendor prefix:
-webkit-sticky
.For Firefox, I had to add
min-height: 0
to one the parent elements. I forget exactly why this was needed.Most unfortunately, the Microsoft Edge implementation seems to be only semi-working. At least, I had some flickering and misaligned table cells in my testing. The table was still usable, but had significant aesthetic issues.
Support for fixed footer
I extended Nathan's function to also support a fixed footer and maximum height. Also, the function will set the CSS itself, and you only have to support a width.
Usage:
Fixed height:
Maximum height (if the browser supports the CSS 'max-height' option):
Script: