Home Contact

Leslee Sheu

Senior Production Artist at e.magination
ASP.NET and MOSS Master Pages, HTML, CSS

News

Archives

Post Categories

Syndication:

CSS Table Scrolling with Fixed Header for IE 7

This week I was tasked with styling a data table so that all the rows would scroll vertically, but the header row would stay in place.   For the project in question, the scrolling must work in both Firefox and IE 7.

Firefox supports CSS for scrolling the <tbody> part of the table.  However, IE does not support scrolling for the <tbody>.  I searched some other blogs for solutions and found that a popular solution for IE involves using expressions in CSS.  A scrolling <div> is placed around the <table>.  The expression is applied to the <thead> row like this:

     thead tr {
         position: relative;
         top: expression(this.offsetParent.scrollTop);
        }

But I found that the relative positioning caused the <thead> to shift down a few pixels when the scroll is activated.  (Adding -2 to the expression did not solve the shifting problem for me.)  So I decided to use position: absolute instead.  This positions the row in <thead> correctly, however it will cover up the rest of the table rows.  So I applied extra padding to the first table row in <tbody>, so that the date will show below the fixed header.  Here is the result:

Here is my code, which I have only tested in Firefox 3 and IE 7.  The first CSS style block is for Firefox.  The second style block is within conditional comments for IE, so that these styles will overwrite the Firefox styling.  (Normally I would be linking to external stylesheets -- a general one for non-IE browsers and a separate one for IE).  For simplicity's sake in this example, I did not use class names -- you could apply your own class names as needed.

CSS:

<style type="text/css">
    table {
        border: solid #66CC99;
        border-width: 0px 1px 1px 0px;
        width: 400px;
    }
    th, td {
        border: solid #66CC99;
        border-width: 1px 0px 0px 1px;
        padding: 4px;
    }
    th {
        background-color: #339999;
        color: #FFFFFF;
    }
    tr.alt td {
        background-color: #EEEEEE;
    }
    tbody {
        height: 200px;
        overflow-y: auto;
        overflow-x: hidden;
    }
</style>
<!--[if IE]>
    <style type="text/css">
        div {
            position: relative;
            height: 200px;
            width: 416px;
            overflow-y: scroll;
            overflow-x: hidden;
            border: solid #66CC99;
            border-width: 0px 0px 1px 0px;
        }
        table {
            border-width: 1px 1px 0px 0px;
        }
        thead tr {
            position: absolute;
            top: expression(this.offsetParent.scrollTop);
        }
        tbody {
            height: auto;
        }
        table tbody tr:first-child td {
            padding: 29px 4px 4px 4px;
        }
    </style>
<![endif]-->


HTML:

<div>
    <table border="0" cellspacing="0" cellpadding="0">
        <thead>
              <tr>
                    <th>HEADER 1</th>
                    <th>HEADER 2</th>
                    <th>HEADER 3</th>
                    <th>HEADER 4</th>
              </tr>
        </thead>
        <tbody>
              <tr>
                    <td>content 1</td>
                    <td>content</td>
                    <td>content </td>
                    <td>content </td>
              </tr>
              <tr class="alt">
                    <td>content 2</td>
                    <td>content </td>
                    <td>content </td>
                    <td>content </td>
              </tr>
              <tr>
                    <td>content 3</td>
                    <td>content </td>
                    <td>content </td>
                    <td>content </td>
              </tr>
              <tr class="alt">
                    <td>content 4</td>
                    <td>content </td>
                    <td>content </td>
                    <td>content </td>
              </tr>
              <tr>
                    <td>content 5</td>
                    <td>content </td>
                    <td>content </td>
                    <td>content </td>
              </tr>
              <tr class="alt">
                    <td>content 6</td>
                    <td>content </td>
                    <td>content </td>
                    <td>content </td>
              </tr>
              <tr>
                    <td>content 7</td>
                    <td>content </td>
                    <td>content </td>
                    <td>content </td>
              </tr>
              <tr class="alt">
                    <td>content 8</td>
                    <td>content </td>
                    <td>content </td>
                    <td>content </td>
              </tr>
              <tr>
                    <td>content 9</td>
                    <td>content </td>
                    <td>content </td>
                    <td>content </td>
              </tr>
              <tr class="alt">
                    <td>content 10</td>
                    <td>content </td>
                    <td>content </td>
                    <td>content </td>
              </tr>
              <tr>
                    <td>content 11</td>
                    <td>content </td>
                    <td>content </td>
                    <td>content </td>
              </tr>
              <tr class="alt">
                    <td>content 12</td>
                    <td>content </td>
                    <td>content </td>
                    <td>content </td>
              </tr>
              <tr>
                    <td>content 13</td>
                    <td>content </td>
                    <td>content </td>
                    <td>content </td>
              </tr>
              <tr class="alt">
                    <td>content 14</td>
                    <td>content </td>
                    <td>content </td>
                    <td>content </td>
              </tr>
        </tbody>
    </table>
</div>

Feedback

# re: CSS Table Scrolling with Fixed Header for IE 7

Leslee
Thank you for the code on Fixed headers. It seems to work in IE6 with a few bugs.
When I look at the table Dream Weaver design view, I see the top row. In browser view it disappears. Can this be fixed?
Also, can someone skilled with HTML write codes for the Tables in DW that will fix the header row?
At the age of 73, I decided to take up webdesign for our club. www.tcdxa.org
The learning curve with DW is nasty, but I have nothing else to do. It keeps me off the streets and out of the bars. Thanks in advance for any help you can pass along.
Jim
4/20/2009 12:03 PM | Jim

