Scrollable HTML Table with fixed header for IE 7, IE 8, Firefox 3.5, Chrome

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


Valid XHTML 1.0!

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.

    Table with scrollable body

  • 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.
    1. Both tables and all their cells should have fixed width.
    2. You should always know the quantity of columns beforehand.
    3. 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.
    1. 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).
    2. Create a DIV-container, let’s name it “y-scroll”, which wraps Table 2 (the body-table).
    3. 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.

    4. 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.
    5. 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.
    6. 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.
    7. 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 scroll
      tableEmulator.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”;
    8. 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.

17 Responses to “Scrollable HTML Table with fixed header for IE 7, IE 8, Firefox 3.5, Chrome”

  • Andy Says:

    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.

  • Aleh Says:

    Thank you for this solution. It has helped a lot.

  • Hugo Says:

    Ok, Andy:
    then show us your (better) example, if you are so proud of it.

  • Sankar Says:

    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 ?

  • Student_101 Says:

    Ya, Andy – it has been more than one year … do you still have your better example?

  • Holger Says:

    That’s a nice and light solution!
    Thank you!

  • Mehmet Says:

    Andy sucks

  • Greg Says:

    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?

  • raj borkar Says:

    hello sir I need to table scrolling with fixed header and footer .
    how can set header and footer
    please suggest

  • oxadeveloper Says:

    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.

  • Kumar Says:

    Dear friends this code is not working in ie6, any help is appreciated.

  • Madhu Says:

    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)

    .
    .
    .

  • Michael F Says:

    Confirmed this code is working great with Safari 5/Win. Good job.

  • oxadeveloper Says:

    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?

  • Michael Says:

    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.

  • bln Says:

    check this one!

    This solution works on all browsers, even IE 6.

    You have a flexible table with fixed header..

  • Michael Says:

    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

Spam protection by WP Captcha-Free