63

Is there a CSS/JavaScript technique to display a long HTML table such that the column headers stay fixed on-screen and the first coloumn stay fixed and scroll with the data.

I want to be able to scroll through the contents of the table, but to always be able to see the column headers at the top and the first column on the left.

If there is a jQuery plugin that would be great! If it helps the only browser I care about is Firefox.

C R
  • 2,182
  • 5
  • 32
  • 41
koogunmo
  • 740
  • 1
  • 5
  • 8
  • 1
    Partial dup: http://stackoverflow.com/questions/673153/html-table-with-fixed-headers – Crescent Fresh Mar 26 '09 at 02:04
  • 2
    Doesn't seems like a duplicate as this question requires fixed column as well. – Selvakumar Arumugam May 31 '13 at 19:39
  • 5
    I voted to reopen this question out of principle. Dozens of upvotes and favorit'ing, apparently someone finds it useful. Yes it requires a long answer. That's what jfiddle and other similar tools are for. – TheLettuceMaster May 17 '16 at 15:26
  • @KickingLettuce Popularity !== on-topic. If the question is reopened it will just be closed again. – TylerH May 17 '16 at 15:53
  • 1
    Google brought me to this question on my first searches, but the answers are outdated and not very satisfying. I eventually found the answer here: https://stackoverflow.com/a/50516259/982107 – Aberrant Feb 22 '19 at 10:28
  • 1
    I don't see why this question is closed. The problem is clearly defined. The question should accept answers with potentially more modern techniques. – Xavier Poinas Oct 30 '22 at 14:05

12 Answers12

19

Working example of link posted by pranav:

http://jsbin.com/nolanole/1/edit?html,js,output

