I tried to figure this out myself recently, and I came up with a good solution that works fine in my browser (Chrome 51) and supports dynamic column widths . I should mention that after I myself received my answer, I also found a similar technique described elsewhere on the Internet ...
The trick is to use two identical tables located one above the other. The bottom table is visible, and the top table is invisible to the header. The top table also has pointer-events: none mounted on the body, so mouse interactions fall into the bottom table. Thus, the bottom table scrolls under the heading of the top table.
For everything you need for layout and resizing (when the user adjusts the screen width, for example), both tables should have the same scroll behavior. However, the top scroll bar of the table is ignored thanks to pointer-events: none and can be invisible with:
<style> .hiddenScrollbar::-webkit-scrollbar { background-color: transparent; } </style>
Here is the complete code:
<html> <head> <style> td { border: 1px solid black; white-space: nowrap; } th { background-color: gray; border: 1px solid black; white-space: nowrap; } .hiddenScrollbar::-webkit-scrollbar { background-color: transparent; } </style> </head> <body> Table test. Use mouse wheel to scroll or scroll to the right to see vertical scroll bar. You can also remove the outermost div if you don't want a horizontal scroll bar. <br/> <div style="display: inline-block; height: 10em; width: 15em; overflow-x: scroll; overflow-y: hidden"> <div style="position: relative;"> <div style="display: inline-block; position: absolute; left: 0px; top: 0px; height: 10em; overflow-y: scroll"> <table style="border-collapse: collapse;"> <thead> <tr> <th>Column 1</th> <th>Another Column</th> </tr> </thead> <tbody> <tr> <td>Data 1</td> <td>123409213750213</td> </tr> <tr> <td>Data 2</td> <td>123409213750213</td> </tr> <tr> <td>Data 3</td> <td>123409213750213</td> </tr> <tr> <td>Data 4</td> <td>123409213750213</td> </tr> <tr> <td>Data 5</td> <td>123409213750213</td> </tr> <tr> <td>Data 6</td> <td>12340921375021342354235 very long...</td> </tr> <tr> <td>Data 7</td> <td>123409213750213</td> </tr> <tr> <td>Data 8</td> <td>123409213750213</td> </tr> <tr> <td>Data 9</td> <td>123409213750213</td> </tr> <tr> <td>Data 10</td> <td>123409213750213</td> </tr> <tr> <td>Data 11</td> <td>123409213750213</td> </tr> <tr> <td>Data 12</td> <td>123409213750213</td> </tr> </tbody> </table> </div> <div class="hiddenScrollbar" style="display: inline-block; pointer-events: none; position: relative; left: 0px; top: 0px; height: 10em; overflow-y: scroll"> <table style="border-collapse: collapse;"> <thead style="pointer-events: auto"> <tr> <th>Column 1</th> <th>Another Column</th> </tr> </thead> <tbody style="visibility: hidden"> <tr> <tr> <td>Data 1</td> <td>123409213750213</td> </tr> <tr> <td>Data 2</td> <td>123409213750213</td> </tr> <tr> <td>Data 3</td> <td>123409213750213</td> </tr> <tr> <td>Data 4</td> <td>123409213750213</td> </tr> <tr> <td>Data 5</td> <td>123409213750213</td> </tr> <tr> <td>Data 6</td> <td>12340921375021342354235 very long...</td> </tr> <tr> <td>Data 7</td> <td>123409213750213</td> </tr> <tr> <td>Data 8</td> <td>123409213750213</td> </tr> <tr> <td>Data 9</td> <td>123409213750213</td> </tr> <tr> <td>Data 10</td> <td>123409213750213</td> </tr> <tr> <td>Data 11</td> <td>123409213750213</td> </tr> <tr> <td>Data 12</td> <td>123409213750213</td> </tr> </tr> </tbody> </table> </div> </div> </div><br/> Stuff after table. </body> </html>
Caveats: additional work is required to prove that this works in other browsers. I was also quite liberal in mixing inline styles and styles. However, I believe that a general concept is the best way to do this, since the target browser supports it. The only problems with the functionality / display is that if you want a horizontal scrollbar, the vertical scrollbar can scroll from the view (as shown in the snippet). However, you can scroll with the mouse wheel. In addition, you cannot have a transparent background in the header (otherwise the bottom table will be displayed). Finally, you need a way to create two identical tables. Personally, I use response.js and itโs easy to react with it, but php or another server generation or javascript will work as well.
bruceceng Aug 04 '16 at 18:07 2016-08-04 18:07
source share