Click here to Skip to main content
Licence CPOL
First Posted 24 Jun 2006
Views 147,744
Downloads 2,777
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
QuestionFix mutliple columns PinmemberMember 897905823:50 24 May '12  
BugERROR PinmemberBudi Arsana7:13 24 May '12  
QuestionCan u provide me code for fixed header a Pinmemberanandkbpalle17:06 14 Feb '12  
QuestionMy "CreateScrollHeader" is called only from page "reload" function in code, not from "refresh" button in browser PinmemberMember 80271315:27 23 Jun '11  
Answer"iframe" and "frame" are the reason. PinmemberMember 80271315:14 29 Jun '11  
GeneralRe: But "search" and "checkbox" are not working PinmemberMember 80271315:17 29 Jun '11  
QuestionHow to set width & heigth PinmemberMember 77987295:15 30 Mar '11  
AnswerRe: How to set width & heigth PinmemberMember 779872921:25 30 Mar '11  
GeneralNice Work PinmemberShahib71310:17 30 Sep '10  
GeneralMy vote of 4 Pinmembermanas sinha19:44 15 Jul '10  
GeneralSupport for Firefox and Chrome PinmemberArne Møller4:51 4 Aug '09  
GeneralRe: Support for Firefox and Chrome [modified] PinmemberKeith Worden4:56 9 Jul '10  
GeneralFreezing multiple columns PinPopularmemberSøren Christensen22:02 22 Apr '09  
GeneralRe: Freezing multiple columns Pinmemberoxid22:51 2 Jun '09  
GeneralRe: Freezing multiple columns Pinmembercyguard5:05 26 Aug '09  
QuestionDoes anyone have a demo posted somewhere that works on Firefox & Safari? Pinmembertactics23379:05 10 Apr '09  
AnswerRe: Does anyone have a demo posted somewhere that works on Firefox & Safari? Pinmembertactics233712:08 10 Apr '09  
GeneralRe: Does anyone have a demo posted somewhere that works on Firefox & Safari? Pinmemberstutid2:38 8 Dec '10  
GeneralNot working Pinmembersaulo benigno8:31 28 Jan '09  
QuestionEvents not firing with fixed columns [modified] Pinmembersrinian11:48 30 Sep '08  
AnswerRe: Events not firing with fixed columns PinmemberMember 79600836:35 27 May '11  
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  
Great piece of work here thanks! Does anyone have any ideas on altering this code so that you could freeze multiple left hand side columns? Haven't figured out how to do that using this solution yet.
 
Thanks!

GeneralRe: Freezing Multiple Columns PinmemberHayw1r312:47 26 Nov '07  

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