Scrollable HTML Table with fixed header for IE 7, IE 8, Firefox 3.5, Chrome
Web development | Posted: October 26th, 2009 | laskovvA lot of developers face a problem of creating a table with scrollable body and fixed header during their web application development tasks. There are several solutions which suite to different situations.
Oxagile software development company created a new solution which supports both IE7 and IE8 and is also suitable for Firefox 3.5 and Google Chrome.
Here is a demo:
| Column 1 | Column 2 | Column 3 | Column 4 | Column 5 | Column 6 | Column 7 | Column 8 | Column 9 | Column 10 |
|---|
| Cell 1.1 | Cell 1.2 | Cell 1.3 | Cell 1.4 | Cell 1.5 | Cell 1.6 | Cell 1.7 | Cell 1.8 | Cell 1.9 | Cell 1.10 |
| Cell 2.1 | Cell 2.2 | Cell 2.3 | Cell 2.4 | Cell 2.5 | Cell 2.6 | Cell 2.7 | Cell 2.8 | Cell 2.9 | Cell 2.10 |
| Cell 3.1 | Cell 3.2 | Cell 3.3 | Cell 3.4 | Cell 3.5 | Cell 3.6 | Cell 3.7 | Cell 3.8 | Cell 3.9 | Cell 3.10 |
| Cell 4.1 | Cell 4.2 | Cell 4.3 | Cell 4.4 | Cell 4.5 | Cell 4.6 | Cell 4.7 | Cell 4.8 | Cell 4.9 | Cell 4.10 |
| Cell 5.1 | Cell 5.2 | Cell 5.3 | Cell 5.4 | Cell 5.5 | Cell 5.6 | Cell 5.7 | Cell 5.8 | Cell 5.9 | Cell 5.10 |
| Cell 6.1 | Cell 6.2 | Cell 6.3 | Cell 6.4 | Cell 6.5 | Cell 6.6 | Cell 6.7 | Cell 6.8 | Cell 6.9 | Cell 6.10 |
| Cell 7.1 | Cell 7.2 | Cell 7.3 | Cell 7.4 | Cell 7.5 | Cell 7.6 | Cell 7.7 | Cell 7.8 | Cell 7.9 | Cell 7.10 |
| Cell 8.1 | Cell 8.2 | Cell 8.3 | Cell 8.4 | Cell 8.5 | Cell 8.6 | Cell 8.7 | Cell 8.8 | Cell 8.9 | Cell 8.10 |
Our idea is simple:
- We created a mark-up which contains two tables: Table 1 for header and Table 2 for body which look and act as a single table.

- We put both tables into a container which scrolls them horizontally.
- We put Table 2 for body into a container which scrolls it vertically.
- We created a container which simulates the scroll of the body and is positioned in the top right corner of visible content.
- Both tables and all their cells should have fixed width.
- You should always know the quantity of columns beforehand.
- Cells must have fixed height for IE7 (when you use some DOCTYPEs) and you should know the quantity of rows, as in some cases IE7 does not support document.documentElement.clientHeight property.
- Create header and body tables with fixed table and cell width. For example, if you have 10 cells, you should set the width of each cell to 100px and the width of the whole table to 1000px).
- Create a DIV-container, let’s name it “y-scroll”, which wraps Table 2 (the body-table).
- Set the width of the y-scroll container to be equal to the width of Table 2 plus the quantity of columns in Table 2.
For example, if the width of Table 2 is 1000px and the quantity of columns is 10, the width of the container should be 1010px.Note: This amendment takes place only in the case when table border width is set to 1px.
- Set overflow style of “y-scroll” to: x – hidden, y – auto and max-height to be equal to the height of Table 2 visible content.
- Create a DIV-container, let’s name it “x-scroll”, which wraps both tables. Set the width of the scroll to be equal to the width of visible content. Set overflow style to: x – auto, y – hidden.
Now you have a scrollable table, but there are two problems:
- The header table is shifted 1px to the left.
To fix this problem just wrap this table into a DIV-container. Name it “header-container”.
- Vertical scroll appears only after you scroll the whole table horizontally.
To fix this problem create a scroll which simulates the behavior of the real vertical scroll and is positioned at top right corner of Table 2 visible content.
- The header table is shifted 1px to the left.
- Create a DIV-container, let’s name it “fake-y-scroll-container”. Set position of this container to relative. We use this DIV as a container for absolute positioning of fake scroll. Fix the width of the container in the same way as for “x-scroll” and set overflow style to hidden.
- Create a DIV-container, let’s name it “fake-scroll” and put it into the “fake-y-scroll-container”. “Fake-scroll” should be positioned absolutely and have inner content height equal to the height of Table 2.
To set the inner content height to be equal to Table 2 height, create an inner DIV-container, let’s name it “table-emulator” and set its height with JavaScript on page load:
var tableEmulator = document.getElementById(’table-emulator’);var table = document.getElementById(’body’);//Set the height of inner content for fake scrolltableEmulator.style.height = table.clientHeight + “px”;var fakeScrollablePanel = document.getElementById(’fake-scroll’);//Position “fake-scroll” under table header.fakeScrollablePanel.style.top = document.getElementById(’header’).clientHeight + 2 + “px”; - The next step is to synchronize the behavior of the real scroll and the fake scroll. Include the following JavaScript code into the page:
var scrollablePanel = document.getElementById(’y-scroll’);var fakeScrollablePanel = document.getElementById(’fake-scroll’);scrollablePanel.onscroll = function (e){fakeScrollablePanel.scrollTop = scrollablePanel.scrollTop;}fakeScrollablePanel.onscroll = function (e){scrollablePanel.scrollTop = fakeScrollablePanel.scrollTop;}
Restrictions and assumptions.
Development.
To create the mark up above, take the following steps:
That’s all! Play with CSS to improve the look and feel of the table and use it for any website development tasks!
Here is the Scrollable HTML Table with fixed header source code.






Print
Send
17 Responses to “Scrollable HTML Table with fixed header for IE 7, IE 8, Firefox 3.5, Chrome”
October 28th, 2009 at 3:03 am
What a noob, I’ve done a lot better even for all browsers including IE6. Even cell resizing and infinite scroll as well. It was hard but it works.
October 28th, 2009 at 5:30 pm
Thank you for this solution. It has helped a lot.
November 16th, 2009 at 6:20 pm
Ok, Andy:
then show us your (better) example, if you are so proud of it.
December 3rd, 2009 at 8:53 am
Hey, That’s a great solution. But its not helping me for the percentage width table layout
Can you give a solution for percentage table also ?
December 21st, 2009 at 3:20 pm
Ya, Andy – it has been more than one year … do you still have your better example?
February 2nd, 2010 at 4:54 pm
That’s a nice and light solution!
Thank you!
February 4th, 2010 at 2:23 pm
Andy sucks
February 18th, 2010 at 4:10 pm
First this method is currently the best way to make a table scroll w/fixed header, but…
Has anyone tried a scroll wheel with this demo? IE scrolls smooth while Firefox and Chrome are slow/jumpy. And if you use the scroll wheel as a button, it works in IE, Chrome, but not Firefox. Of those who answered ‘yes’, any idea how to fix it?
March 30th, 2010 at 2:18 pm
hello sir I need to table scrolling with fixed header and footer .
how can set header and footer
please suggest
April 5th, 2010 at 12:12 pm
Dear Raj,
If you want to add a fixed footer you should create one more table (for footer content) and put it under div#y-scroll. This table should have fixed cells width as table for header.
Also we’ve added the link to demo and source where left scroll jumping is fixed. Here is the difference between the new and old mark-up:
1. Header, Footer, and Content tables are wrapped with div tags
2. We’ve created one more fake scroll (x-scroll) that we’ve put under all these tables
3. JavaScript synchronizes scrolling of header, footer, content tables and fake scroll.
June 9th, 2010 at 3:09 pm
Dear friends this code is not working in ie6, any help is appreciated.
June 10th, 2010 at 11:56 am
Thank you for the code.
If I give an onclick=javascript function in table, ie supports, but does not work in firefox. Any suggestion? The same work fine in a normal table (no scrolling)
.
.
.
June 11th, 2010 at 7:29 pm
Confirmed this code is working great with Safari 5/Win. Good job.
June 21st, 2010 at 2:31 pm
Madhu, we cannot reproduce your problem on our environment. Could you please give us more exact steps and the example of the code you are using?
June 25th, 2010 at 3:23 pm
Great solution! I’m wondering what would need to be done to make it compatible with IE6 also. Our clients are in the process of migrating from XP/IE6 to W7/IE8 and we need to support the older environment.
August 25th, 2010 at 10:37 am
check this one!
This solution works on all browsers, even IE 6.
You have a flexible table with fixed header..
August 27th, 2010 at 10:53 pm
Strangely I cannot scroll the table with the mouse scroller unless my cursor is right over the scroll bar, even after I click within the table body – IE8 and Firefox 3.6. The one I’ve been using works but I’ve been looking for something better and more compatible with IE8. The scrollable table is a real sore spot in CSS/browsers.
Leave a Reply