Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

css only - Table overflow with fixed header

Tags:

css

css-tables

I am trying to build a table which is scrollable in the x and y directions if the content is bigger than the container. I also want the header to always be visible at the top. I've got the first part working, and the header is always visible at the top, however the header column sizes do not match up with the table table sizes.

I've created this fiddle:

http://jsfiddle.net/xxQQS/1/

I am after a CSS only solution.

EDIT: There seem to be a quite a few people who seem to think that this cannot only be done with CSS. This may be true, however please don't just post to say 'no this can't be done'. If you can explain why this can't be done without CSS I'll accept your answer.

like image 821
Luca Spiller Avatar asked Mar 20 '12 17:03

Luca Spiller


1 Answers

Create a clone of your table. For the first table, hide all rows except the headers using visibility: hidden. Hide the header of the other table using visibility: hidden. Place your tables inside divs with overflow properties set as follows:

<div style="overflow-x: hidden; width: 400px;">
    <div style="overflow-y: hidden; height: 20px;">
        <table id="head-only">
        </table>
    </div>
    <div style="overflow-y: scroll; height: 100px;">
        <table id="body-only">
        </table>
    </div>
</div>
like image 165
Salman A Avatar answered Sep 20 '22 03:09

Salman A