Click here to Skip to main content
Licence CPOL
First Posted 24 Jun 2006
Views 137,655
Downloads 2,085
Bookmarked 47 times

Fixed headers in large HTML tables

By Karin Huber | 24 Jun 2006
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.
1 vote, 3.7%
1

2
2 votes, 7.4%
3
4 votes, 14.8%
4
20 votes, 74.1%
5
4.67/5 - 27 votes
1 removed
μ 4.31, σa 1.66 [?]

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
QuestionMy "CreateScrollHeader" is called only from page "reload" function in code, not from "refresh" button in browser PinmemberMember 80271316:27 23 Jun '11  
Answer"iframe" and "frame" are the reason. PinmemberMember 80271316:14 29 Jun '11  
GeneralRe: But "search" and "checkbox" are not working PinmemberMember 80271316:17 29 Jun '11  
QuestionHow to set width & heigth PinmemberMember 77987296:15 30 Mar '11  
AnswerRe: How to set width & heigth PinmemberMember 779872922:25 30 Mar '11  
GeneralNice Work PinmemberShahib71311:17 30 Sep '10  
GeneralMy vote of 4 Pinmembermanas sinha20:44 15 Jul '10  
GeneralSupport for Firefox and Chrome PinmemberArne Møller5:51 4 Aug '09  
GeneralRe: Support for Firefox and Chrome [modified] PinmemberKeith Worden5:56 9 Jul '10  
GeneralFreezing multiple columns PinPopularmemberSøren Christensen23:02 22 Apr '09  
GeneralRe: Freezing multiple columns Pinmemberoxid23:51 2 Jun '09  
GeneralRe: Freezing multiple columns Pinmembercyguard6:05 26 Aug '09  
QuestionDoes anyone have a demo posted somewhere that works on Firefox & Safari? Pinmembertactics233710:05 10 Apr '09  
AnswerRe: Does anyone have a demo posted somewhere that works on Firefox & Safari? Pinmembertactics233713:08 10 Apr '09  
GeneralRe: Does anyone have a demo posted somewhere that works on Firefox & Safari? Pinmemberstutid3:38 8 Dec '10  
GeneralNot working Pinmembersaulo benigno9:31 28 Jan '09  
QuestionEvents not firing with fixed columns [modified] Pinmembersrinian12:48 30 Sep '08  
AnswerRe: Events not firing with fixed columns PinmemberMember 79600837:35 27 May '11  
GeneralCheckboxes in Scrollable Table Pinmemberkusseal23:01 3 May '07  
GeneralRe: Checkboxes in Scrollable Table Pinmemberdubbele onzin10:59 29 Oct '07  
JokeFreezing Multiple Columns Pinmembersivad117815:14 25 Oct '06  
GeneralRe: Freezing Multiple Columns PinmemberHayw1r313:47 26 Nov '07  
GeneralRe: Freezing Multiple Columns Pinmemberpete in atlanta3:24 17 Dec '07  
GeneralRe: Freezing Multiple Columns PinmemberDavid Barraclough3:52 1 Oct '08  
Questionwrapping cell text Pinmemberthedrums9:27 16 Oct '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
Web01 | 2.5.120206.1 | Last Updated 25 Jun 2006
Article Copyright 2006 by Karin Huber
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid