Posted By: Anonymous
I want to make an html table with the top row frozen (so when you scroll down vertically you can always see it).
Note that I do NOT need the left column frozen.
This is called Fixed Header Scrolling. There are a number of documented approaches:
There are a number of gotchyas with any approach you take, especially concerning cross browser/version support.
Even if it’s not the header you want to fix, but the first row of data, the concept is still the same. I wasn’t 100% which you were referring to.
I was tasked by my company to research a solution for this that could function in IE7+, Firefox, and Chrome.
After many moons of searching, trying, and frustration it really boiled down to a fundamental problem. For the most part, in order to gain the fixed header, you need to implement fixed height/width columns because most solutions involve using two separate tables, one for the header which will float and stay in place over the second table that contains the data.
//float this one right over second table <table> <tr> <th>Header 1</th> <th>Header 2</th> </tr> </table> <table> //Data </table>
An alternative approach some try is utilize the tbody and thead tags but that is flawed too because IE will not allow you put a scrollbar on the tbody which means you can’t limit its height (so stupid IMO).
<table> <thead style="do some stuff to fix its position"> <tr> <th>Header 1</th> <th>Header 2</th> </tr> </thead> <tbody style="No scrolling allowed here!"> Data here </tbody> </table>
This approach has many issues such as ensures EXACT pixel widths because tables are so cute in that different browsers will allocate pixels differently based on calculations and you simply CANNOT (AFAIK) guarantee that the distribution will be perfect in all cases. It becomes glaringly obvious if you have borders within your table.
I took a different approach and said screw tables since you can’t make this guarantee. I used divs to mimic tables. This also has issues of positioning the rows and columns (mainly because floating has issues, using in-line block won’t work for IE7, so it really left me with using absolute positioning to put them in their proper places).
There is someone out there that made the Slick Grid which has a very similar approach to mine and you can use and a good (albeit complex) example for achieving this.