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>
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Friday, January 30, 2009 1:30 PM

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

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

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

Any fix for IE8? 2/1/2010 12:24 AM | anjsag

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

Thanks for this solution...but...it does not work with IE8 (8.0.6001.18882). Please help!? 2/2/2010 2:58 AM | anteportas

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

i got this from another forum and this works for ie8
just add below html
<meta http-equiv="X-UA-Compatible" content="IE=7" /> 2/24/2010 10:47 PM | icasper

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

.locked{
left: expression(parentNode.parentNode.parentNode.parentNode.scrollLeft); /* IE5+ only */
position: relative;
z-index: 10;
}

Someone asked to lock the frst column..try this...It worked in my previous application...dont remember all the things though...just include this class in the TD you want to fix... 3/16/2010 9:18 AM | Harik

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

Thanks, very util. 3/20/2010 5:20 PM | Lester

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

Hey, extremely useful, thank you very much. 6/22/2010 12:17 AM | Sandeep Chatterjee

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

Thanks a lot this worked in IE8
<meta http-equiv="X-UA-Compatible" content="IE=7" /> 7/5/2010 8:15 PM | Mani

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

Hey the IE=7 Solution is working, but i also work with master pages in asp.net. Is there any other way to get this thing to work under IE8. 7/21/2010 7:23 PM | Bjoern

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

Doesn't work in FF4.0b2. The technique was intended to work with FF3.x, and no doubt does, but if the new version of the browser breaks it, then this is probably not a good method to go with. I can only imagine the headache of people trying to modify legacy code having used this technique.

The use of doubled-up tables (one table for the header, another for the body) each in their own divs seems like a more robust and long-term solution. 8/3/2010 8:03 AM | FDR

# re: CSS Table Scrolling with Fixed Header for IE8

hi,

thx a lot, i've fixed horizontal scroll in ie8.
8/12/2010 1:36 AM | pandi

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

a good solution here http://www.tablefixedheader.com/ 8/22/2010 7:09 PM | bln

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

Hi Buddy,

I have created fix header table. I hope this will help you.

http://s7u.blogspot.com/2010/09/fixed-header-footer-with-out-css-hacks.html

Regards,
Shahib 9/29/2010 2:31 AM | Shahib

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

Forgot to mention my blog link
http://s7u.blogspot.com/2010/09/fixed-header-footer-with-out-css-hacks.html 10/1/2010 6:45 PM | Shahib

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

New version available please try http://s7u.blogspot.com 10/8/2010 4:24 AM | Shahib

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

One more thank you... THANK YOU! 12/8/2010 10:26 AM | aprigliano

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

@icasper : thank you.. 12/13/2010 12:23 AM | era

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

it will not in safari in mac. Please show the solution. 1/13/2011 8:07 PM | chris

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

Not working in Googlechrome any solution.... Its Urgent! 3/16/2011 4:30 AM | mayank

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

I don't see it work in FF 4 :( 4/17/2011 6:22 PM | PCTip

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

Thanks. It really helped. specially the padding part for first element. 4/18/2011 4:33 AM | Ken

# Solucion Correcta OK++

EXCELENTE

Solucion correcta, tube problemas centrando una tabla denro de un div, pero con su solucion lo logre.

Probe el resultado en: IE, FF, CHROME. OK++

http://www.educolombia.org/notas/colpruebas/2010/califica/62/1/
4/25/2011 12:58 PM | Hely Rojas

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

it works fine on FF 3 but no in FF 4
4/28/2011 10:55 PM | sakini

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

Thanks Leslee, this is a great solution for IE, but this does not work in Chrome, Do you have a workaround of this for Chrome ?

4/29/2011 6:29 AM | Andrews

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

Hmm, row 1 is hidden underneath the header for me! 5/2/2011 3:38 PM | Chalky

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

You've been a life saver! thanks. 5/23/2011 2:25 AM | Jim

# Easy to understand and implement.

I love your solution for scrolling the tbody.
IMHO the best way of solving this ff/ie problem.

Thanks a lot! 6/15/2011 12:15 AM | malk

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

Good article. It should be pointed out that as of IE 9, it still does not support scrolling via the tbody tag. 6/23/2011 5:26 AM | ColdFusion Developer

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

Sorry buddy, output is not same in ie8 and firefox 4+ when comparing with ie7. Any way Good job! :) 7/27/2011 2:52 AM | Ravikumar V.

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

That is what i am expecting....
8/8/2011 12:11 AM | raja

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

awesome ! 10/17/2011 6:31 AM | marcio

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

not working 12/21/2011 5:34 AM | SS

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

Works fantastic in IE7 12/28/2011 10:38 PM | sangita mane

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

The code in the initial post works like a magic.
Thanks for your help!! 1/5/2012 12:17 AM | Tom

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

not working on ie 9 1/12/2012 12:26 PM | yalem

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