I've found an implementation of what you're looking for at: http://www.cssplay.co.uk/menu/tablescroll.html
The author has graciously allowed the reuse of his code, with attribution, and you can learn more about it from his website ... but I'll delve into the reasons why his solution works here.
The table is wrapped in two divs, .outer and .innera. Those divs have the following properties:
.outer:
css Code:
Original
- css Code |
|
|
|
.outer {
position:relative;
padding:4em 0 3em 0;
width:54em;
background:#eee;
margin:0 auto 3em auto;
}
.innera:
css Code:
Original
- css Code |
|
|
|
.innera {
overflow:auto;
width:54em;
height:9.6em;
background:#eee;
}
Now this is the important bit which enables his solution; each TR within a THEAD has the following styles applied to it:
css Code:
Original
- css Code |
|
|
|
.outer thead tr {
position:absolute;
top:1.5em;
height:1.5em;
left:0;
}
What this is doing is the following: specify a height for the div containing the table via the .inner class (
height:9.6em;), and force the contents of that div to be scrollable (
overflow: auto;).
Now, were you to just implement the overflow: auto; on a div with a specified height wrapped around your table, the entire table - headers and all - would scroll. This is where relative/absolute positioning come into play...
Since the .outer class uses relative positioning (
position: relative;), any element within it can be absolutely positioned (
position: absolute;). An absolutely positioned element stays put. It does not scroll: it sticks to whatever geometry you assign it.
So, when the author specifies (
top:1.5em; left:0;) it means this: "Force all of the TR's within THEAD tags under the .outer class to absolutely position themselves 0px from the left edge of the containing div, and 1.5em from the top."
Ergo the entire table still scrolls as discussed before, the only difference now is that you've told the THEAD's to stay put at a certain location regardless of scrolling, thereby achieving the result you're aiming for.
- Null