I would like to be able to "tweak" an HTML table's presentation to add a single feature: when scrolling down through the page so that the table is on the screen but the header rows are off-screen, I would like the headers to remain visible at the top of the viewing area.
This would be conceptually like the "freeze panes" feature in Excel. However, an HTML page might contain several tables in it and I only would want it to happen for the table that is currently in-view, only while it is in-view.
Note: I've seen one solution where the table data area is made scrollable while the headers do not scroll. That's not the solution I'm looking for.
I've encountered this problem very recently. Unfortunately, I had to do 2 tables, one for the header and one for the body. It's probably not the best approach ever but here goes:
This worked for me, it's probably not the elegant way but it does work. I'll investigate so see if I can do something better, but it allows for multiple tables.
Go read on the overflow propriety to see if it fits your need
Using
display: fixed
on thethead
section should work, but for it only work on the current table in view, you will need the help of JavaScript. And it will be tricky because it will need to figure out scrolling places and location of elements relative to the viewport, which is one of the prime areas of browser incompatibility.Have a look at the popular JavaScript frameworks (jQuery, MooTools, YUI, etc etc.) to see if they can either do what you want or make it easier to do what you want.
Craig, I refined your code a bit (among a few other things it's now using position:fixed) and wrapped it as a jQuery plugin.
Try it out here: http://jsfiddle.net/jmosbech/stFcx/
And get the source here: https://github.com/jmosbech/StickyTableHeaders
If you use a full screen table you are maybe interested in setting th to display:fixed; and top:0; or try a very similar approach via css.
Update
Just quickly build up a working solution with iframes (html4.0). This example IS NOT standard conform, however you will easily be able to fix it:
outer.html
test.html
If you're targeting modern browsers (currently anything but IE) you can use
position:sticky
, which doesn't require JS and won't break the table layout. Nor does it require fixed column width to work properly.Example for a single header row:
For multiple header rows, you can use something like this:
You might need to play a bit with the top property of the last child changing the number of pixels to match the height of the first row (+ the margin + the border + the padding, if any), so the second row sticks just down bellow the first one.
Also both solutions work even if you have more than one table in the same page: the
th
element of each one starts to be sticky when its top position is the one indicated into the css definition and just disappear when all the table scrolls down. So if there are more tables all work beautifully the same way.Browser support: https://caniuse.com/#feat=css-sticky