Click here to Skip to main content
Licence CPOL
First Posted 24 Jun 2006
Views 147,819
Downloads 2,780
Bookmarked 47 times

Fixed headers in large HTML tables

By | 24 Jun 2006 | Article
There are quite a lot of ways to fix the header column and rows in HTML tables. But when tables become larger, most of them are not useful because scrolling gets far too slow. In this article, I will show an applicable way for IE.

Introduction

There are quite a lot of ways to fix the header column and rows in HTML tables. But when tables become larger, most of them are not useful because scrolling gets far too slow. In the following sample, I will show an applicable way for IE.

My HTML page contains two divs and a table.

...
<div id="outerDiv">
 <div id="innerDiv">
  <table>
   ...
  </table>
 </div>
</div>
...

In my sample, the table looks like this (the red border shows the innerDiv):

Sample screenshot

The main idea is to copy the innerDiv with the table three times so that there is a div each for the header row, the header column, the first cell in the header row, and the body of the table.

  • In the first three divs, overflow must be set to hidden.
  • In the body div, overflow can be set to scroll if the body is larger then the available space. Furthermore, the table in the body div needs to be positioned absolutely. Top and Left positions have to be negative (Top = -height of header row, Left = -width of header column) so that the headers are no more visible than the body div.

By copying the whole table, all rows and columns will have equal width and height. If you would only copy the first row of the table to the header div, column width in the header could differ from the body columns. After copying the divs, my outerDiv contains four innerDivs (the red borders show the innerDivs):

Sample screenshot

Finally, the divs have to scroll synchronously. When you scroll to the right, the header row has to move to the right too, and when you scroll to the bottom, the header column needs to move too. I found a nice way to do this here.

Sample screenshot

You can view an online demo here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Karin Huber

Software Developer
software architects
Austria Austria

Member

Hi, my name is Karin Huber. Since 1998 I have been working as a developer and IT consultant focusing on building database oriented web applications. In 2007 my friend Rainer and I decided that we want to build a business based on COTS (component off-the-shelf) software. As a result we founded "software architects".
 
These days we are offering our first version of the time tracking software called 'time cockpit'. You can find more information at www.timecockpit.com.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralCheckboxes in Scrollable Table Pinmemberkusseal22:01 3 May '07  
GeneralRe: Checkboxes in Scrollable Table Pinmemberdubbele onzin9:59 29 Oct '07  
JokeFreezing Multiple Columns Pinmembersivad117814:14 25 Oct '06  
GeneralRe: Freezing Multiple Columns PinmemberHayw1r312:47 26 Nov '07  
GeneralRe: Freezing Multiple Columns Pinmemberpete in atlanta2:24 17 Dec '07  
GeneralRe: Freezing Multiple Columns PinmemberDavid Barraclough2:52 1 Oct '08  
Questionwrapping cell text Pinmemberthedrums8:27 16 Oct '06  
This solution is excellent! I have been trying to do this for some time. I was wondering about wrapping the text in the table cells, however. Before implementing your solution, my table looked very nice but was far less usable due to the non-fixed headers. Now my table works great but it contains some incredibly wide cells because the text is not wrapping.
 
I admit I am not a javascript expert by any means. Is there a relatively easy way to fix this and still maintain the fixed headers?
 
Thanks so much.
 
Andy
GeneralWorking In Most Browsers [modified] PinmemberKenneth Bogert21:02 5 Oct '06  
GeneralRe: Working In Most Browsers PinmemberKeith Worden5:55 9 Jul '10  
GeneralRe: Working In Most Browsers Pinmemberhec7822:13 27 Dec '10  
GeneralWorking in Firefox + IE PinmemberKenneth BOgert11:45 11 Aug '06  
GeneralRe: Working in Firefox + IE Pinmemberchathuraka19:18 7 Jun '07  
GeneralProblem when using Ctrl-F to find PinmemberTHEDI18:07 8 Aug '06  
GeneralDisplay row using javascript Pinmemberadril1qqww6:53 25 Jul '06  
GeneralCopying tables Pinmemberjasongilley7:44 4 Jul '06  
GeneralRe: Copying tables PinmemberDwight Johnson2:56 5 Jul '06  
GeneralRe: Copying tables PinmemberKarin Huber7:20 5 Jul '06  
GeneralRe: Copying tables Pinmemberjasongilley8:51 5 Jul '06  
GeneralRe: Copying tables PinmemberKarin Huber3:02 6 Jul '06  
GeneralDuplicating the entire table PinmemberAshley van Gerven7:20 27 Jun '06  
GeneralRe: Duplicating the entire table PinmemberKarin Huber5:52 29 Jun '06  
GeneralRe: Duplicating the entire table PinmemberAshley van Gerven13:43 29 Jun '06  
Questionfirefox solution ? Pinmemberwebber1234566:43 26 Jun '06  
AnswerRe: firefox solution ? PinmemberAshley van Gerven23:48 26 Jun '06  
GeneralRe: firefox solution ? Pinmemberwebber1234566:51 27 Jun '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120529.1 | Last Updated 25 Jun 2006
Article Copyright 2006 by Karin Huber
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid