Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fixed header table with horizontal scrollbar and vertical scrollbar on

I have been trying to figure out this problem i have with html/css sticky header + scrollbars. We are creating a program that requires scrollbars to show up once the containersize reaches a certain point(depending on resolution of the user).

I am forcing a min-width on the second column in the table, so the table stops decreasing at a certain point and forces the container to stay at a certain width. The overflow on the container shows the horizontal scrollbar. Everything works fine. Once i add a second scrollbar for the vertical scrolling, things are getting messed up. Does someone have a solution for this problem? I want to have a vertical scrollbar on the .table-body, but the scrollbar must be visible on the outercontainer.

Is there a good html/css solution for fixed header tables? I have been searching for a week, but can only find jQuery plugins for this kind of behaviour.

This is my current HTML:

<!DOCTYPE html> <html> <head>     <title>fixed header prototyping</title>     <link rel="stylesheet" href="css/style.css"> </head>  <body> <div class="outer-container"> <!-- absolute positioned container --> <div class="inner-container">      <div class="table-header">         <table id="headertable" width="100%" cellpadding="0" cellspacing="0">             <thead>                 <tr>                     <th class="header-cell col1">One</th>                     <th class="header-cell col2">Two</th>                     <th class="header-cell col3">Three</th>                     <th class="header-cell col4">Four</th>                     <th class="header-cell col5">Five</th>                 </tr>             </thead>         </table>     </div>      <div class="table-body">         <table id="bodytable" width="100%" cellpadding="0" cellspacing="0">             <tbody>                 <tr>                     <td class="body-cell col1">body row1</td>                     <td class="body-cell col2">body row2</td>                     <td class="body-cell col3">body row2</td>                     <td class="body-cell col4">body row2</td>                     <td class="body-cell col5">body row2 en nog meer</td>                 </tr>                 <tr>                     <td class="body-cell col1">body row1</td>                     <td class="body-cell col2">body row2</td>                     <td class="body-cell col3">body row2</td>                     <td class="body-cell col4">body row2</td>                     <td class="body-cell col5">body row2 en nog meer</td>                 </tr>                 <tr>                     <td class="body-cell col1">body row1</td>                     <td class="body-cell col2">body row2</td>                     <td class="body-cell col3">body row2</td>                     <td class="body-cell col4">body row2</td>                     <td class="body-cell col5">body row2 en nog meer</td>                 </tr>                 <tr>                     <td class="body-cell col1">body row1</td>                     <td class="body-cell col2">body row2</td>                     <td class="body-cell col3">body row2</td>                     <td class="body-cell col4">body row2</td>                     <td class="body-cell col5">body row2 en nog meer</td>                 </tr>                 <tr>                     <td class="body-cell col1">body row1</td>                     <td class="body-cell col2">body row2</td>                     <td class="body-cell col3">body row2</td>                     <td class="body-cell col4">body row2</td>                     <td class="body-cell col5">body row2 en nog meer</td>                 </tr>                 <tr>                     <td class="body-cell col1">body row1</td>                     <td class="body-cell col2">body row2</td>                     <td class="body-cell col3">body row2</td>                     <td class="body-cell col4">body row2</td>                     <td class="body-cell col5">body row2 en nog meer</td>                 </tr>                 <tr>                     <td class="body-cell col1">body row1</td>                     <td class="body-cell col2">body row2</td>                     <td class="body-cell col3">body row2</td>                     <td class="body-cell col4">body row2</td>                     <td class="body-cell col5">body row2 en nog meer</td>                 </tr>                 <tr>                     <td class="body-cell col1">body row1</td>                     <td class="body-cell col2">body row2</td>                     <td class="body-cell col3">body row2</td>                     <td class="body-cell col4">body row2</td>                     <td class="body-cell col5">body row2 en nog meer</td>                 </tr>                 <tr>                     <td class="body-cell col1">body row1</td>                     <td class="body-cell col2">body row2</td>                     <td class="body-cell col3">body row2</td>                     <td class="body-cell col4">body row2</td>                     <td class="body-cell col5">body row2 en nog meer</td>                 </tr>                 <tr>                     <td class="body-cell col1">body row1</td>                     <td class="body-cell col2">body row2</td>                     <td class="body-cell col3">body row2</td>                     <td class="body-cell col4">body row2</td>                     <td class="body-cell col5">body row2 en nog meer</td>                 </tr>                 <tr>                     <td class="body-cell col1">body row1</td>                     <td class="body-cell col2">body row2</td>                     <td class="body-cell col3">body row2</td>                     <td class="body-cell col4">body row2</td>                     <td class="body-cell col5">body row2 en nog meer</td>                 </tr>                 <tr>                     <td class="body-cell col1">body row1</td>                     <td class="body-cell col2">body row2</td>                     <td class="body-cell col3">body row2</td>                     <td class="body-cell col4">body row2</td>                     <td class="body-cell col5">body row2 en nog meer</td>                 </tr>                 <tr>                     <td class="body-cell col1">body row1</td>                     <td class="body-cell col2">body row2</td>                     <td class="body-cell col3">body row2</td>                     <td class="body-cell col4">body row2</td>                     <td class="body-cell col5">body row2 en nog meer</td>                 </tr>                 <tr>                     <td class="body-cell col1">body row1</td>                     <td class="body-cell col2">body row2</td>                     <td class="body-cell col3">body row2</td>                     <td class="body-cell col4">body row2</td>                     <td class="body-cell col5">body row2 en nog meer</td>                 </tr>                 <tr>                     <td class="body-cell col1">body row1</td>                     <td class="body-cell col2">body row2</td>                     <td class="body-cell col3">body row2</td>                     <td class="body-cell col4">body row2</td>                     <td class="body-cell col5">body row2 en nog meer</td>                 </tr>                 <tr>                     <td class="body-cell col1">body row1</td>                     <td class="body-cell col2">body row2</td>                     <td class="body-cell col3">body row2</td>                     <td class="body-cell col4">body row2</td>                     <td class="body-cell col5">body row2 en nog meer</td>                 </tr>                 <tr>                     <td class="body-cell col1">body row1</td>                     <td class="body-cell col2">body row2</td>                     <td class="body-cell col3">body row2</td>                     <td class="body-cell col4">body row2</td>                     <td class="body-cell col5">body row2 en nog meer</td>                 </tr>                 <tr>                     <td class="body-cell col1">body row1</td>                     <td class="body-cell col2">body row2</td>                     <td class="body-cell col3">body row2</td>                     <td class="body-cell col4">body row2</td>                     <td class="body-cell col5">body row2 en nog meer</td>                 </tr>                 <tr>                     <td class="body-cell col1">body row1</td>                     <td class="body-cell col2">body row2</td>                     <td class="body-cell col3">body row2</td>                     <td class="body-cell col4">body row2</td>                     <td class="body-cell col5">body row2 en nog meer</td>                 </tr>                 <tr>                     <td class="body-cell col1">body row1</td>                     <td class="body-cell col2">body row2</td>                     <td class="body-cell col3">body row2</td>                     <td class="body-cell col4">body row2</td>                     <td class="body-cell col5">body row2 en nog meer</td>                 </tr>                 <tr>                     <td class="body-cell col1">body row1</td>                     <td class="body-cell col2">body row2</td>                     <td class="body-cell col3">body row2</td>                     <td class="body-cell col4">body row2</td>                     <td class="body-cell col5">body row2 en nog meer</td>                 </tr>                 <tr>                     <td class="body-cell col1">body row1</td>                     <td class="body-cell col2">body row2</td>                     <td class="body-cell col3">body row2</td>                     <td class="body-cell col4">body row2</td>                     <td class="body-cell col5">body row2 en nog meer</td>                 </tr>                 <tr>                     <td class="body-cell col1">body row1</td>                     <td class="body-cell col2">body row2</td>                     <td class="body-cell col3">body row2</td>                     <td class="body-cell col4">body row2</td>                     <td class="body-cell col5">body row2 en nog meer</td>                 </tr>                 <tr>                     <td class="body-cell col1">body row1</td>                     <td class="body-cell col2">body row2</td>                     <td class="body-cell col3">body row2</td>                     <td class="body-cell col4">body row2</td>                     <td class="body-cell col5">body row2 en nog meer</td>                 </tr>                 <tr>                     <td class="body-cell col1">body row1</td>                     <td class="body-cell col2">body row2</td>                     <td class="body-cell col3">body row2</td>                     <td class="body-cell col4">body row2</td>                     <td class="body-cell col5">body row2 en nog meer</td>                 </tr>                 <tr>                     <td class="body-cell col1">body row1</td>                     <td class="body-cell col2">body row2</td>                     <td class="body-cell col3">body row2</td>                     <td class="body-cell col4">body row2</td>                     <td class="body-cell col5">body row2 en nog meer</td>                 </tr>                 <tr>                     <td class="body-cell col1">body row1</td>                     <td class="body-cell col2">body row2</td>                     <td class="body-cell col3">body row2</td>                     <td class="body-cell col4">body row2</td>                     <td class="body-cell col5">body row2 en nog meer</td>                 </tr>                 <tr>                     <td class="body-cell col1">body row1</td>                     <td class="body-cell col2">body row2</td>                     <td class="body-cell col3">body row2</td>                     <td class="body-cell col4">body row2</td>                     <td class="body-cell col5">body row2 en nog meer</td>                 </tr>                 <tr>                     <td class="body-cell col1">body row1</td>                     <td class="body-cell col2">body row2</td>                     <td class="body-cell col3">body row2</td>                     <td class="body-cell col4">body row2</td>                     <td class="body-cell col5">body row2 en nog meer</td>                 </tr>                 <tr>                     <td class="body-cell col1">body row1</td>                     <td class="body-cell col2">body row2</td>                     <td class="body-cell col3">body row2</td>                     <td class="body-cell col4">body row2</td>                     <td class="body-cell col5">body row2 en nog meer</td>                 </tr>                 <tr>                     <td class="body-cell col1">body row1</td>                     <td class="body-cell col2">body row2</td>                     <td class="body-cell col3">body row2</td>                     <td class="body-cell col4">body row2</td>                     <td class="body-cell col5">body row2 en nog meer</td>                 </tr>                 <tr>                     <td class="body-cell col1">body row1</td>                     <td class="body-cell col2">body row2</td>                     <td class="body-cell col3">body row2</td>                     <td class="body-cell col4">body row2</td>                     <td class="body-cell col5">body row2 en nog meer</td>                 </tr>                 <tr>                     <td class="body-cell col1">body row1</td>                     <td class="body-cell col2">body row2</td>                     <td class="body-cell col3">body row2</td>                     <td class="body-cell col4">body row2</td>                     <td class="body-cell col5">body row2 en nog meer</td>                 </tr>                 <tr>                     <td class="body-cell col1">body row1</td>                     <td class="body-cell col2">body row2</td>                     <td class="body-cell col3">body row2</td>                     <td class="body-cell col4">body row2</td>                     <td class="body-cell col5">body row2 en nog meer</td>                 </tr>                 <tr>                     <td class="body-cell col1">body row1</td>                     <td class="body-cell col2">body row2</td>                     <td class="body-cell col3">body row2</td>                     <td class="body-cell col4">body row2</td>                     <td class="body-cell col5">body row2 en nog meer</td>                 </tr>             </tbody>         </table>     </div> </div> </div>    </body> </html> 