FYI: Tested in IE 6, 7, & 8 (compatibility mode on or off), FF 3 & 3.5, Chrome 2. Not screen-reader-friendly (headers aren't part of content table).

EDIT 5/5/14: moved example to jsBin. This is old, but amazingly still works in current Chrome, IE, and Firefox (though IE and Firefox might require some adjustments to row heights).

acatalept
  • 451
  • 5
  • 9
  • doesn't work for me in winxp ie8 or ff3.6 – allyourcode Jun 24 '10 at 23:09
  • 2
    The link to the jQuery library hosted on jQuery.com changed, and their redirect was incorrect... so it stopped working in ALL browsers. I've fixed the link to point to the file on Google's servers instead - hopefully that one should be more reliable. – acatalept Jul 21 '10 at 16:37
  • Thanks. This helped me a lot. I had to modify the code to use setTimeout() to get it to work with larger tables, but this is a nice example. – Robert Van Hoose Dec 16 '10 at 15:33
3

The jQuery DataTables plug-in is one excellent way to achieve excel-like fixed column(s) and headers.

Note the examples section of the site and the "extras".
http://datatables.net/examples/
http://datatables.net/extras/

The "Extras" section has tools for fixed columns and fixed headers.

Fixed Columns
http://datatables.net/extras/fixedcolumns/
(I believe the example on this page is the one most appropriate for your question.)

Fixed Header
http://datatables.net/extras/fixedheader/
(Includes an example with a full page spreadsheet style layout: http://datatables.net/release-datatables/extras/FixedHeader/top_bottom_left_right.html)

mg1075
  • 17,985
  • 8
  • 59
  • 100
  • 1
    note: from my experience (correct me if I'm wrong), it only works if all of your table cells have `colSpan` and `rowSpan` = 1. – J. Ed Dec 12 '13 at 13:14
2

I see this, although an old question, is a pretty good place to plug my own script:

http://code.google.com/p/js-scroll-table-header/

It just works with no configuration and is really easy to setup.

raveren
  • 17,799
  • 12
  • 70
  • 83
1

In this answer there is also the best answer I found to your question:

HTML table with fixed headers?

and based on pure CSS.

Community
  • 1
  • 1
Marco Demaio
  • 33,578
  • 33
  • 128
  • 159
1

If what you want is to have the headers stay put while the data in the table scrolls vertically, you should implement a <tbody> styled with "overflow-y: auto" like this:

<table>
  <thead>
    <tr>
      <th>Header1</th>
       . . . 
    </tr>
   </thead>
   <tbody style="height: 300px; overflow-y: auto"> 
     <tr>
       . . .
     </tr>
     . . .
   </tbody>
 </table>

If the <tbody> content grows taller than the desired height, it will start scrolling. However, the headers will stay fixed at the top regardless of the scroll position.

levik
  • 114,835
  • 27
  • 73
  • 90
0

I have created something which has fixed header, fixed footer, fixed left column and also fixed right column. This only works fine in IE. As most of the users are still using IE this can be helpful. Please find the code here in Scrollable Table. Please let me your suggestions.

Meanwhile I am working to fix columns in other browser. I will keep you posted. :-)

Shahib
  • 9
  • 1
  • 2
    This isn't a great solution, IE has stopped supporting CSS expressions: http://msdn.microsoft.com/en-us/library/cc304082%28VS.85%29.aspx#expressions – Homer Sep 30 '10 at 14:44
  • 2
    "most of the users are still using IE" LOL :D – J. Ed Dec 12 '13 at 13:12
0
<script>
   $(document).ready(function () {
   $("#GridHeader table").html($('#<%= GridView1.ClientID %>').html());
   $("#GridHeader table tbody .rows").remove();
   $('#<%= GridView1.ClientID %> tr:first th').hide();
});
</script>

<div id="GridHeader">
    <table></table>
</div>

<div style="overflow: auto; height:400px;">
    <asp:GridView ID="GridView1" runat="server" />
</div>
Pat
  • 89
  • 1
  • 2
0

Not quite perfect, but it got me closer than some of the top answers here.

Two different tables, one with the header, and the other, wrapped with a div with the content

<table>
  <thead>
    <tr><th>Stuff</th><th>Second Stuff</th></tr>
  </thead>
</table>
<div style="height: 600px; overflow: auto;">
  <table>
    <tbody>
      //Table
    </tbody>
  </table>
</div>
Ascherer
  • 8,223
  • 3
  • 42
  • 60
-2

I know you can do it for MSIE and this limited example seems to work for firefox (not sure how extensible the technique is).

MarkusQ
  • 21,814
  • 3
  • 56
  • 68
-2

The first column has a scrollbar on the cell right below the headers

<table>
    <thead>
        <th> Header 1</th>
        <th> Header 2</th>
        <th> Header 3</th>
    </thead>
    <tbody>
        <tr>
            <td>
                <div style="width: 50; height:30; overflow-y: scroll"> 
                    Tklasdjf alksjf asjdfk jsadfl kajsdl fjasdk fljsaldk 
                    fjlksa djflkasjdflkjsadlkf jsakldjfasdjfklasjdflkjasdlkfjaslkdfjasdf
                </div>
            </td>
            <td>
                Hello world
            </td>
            <td> Hello world2
        </tr>
    </tbody>
</table>
Jaime Garcia
  • 6,744
  • 7
  • 49
  • 61
  • I think you've misunderstood what's being asked. We want to be able to scroll to different cells in a (large) table. – allyourcode Jun 24 '10 at 23:38
-2

YUI DataTable

I don't know if YUI DT has this feature but I won't be surprised if it does.

ksuralta
  • 16,276
  • 16
  • 38
  • 36
  • Thanks I took a look at that. It does have a scrolling table but that does have a fixed header but not the fixed coloumn that I need. http://developer.yahoo.com/yui/examples/datatable/dt_fixedscroll.html – koogunmo Mar 26 '09 at 18:21
  • yeah, I couldn't find that either from their list of examples. – allyourcode Jun 24 '10 at 23:35
-3

Here is a good jQuery plugin, working in all browsers!

You have a fixed header table without fixing its width.

Check it: https://github.com/benjaminleouzon/tablefixedheader

Disclaimer: I am the author of the plugin.

Pang
  • 9,564
  • 146
  • 81
  • 122
bln
  • 310
  • 1
  • 5