Is there a way to add a Horizontal scrollbar to an HTML table? I actually need it to be scrollable both vertically and horizontally depending on how the table grows but I can't get either scrollbar to appear.
This question is related to
html
css
html-table
scrollbar
Wrap the table in a DIV, set with the following style:
div.wrapper {
width: 500px;
height: 500px;
overflow: auto;
}
This is an improvement of Serge Stroobandt's answer and works perfectly. It solves the issue of the table not filling the whole page width if it has less columns.
<style>
.table_wrapper{
display: block;
overflow-x: auto;
white-space: nowrap;
}
</style>
<div class="table_wrapper">
<table>
...
</table>
</div>
Edit: @WickyNilliams has noted that setting display: block on a table body will strip the table of semantics and thus is not a good solution due to accessibility issues.
I had good success with the solution proposed by @Serge Stroobandt, but I encountered the problem @Shevy had with the cells then not filling the full width of the table. I was able to fix this by adding some styles to the tbody
.
table {
display: block;
overflow-x: auto;
white-space: nowrap;
}
table tbody {
display: table;
width: 100%;
}
This worked for me in Firefox, Chrome, and Safari on Mac.
Use the CSS attribute "overflow" for this.
Short summary:
overflow: visible|hidden|scroll|auto|initial|inherit;
e.g.
table {
display: block;
overflow: scroll;
}
display: block
of your tablethen, set overflow-x:
to auto
.
table {
display: block;
overflow-x: auto;
white-space: nowrap;
}
Nice and clean. No superfluous formatting.
Here are more involved examples with scrolling table captions from a page on my website.
If an issue is taken about cells not filling the entire table, append the following additional CSS code:
table tbody {
display: table;
width: 100%;
}
//Representation of table
<div class="search-table-outter">
<table class="table table-responsive search-table inner">
</table>
</div>
//Css to make Horizontal Dropdown
<style>
.search-table{table-layout: auto; margin:40px auto 0px auto; }
.search-table, td, th {
border-collapse: collapse;
}
th{padding:20px 7px; font-size:15px; color:#444;}
td{padding:5px 10px; height:35px;}
.search-table-outter { overflow-x: scroll; }
th, td { min-width: 200px; }
</style>
I couldn't get any of the above solutions to work. However, I found a hack:
body {_x000D_
background-color: #ccc;_x000D_
}_x000D_
_x000D_
.container {_x000D_
width: 300px;_x000D_
background-color: white;_x000D_
}_x000D_
_x000D_
table {_x000D_
width: 100%;_x000D_
border-collapse: collapse;_x000D_
}_x000D_
_x000D_
td {_x000D_
border: 1px solid black;_x000D_
}_x000D_
_x000D_
/* try removing the "hack" below to see how the table overflows the .body */_x000D_
.hack1 {_x000D_
display: table;_x000D_
table-layout: fixed;_x000D_
width: 100%;_x000D_
}_x000D_
_x000D_
.hack2 {_x000D_
display: table-cell;_x000D_
overflow-x: auto;_x000D_
width: 100%;_x000D_
}
_x000D_
<div class="container">_x000D_
_x000D_
<div class="hack1">_x000D_
<div class="hack2">_x000D_
_x000D_
<table>_x000D_
<tr>_x000D_
<td>table or other arbitrary content</td>_x000D_
<td>that will cause your page to stretch</td>_x000D_
</tr>_x000D_
<tr>_x000D_
<td>uncontrollably</td>_x000D_
<td>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</td>_x000D_
</tr>_x000D_
</table>_x000D_
_x000D_
</div>_x000D_
</div>_x000D_
_x000D_
</div>
_x000D_
The 'more than 100% width' on the table really made it work for me.
.table-wrap {_x000D_
width: 100%;_x000D_
overflow: auto;_x000D_
}_x000D_
_x000D_
table {_x000D_
table-layout: fixed;_x000D_
width: 200%;_x000D_
}
_x000D_
I was running into the same issue. I discovered the following solution, which has only been tested in Chrome v31:
table {
table-layout: fixed;
}
tbody {
display: block;
overflow: scroll;
}
I figured out this answer based on previous solution and it's comment and added some adjustments of my own. This works for me on the responsive table.
table {
display: inline-block;
overflow-x: auto;
white-space: nowrap;
// make fixed table width effected by overflow-x
max-width: 100%;
// hide all borders that make rows not filled with the table width
border: 0;
}
// add missing borders
table td {
border: 1px solid;
}
With bootstrap
<div class="table-responsive">
<table class="table">
...
</table>
</div>
Insert the table inside a div, so the table will take full length
HTML
<div class="scroll">
<table> </table>
</div>
CSS
.scroll{
overflow-x: auto;
white-space: nowrap;
}
Source: Stackoverflow.com