Click here to Skip to main content
15,887,683 members
Articles / Programming Languages / Javascript
Article

Fixed headers in large HTML tables

Rate me:
Please Sign up or sign in to vote.
4.76/5 (34 votes)
24 Jun 2006CPOL2 min read 371.4K   10.7K   53   73
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.

HTML
...
<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)


Written By
Software Developer software architects
Austria Austria
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.

Comments and Discussions

 
GeneralRe: Working In Most Browsers Pin
hec7827-Dec-10 22:13
hec7827-Dec-10 22:13 
GeneralWorking in Firefox + IE Pin
Kenneth Bogert11-Aug-06 11:45
Kenneth Bogert11-Aug-06 11:45 
GeneralRe: Working in Firefox + IE Pin
chathuraka7-Jun-07 19:18
chathuraka7-Jun-07 19:18 
GeneralProblem when using Ctrl-F to find Pin
THEDI8-Aug-06 18:07
THEDI8-Aug-06 18:07 
GeneralDisplay row using javascript Pin
adril1qqww25-Jul-06 6:53
adril1qqww25-Jul-06 6:53 
GeneralCopying tables Pin
jasongilley4-Jul-06 7:44
jasongilley4-Jul-06 7:44 
GeneralRe: Copying tables Pin
Dwight Johnson5-Jul-06 2:56
Dwight Johnson5-Jul-06 2:56 
GeneralRe: Copying tables Pin
Karin Huber5-Jul-06 7:20
Karin Huber5-Jul-06 7:20 
GeneralRe: Copying tables Pin
jasongilley5-Jul-06 8:51
jasongilley5-Jul-06 8:51 
GeneralRe: Copying tables Pin
Karin Huber6-Jul-06 3:02
Karin Huber6-Jul-06 3:02 
GeneralDuplicating the entire table Pin
Ashley van Gerven27-Jun-06 7:20
Ashley van Gerven27-Jun-06 7:20 
GeneralRe: Duplicating the entire table Pin
Karin Huber29-Jun-06 5:52
Karin Huber29-Jun-06 5:52 
GeneralRe: Duplicating the entire table Pin
Ashley van Gerven29-Jun-06 13:43
Ashley van Gerven29-Jun-06 13:43 
Questionfirefox solution ? Pin
webber12345626-Jun-06 6:43
webber12345626-Jun-06 6:43 
AnswerRe: firefox solution ? Pin
Ashley van Gerven26-Jun-06 23:48
Ashley van Gerven26-Jun-06 23:48 
GeneralRe: firefox solution ? Pin
webber12345627-Jun-06 6:51
webber12345627-Jun-06 6:51 
GeneralRe: firefox solution ? Pin
Ashley van Gerven27-Jun-06 7:04
Ashley van Gerven27-Jun-06 7:04 
GeneralRe: firefox solution ? Pin
webber12345627-Jun-06 9:44
webber12345627-Jun-06 9:44 
AnswerRe: firefox solution ? [modified] Pin
Karin Huber29-Jun-06 5:57
Karin Huber29-Jun-06 5:57 
GeneralQuestion ? firefox solution ? Pin
test-20060720-Jun-07 23:42
test-20060720-Jun-07 23:42 
GeneralSetup for multiple tables - the only issue I am having is that if you scroll to the bottom or right then make the window full size, it does not resync the scroll . . .hmmm Pin
oakgoat9-Oct-10 16:13
oakgoat9-Oct-10 16:13 
GeneralRe: Setup for multiple tables - the only issue I am having is that if you scroll to the bottom or right then make the window full size, it does not resync the scroll . . .hmmm Pin
Member 796008327-May-11 6:34
Member 796008327-May-11 6:34 

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

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