Click here to Skip to main content
15,879,326 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 370.9K   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

 
AnswerRe: How to set width & heigth Pin
Talha Ashfaque24-Sep-12 3:57
Talha Ashfaque24-Sep-12 3:57 
GeneralNice Work Pin
Shahib71330-Sep-10 10:17
Shahib71330-Sep-10 10:17 
GeneralMy vote of 4 Pin
manas sinha15-Jul-10 19:44
manas sinha15-Jul-10 19:44 
GeneralSupport for Firefox and Chrome PinPopular
Arne Møller4-Aug-09 4:51
Arne Møller4-Aug-09 4:51 
GeneralRe: Support for Firefox and Chrome [modified] Pin
Keith Worden9-Jul-10 4:56
Keith Worden9-Jul-10 4:56 
GeneralFreezing multiple columns PinPopular
Søren Christensen22-Apr-09 22:02
Søren Christensen22-Apr-09 22:02 
GeneralRe: Freezing multiple columns Pin
oxid2-Jun-09 22:51
oxid2-Jun-09 22:51 
GeneralRe: Freezing multiple columns Pin
cyguard26-Aug-09 5:05
cyguard26-Aug-09 5:05 
QuestionDoes anyone have a demo posted somewhere that works on Firefox & Safari? Pin
tactics233710-Apr-09 9:05
tactics233710-Apr-09 9:05 
AnswerRe: Does anyone have a demo posted somewhere that works on Firefox & Safari? Pin
tactics233710-Apr-09 12:08
tactics233710-Apr-09 12:08 
GeneralRe: Does anyone have a demo posted somewhere that works on Firefox & Safari? Pin
stuti IT8-Dec-10 2:38
stuti IT8-Dec-10 2:38 
GeneralNot working Pin
saulo benigno28-Jan-09 8:31
saulo benigno28-Jan-09 8:31 
QuestionEvents not firing with fixed columns [modified] Pin
srinian30-Sep-08 11:48
srinian30-Sep-08 11:48 
AnswerRe: Events not firing with fixed columns Pin
Member 796008327-May-11 6:35
Member 796008327-May-11 6:35 
GeneralRe: Events not firing with fixed columns Pin
Member 291225510-Apr-15 11:45
Member 291225510-Apr-15 11:45 
GeneralCheckboxes in Scrollable Table Pin
kusseal3-May-07 22:01
kusseal3-May-07 22:01 
GeneralRe: Checkboxes in Scrollable Table Pin
dubbele onzin29-Oct-07 9:59
dubbele onzin29-Oct-07 9:59 
GeneralRe: Checkboxes in Scrollable Table Pin
Member 1018685024-Nov-14 20:31
Member 1018685024-Nov-14 20:31 
JokeFreezing Multiple Columns Pin
sivad117825-Oct-06 14:14
sivad117825-Oct-06 14:14 
GeneralRe: Freezing Multiple Columns Pin
Hayw1r326-Nov-07 12:47
Hayw1r326-Nov-07 12:47 
GeneralRe: Freezing Multiple Columns Pin
pete in atlanta17-Dec-07 2:24
pete in atlanta17-Dec-07 2:24 
GeneralRe: Freezing Multiple Columns Pin
David Barraclough1-Oct-08 2:52
David Barraclough1-Oct-08 2:52 
Questionwrapping cell text Pin
thedrums16-Oct-06 8:27
thedrums16-Oct-06 8:27 
GeneralWorking In Most Browsers [modified] Pin
Kenneth Bogert5-Oct-06 21:02
Kenneth Bogert5-Oct-06 21:02 
GeneralRe: Working In Most Browsers Pin
Keith Worden9-Jul-10 5:55
Keith Worden9-Jul-10 5:55 

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.