Here's a solution which again is not a CSS only solution. It is similar to avrahamcool's solution in that it uses a few lines of jQuery, but instead of changing heights and moving the header along, all it does is changing the width of tbody
based on how far its parent table
is scrolled along to the right.
An added bonus with this solution is that it works with a semantically valid HTML table.
It works great on all recent browser versions (IE10, Chrome, FF) and that's it, the scrolling functionality breaks on older versions.
But then the fact that you are using a semantically valid HTML table will save the day and ensure the table is still displayed properly, it's only the scrolling functionality that won't work on older browsers.
Here's a jsFiddle for demonstration purposes.
CSS
table {
width: 300px;
overflow-x: scroll;
display: block;
}
thead, tbody {
display: block;
}
tbody {
overflow-y: scroll;
overflow-x: hidden;
height: 140px;
}
td, th {
min-width: 100px;
}
JS
$("table").on("scroll", function () {
$("table > *").width($("table").width() + $("table").scrollLeft());
});
I needed a version which degrades nicely in IE9 (no scrolling, just a normal table). Posting the fiddle here as it is an improved version. All you need to do is set a height on the tr
.
Additional CSS to make this solution degrade nicely in IE9
tr {
height: 25px; /* This could be any value, it just needs to be set. */
}
Here's a jsFiddle demonstrating the nicely degrading in IE9 version of this solution.
Edit: Updated fiddle links to link to a version of the fiddle which contains fixes for issues mentioned in the comments. Just adding a snippet with the latest and greatest version while I'm at it:
$('table').on('scroll', function() {_x000D_
$("table > *").width($("table").width() + $("table").scrollLeft());_x000D_
});
_x000D_
html {_x000D_
font-family: verdana;_x000D_
font-size: 10pt;_x000D_
line-height: 25px;_x000D_
}_x000D_
_x000D_
table {_x000D_
border-collapse: collapse;_x000D_
width: 300px;_x000D_
overflow-x: scroll;_x000D_
display: block;_x000D_
}_x000D_
_x000D_
thead {_x000D_
background-color: #EFEFEF;_x000D_
}_x000D_
_x000D_
thead,_x000D_
tbody {_x000D_
display: block;_x000D_
}_x000D_
_x000D_
tbody {_x000D_
overflow-y: scroll;_x000D_
overflow-x: hidden;_x000D_
height: 140px;_x000D_
}_x000D_
_x000D_
td,_x000D_
th {_x000D_
min-width: 100px;_x000D_
height: 25px;_x000D_
border: dashed 1px lightblue;_x000D_
overflow: hidden;_x000D_
text-overflow: ellipsis;_x000D_
max-width: 100px;_x000D_
}
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>_x000D_
<table>_x000D_
<thead>_x000D_
<tr>_x000D_
<th>Column 1</th>_x000D_
<th>Column 2</th>_x000D_
<th>Column 3</th>_x000D_
<th>Column 4</th>_x000D_
<th>Column 5</th>_x000D_
</tr>_x000D_
</thead>_x000D_
<tbody>_x000D_
<tr>_x000D_
<td>AAAAAAAAAAAAAAAAAAAAAAAAAA</td>_x000D_
<td>Row 1</td>_x000D_
<td>Row 1</td>_x000D_
<td>Row 1</td>_x000D_
<td>Row 1</td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td>Row 2</td>_x000D_
<td>Row 2</td>_x000D_
<td>Row 2</td>_x000D_
<td>Row 2</td>_x000D_
<td>Row 2</td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td>Row 3</td>_x000D_
<td>Row 3</td>_x000D_
<td>Row 3</td>_x000D_
<td>Row 3</td>_x000D_
<td>Row 3</td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td>Row 4</td>_x000D_
<td>Row 4</td>_x000D_
<td>Row 4</td>_x000D_
<td>Row 4</td>_x000D_
<td>Row 4</td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td>Row 5</td>_x000D_
<td>Row 5</td>_x000D_
<td>Row 5</td>_x000D_
<td>Row 5</td>_x000D_
<td>Row 5</td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td>Row 6</td>_x000D_
<td>Row 6</td>_x000D_
<td>Row 6</td>_x000D_
<td>Row 6</td>_x000D_
<td>Row 6</td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td>Row 7</td>_x000D_
<td>Row 7</td>_x000D_
<td>Row 7</td>_x000D_
<td>Row 7</td>_x000D_
<td>Row 7</td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td>Row 8</td>_x000D_
<td>Row 8</td>_x000D_
<td>Row 8</td>_x000D_
<td>Row 8</td>_x000D_
<td>Row 8</td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td>Row 9</td>_x000D_
<td>Row 9</td>_x000D_
<td>Row 9</td>_x000D_
<td>Row 9</td>_x000D_
<td>Row 9</td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td>Row 10</td>_x000D_
<td>Row 10</td>_x000D_
<td>Row 10</td>_x000D_
<td>Row 10</td>_x000D_
<td>Row 10</td>_x000D_
</tr>_x000D_
</tbody>_x000D_
</table>
_x000D_