And my CSS looks like this:

body {     margin:0;     padding:0;     height: 100%;     width: 100%; } table {     border-collapse: collapse; /* make simple 1px lines borders if border defined */ } tr {     width: 100%; }  .outer-container {     background-color: #ccc;     position: absolute;     top:0;     left: 0;     right: 300px;     bottom:40px;     overflow: hidden;  } .inner-container {     width: 100%;     height: 100%;     position: relative;     overflow-x: scroll;     overflow-y:hidden; } .table-header {     float:left;     width: 100%; } .table-body {     float:left;     height: 100%;     width: inherit;     overflow-y: scroll; } .header-cell {     background-color: yellow;     text-align: left;     height: 40px; } .body-cell {     background-color: blue;     text-align: left; } .col1, .col3, .col4, .col5 {     width:120px;     min-width: 120px; } .col2 {     min-width: 300px; } 

JSFiddle example - http://jsfiddle.net/W8URM/

Thanks in advance guys!

like image 205
Remco Bakker Avatar asked Feb 20 '13 10:02

Remco Bakker


People also ask

How do I make my table scrollable with fixed header?

Create a Table That Has a Fixed Header. We can create an HTML table that has a fixed header with some CSS. We set the height of the table element to 120px to make restrict the height of it so we can make it scrollable. To make it scrollable, we set the overflow CSS property to scroll .

How do you change the vertical and horizontal scrollbar in a table?

Suppose we want to add a scroll bar option in HTML, use an “overflow” option and set it as auto-enabled for adding both horizontal and vertical scroll bars. If we want to add a vertical bar option in Html, add the line “overflow-y” in the files.

How do I make my table header fixed while scrolling in HTML?

To freeze the row/column we can use a simple HTML table and CSS. HTML: In HTML we can define the header row by <th> tag or we can use <td> tag also. Below example is using the <th> tag. We also put the table in DIV element to see the horizontal and vertical scrollbar by setting the overflow property of the DIV element.


1 Answers

This is not an easy one. I've come up with a Script solution. (I don't think this can be done using pure CSS)