# re: CSS Table Scrolling with Fixed Header for IE 7

Merci
4/30/2009 5:30 AM | Yassine

# re: CSS Table Scrolling with Fixed Header for IE 7

Very usefull tip!

Does it handle horizontal scroll too (with a wider table lets say)?

Thanks! 5/14/2009 5:28 PM | Mario GS

# re: CSS Table Scrolling with Fixed Header for IE 7

This solution is for vertical scrolling only. I've just been assigned another project that will use vertical and horizontal scrolling for the table. If I figure out a good solution for that, I'll try to post it. 5/15/2009 9:37 AM | Leslee

# re: CSS Table Scrolling with Fixed Header for IE 7

does not work in google chrome! 5/18/2009 6:20 PM | Jim

# re: CSS Table Scrolling with Fixed Header for IE 7

doesn't work in Safari either. 5/20/2009 6:33 PM | Rusty Howson

# re: CSS Table Scrolling with Fixed Header for IE 7

Hi Jim and Rusty -- as my post mentioned, for this problem I was tasked with making it work in IE7 and Firefox only. We targeted these two browsers because the project is an internal application and all the employees (end users) were using either IE7 or Firefox. Thank you for testing it in Chrome and Safari. If you figure out a solution for Chrome and/or Safari, it would be helpful to know! 5/25/2009 12:45 PM | Leslee

# re: CSS Table Scrolling with Fixed Header for IE 7

Hello, I tested this code on IE8 but it does not work. Can you please do the same for IE8. 6/4/2009 8:56 AM | Usman

# re: CSS Table Scrolling with Fixed Header for IE 7

Try it out with only 2-3 rows and you'll see why this solution doesn't work for all cases. It will stretch the rows to fill the fixed height of the tbody. So it's not suitable for a variable number of rows.

I'm coming to the conclusion that the only way to achieve this effect is to have one table for column headers and another scrolling one below it for the data. Not too bad if you have fixed pixel widths for all the columns, but obviously pretty bad if you don't know them in advance since the headers and data won't quite line up. 6/10/2009 9:54 AM | Ian

# re: CSS Table Scrolling with Fixed Header for IE 7 - printing

What would be the magic code to put into a print stylesheet to make this print the whole table? I managed to get it to work for printing in firefox by setting the overflow to auto, but for some reason in IE7 it seems to then loose the table borders .... 6/18/2009 2:18 PM | s

# re: CSS Table Scrolling with Fixed Header for IE 7

Above piece of information is very much informative. 6/22/2009 7:19 AM | ashwin

# re: CSS Table Scrolling with Fixed Header for IE 7

Not working in IE 6,7
6/23/2009 3:26 AM | Julian

# re: CSS Table Scrolling with Fixed Header for IE 7

It works fine for me in IE6. Nice job. 7/6/2009 6:43 PM | Jason

# re: CSS Table Scrolling with Fixed Header for IE 7

I'm encountering an issue: first-child doesn't work in IE6, so the first row in <tbody> is hidden behind the static <thead>.

I tried using this CSS below for IE6:

table tbody tr td {
padding-top: expression(this.previousSibling==null?'29px':'4px');
}

but that targets the first <td> in every row instead of all the <td>'s in the first row. Not good.

Does anyone have a solution for this? 7/8/2009 5:38 PM | HM

# re: CSS Table Scrolling with Fixed Header for IE 7

I solved this problem by adding extra empty row before the first row into <tbody>. had to measure the height of <thead> before and set it to that new row. 7/9/2009 6:29 AM | Tim

# re: CSS Table Scrolling with Fixed Header for IE 7

Hi, this page is not working for me in IE 7 normal mode but it does work in Compatability mode. Took a screen shot and linked it.

As you can see the headers aren't the full width, and I've scrolled the table down slightly and the heading doesn't stay in place.
http://imgur.com/ADYyN.jpg


On a lark I tried different doc types on a local copy but it didn't solve the problem for me. Used http://htmlhelp.com/tools/validator/doctype.html for a list.

7/27/2009 8:01 AM | Bob

# re: CSS Table Scrolling with Fixed Header for IE 7

thanks a lot!
worked fine on ie6 8/18/2009 12:21 AM | oscar

# re: CSS Table Scrolling with Fixed Header for IE 7

Thanks for very useful suggestions.

Does anyone know of any way to fix first column the same way the header is fixed outside scroll area? 9/21/2009 6:39 AM | Len Yabloko

# re: CSS Table Scrolling with Fixed Header for IE 7

Thanks for the solution. I had played with a number of similar css/html ideas, and got close - but your code was perfect! Thanks 10/7/2009 1:22 PM | bob

# re: CSS Table Scrolling with Fixed Header for IE 7

This code will work on Safari, Opera and Chrome IF AND ONLY IF the table is in an iframe. I'm trying to figure out how to dump the frame, and will let you know if I succeed. 10/8/2009 11:26 AM | bob

# re: CSS Table Scrolling with Fixed Header for IE 7

doesnt work in IE8 10/8/2009 9:13 PM | supid IE

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: