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 cannot get either scrollbar to appear.
First, make a display: block of your table
then, 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%;
}
white-space: nowrap;
helps immediately see the scrollbar.Did you try CSS
overflow
property?UPDATE As other users are pointing out, this is not enough to add the scrollbars. So please, see and upvote comments and answers below.
display: block
.display: block
on tables - surprisingly it strips them of their semantics! This harms accessibility and makes life difficult for users of screen readers. See here developer.paciellogroup.com/blog/2018/03/…Wrap the table in a DIV, set with the following style:
overflow:auto
here is unecessary. Do you know of anyway to achieve this without setting the width... that always seems to be the solution in html - hardcode some width or height but that seems to defeat the point of having a layout engine!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.
white-space: nowrap;
seems to be optional.display: block;
Use the CSS attribute "overflow" for this.
Short summary:
e.g.
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
.This worked for me in Firefox, Chrome, and Safari on Mac.
display: block
on tables - surprisingly it strips them of their semantics! This harms accessibility and makes life difficult for users of screen readers. See here developer.paciellogroup.com/blog/2018/03/…table tbody{...}
styles don't get applied to my mat-table... Anyone have similar problems?I couldn't get any of the above solutions to work. However, I found a hack:
body { background-color: #ccc; } .container { width: 300px; background-color: white; } table { width: 100%; border-collapse: collapse; } td { border: 1px solid black; } /* try removing the "hack" below to see how the table overflows the .body */ .hack1 { display: table; table-layout: fixed; width: 100%; } .hack2 { display: table-cell; overflow-x: auto; width: 100%; }
I was running into the same issue. I discovered the following solution, which has only been tested in Chrome v31:
table-layout: fixed
is nice.tbody {...}
styles don't seem to get applied to my mat-table. Any idea why?Insert the table inside a div, so the table will take full length
HTML
CSS
With bootstrap
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.
The 'more than 100% width' on the table really made it work for me.
.table-wrap { width: 100%; overflow: auto; } table { table-layout: fixed; width: 200%; }
add tag table to div element with style="overflow-x:auto"
This is what worked for me
For what it's worth, the best answer I found was here: https://github.com/filamentgroup/tablesaw/issues/58#issuecomment-63966574
I tried all the above solutions but had some issues with them.
If we add
display: 'block'
to the table, the cells do not occupy the full width. If we add it to the table wrapper, your custom table header like search, filter etc will also scroll which will look bad.I was able to achieve the expected behaviour by adding
overflow-x: auto
to the body wrapper of the table.Cells take full width even with less columns and a scroll bar appears automatically as needed.
Like already stated, using
display:block;
on table is bad. I tried most of the answers in this thread, none worked as I wanted. If your HTML is structured like this:And you want the parent div to be horizontally scrollable, you can try the following:
.text-left {text-align:left;} /* Ignore */ .x-auto { overflow-x: auto; } .table { text-align: left; table-layout: fixed; width: 100%; white-space: nowrap; } .table tbody { display: table; width: 100%; }
Follow WeChat
Success story sharing
Want to stay one step ahead of the latest teleworks?
Subscribe Now相似问题
HuntsBot,a one-stop outsourcing task, remote job, product ideas sharing and subscription platform, which supports DingTalk, Lark, WeCom, Email and Telegram robot subscription. The platform will push outsourcing task requirements, remote work opportunities, product ideas to every subscribed user with timely, stable and reliable.
Platform
Support
Contact US
Any questions or suggestions during use, you can contact us in the following ways:
Email: huntsbot@xinbeitime.com
微信公众号
Tips