Click here to Skip to main content
Licence CPOL
First Posted 24 Jun 2006
Views 148,043
Downloads 2,802
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
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  
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  
This is what I want to do but I'm not sure how to implement what you suggest.   I have 3 'title cells' to freeze on each row (the ones with class="reportRowHeader").   Here is the HTML section that is working now to freeze 1 cell on each row.   Can you give me an example in this section to freeze the 3 columns?   If you could give an example of how to do that with a dynamic number of columns (ie. all reportRowHeader columns) that completely solve my problem.   Thanks.
 
      <div id = "outerDiv">
            <div id = "innerDiv">
                  <table class = "reportTable" cellspacing = "0">
                        <tr>
                              <td class = "reportCorner">&nbsp;</td>
                              <td class = "reportCorner">&nbsp;</td>
                              <td class = "reportCorner">&nbsp;</td>
                              <td class = "reportColumnHeader">Albania</td>
                              <td class = "reportColumnHeader">Argentina</td>
                              <td class = "reportColumnHeader">Australia</td>
                              <td class = "reportColumnHeader">Denmark</td>
                              <td class = "reportColumnHeader">Finland</td>
                              <td class = "reportColumnHeader">Iceland</td>
                              <td class = "reportColumnHeader">Norway</td>
                              <td class = "reportColumnHeader">Sweden</td>
                              <td class = "reportColumnHeader">Number</td>
                              <td class = "reportColumnHeader">Percentage (of the whole sample)</td>
                        </tr>
                        <tr>
                              <td class = "reportRowHeader">Q.4 Legal Framework - What is the legal basis for the following?</td>
                              <td class = "reportRowHeader">&nbsp;</td>
                              <td class = "reportRowHeader">&nbsp;</td>
                              <td class = "reportDataCell">&nbsp;</td>
                              <td class = "reportDataCell">&nbsp;</td>
                              <td class = "reportDataCell">&nbsp;</td>
                              <td class = "reportDataCell">&nbsp;</td>
                              <td class = "reportDataCell">&nbsp;</td>
                              <td class = "reportDataCell">&nbsp;</td>
                              <td class = "reportDataCell">&nbsp;</td>
                              <td class = "reportDataCell">&nbsp;</td>
                              <td class = "reportDataCell">&nbsp;</td>
                              <td class = "reportDataCell">&nbsp;</td>
                        </tr>
                        <tr>
                              <td class = "reportRowHeader">&nbsp;</td>
                              <td class = "reportRowHeader">4.a   The form and structure of the annual budget and related legislation</td>
                              <td class = "reportRowHeader">&nbsp;</td>
                              <td class = "reportDataCell">&nbsp;</td>
                              <td class = "reportDataCell">&nbsp;</td>
                              <td class = "reportDataCell">&nbsp;</td>
                              <td class = "reportDataCell">&nbsp;</td>
                              <td class = "reportDataCell">&nbsp;</td>
                              <td class = "reportDataCell">&nbsp;</td>
                              <td class = "reportDataCell">&nbsp;</td>
                              <td class = "reportDataCell">&nbsp;</td>
                              <td class = "reportDataCell">&nbsp;</td>
                              <td class = "reportDataCell">&nbsp;</td>
                        </tr>
                        <tr>
                              <td class = "reportRowHeader">&nbsp;</td>
                              <td class = "reportRowHeader">&nbsp;</td>
                              <td class = "reportRowHeader">Constitution</td>
                              <td class = "reportDataCell">1</td>
                              <td class = "reportDataCell">&nbsp;</td>
                              <td class = "reportDataCell">1</td>
                              <td class = "reportDataCell">1</td>
                              <td class = "reportDataCell">1</td>
                              <td class = "reportDataCell">&nbsp;</td>
                              <td class = "reportDataCell">&nbsp;</td>
                              <td class = "reportDataCell">&nbsp;</td>
                              <td class = "reportDataCell">4</td>
                              <td class = "reportDataCell">50.0%</td>
                        </tr>
                  </table>
            </div>
      </div>
Questionwrapping cell text Pinmemberthedrums8:27 16 Oct '06  
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  

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