the HTML stays the same as you posted, the CSS changes a little bit, JQuery code added.

Working Fiddle Tested on: IE10, IE9, IE8, FF, Chrome

BTW: if you have unique elements, why don't you use id's instead of classes? I think it gives a better selector performance.

Explanation of how it works: inner-container will span the entire space of the outer-container (so basically, he's not needed) but I left him there, so you wont need to change you DOM.

the table-header is relatively positioned, without a scroll (overflow: hidden), we will handle his scroll later.

the table-body have to span the rest of the inner-container height, so I used a script to determine what height to fix him. (it changes dynamically when you re-size the window) without a fixed height, the scroll wont appear, because the div will just grow large instead.. notice that this part can be done without script, if you fix the header height and use CSS3 (as shown in the end of the answer)

now it's just a matter of moving the header along with the body each time we scroll. this is done by a function assigned to the scroll event.

CSS (some of it was copied from your style)

* {     padding: 0;     margin: 0; }  body {     height: 100%;     width: 100%; } table {     border-collapse: collapse; /* make simple 1px lines borders if border defined */ } .outer-container {     background-color: #ccc;     position: absolute;     top:0;     left: 0;     right: 300px;     bottom: 40px; }  .inner-container {     height: 100%;     overflow: hidden; }  .table-header {     position: relative; } .table-body {     overflow: auto; }  .header-cell {     background-color: yellow;     text-align: left;     height: 40px; } .body-cell  {     background-color: blue;     text-align: left; } .col1, .col3, .col4, .col5 {     width:120px;     min-width: 120px; } .col2 {     min-width: 300px; } 

JQuery

$(document).ready(function () {     setTableBody();     $(window).resize(setTableBody);     $(".table-body").scroll(function ()     {         $(".table-header").offset({ left: -1*this.scrollLeft });     }); });  function setTableBody() {     $(".table-body").height($(".inner-container").height() - $(".table-header").height()); } 

If you don't care about fixing the header height (I saw that you fixed the cell's height in your CSS), some of the Script can be skiped if you use CSS3 :Shorter Fiddle (this will not work on IE8)

like image 134
avrahamcool Avatar answered Sep 19 '22 22:09

avrahamcool