tags:

views:

1834

answers:

4

If you found an answer or the question useful please upvote them.

how do I create an HTML table with fixed/frozen left column and scrollable body?

I need a simple solution. I know it's similar to some other questions, like:

http://stackoverflow.com/questions/684211/html-table-with-fixed-headers-and-a-fixed-column http://stackoverflow.com/questions/296020/how-can-i-lock-the-first-row-and-first-column-of-a-table-when-scrolling-possibly

But I need just a single left column to be frozen and I would prefer a simple and script-less solution.

+1  A: 

Style the left column with position: fixed. (You'll presumably want to use top and left styles to control where exactly it occurs.)

chaos
That's brilliant in its simplicity. Is it possible to allow proper vertical scrolling of entire page?
agsamek
Yeah; you should skip the whole "table" part and just use two divs, one for the left column and one for the rest of the content. Left column div gets `position: fixed` and stays put, rest of content acts normally (presumably with a left margin set so it doesn't overlap the left column).
chaos
A: 

Alternatively, style the tbody with a predetermined size (via height:20em, for example) and use overflow-y:scroll;

Then, you can have a huge tbody, which will scroll independently of the rest of the page.

Eamon Nerbonne
Typo on `scroll`
Randell
fixed, thanks...
Eamon Nerbonne
A: 

may I ask why you specifically want a table to do this? it may be a lot easier using regular div's

Litso
div solution may be OK as well
agsamek
+3  A: 

If you want a table where only the columns scroll horizontally, you can position:absolute the first column, and then wrap the entire table in an overflow-x: scroll block. Don't bother trying this in IE7, however...

<!DOCTYPE html>
<html>
<head><title>testdoc</title>
<style type="text/css">
    body { font:20pt Calibri;}
  table { border-collapse:collapse; border-top: 3px solid grey; margin-top:3px;}
  td {margin:0;
   border:3px solid grey; 
   border-top-width:0px; 
   white-space:nowrap;}
  div { width: 600px; 
  overflow-x:scroll;  
   margin-left:5em; 
   overflow-y:visible;
    padding-bottom:1px;}

  .headcol {position:absolute; 
  width:5em; 
  left:0; top:auto;
  border-right: 0px none black; 
  border-top: 3px solid grey; 
  margin-top:-3px; padding-right:4px;}
  .headcol:before {content: 'Row ';}
  .long { min-width:100em; background:yellow; text-align:center;}
</style>
</head>
<body>
<div><table>
<tr><td class="headcol">1</td><td class="long">...text...</td></tr>
<tr><td class="headcol">2</td><td class="long">...text...</td></tr>
<tr><td class="headcol">3</td><td class="long">...text...</td></tr>
<tr><td class="headcol">4</td><td class="long">...text...</td></tr>
<tr><td class="headcol">5</td><td class="long">...text...</td></tr>
<tr><td class="headcol">6</td><td class="long">...text...</td></tr>
<tr><td class="headcol">7</td><td class="long">...text...</td></tr>
<tr><td class="headcol">8</td><td class="long">...text...</td></tr>
<tr><td class="headcol">9</td><td class="long">...text...</td></tr>
</table></div>
</body>
</html>
Eamon Nerbonne
This is incredible! Works fine in IE8
agsamek
Note that getting the borders right (i.e., pixel perfectly aligned) is very tricky; in practice if you really want borders, I'd place them below the td level (by including all content of the td inside a div, for instance).
Eamon Nerbonne