是否有一种方法可以将水平滚动条添加到HTML表中?我实际上需要它是可滚动的,垂直和水平取决于表如何增长,但我不能得到任何滚动条出现。


当前回答

这对我来说很管用

.wrapper {
  overflow-x: auto;
  white-space: nowrap;
}

.wrapper table {
  width: auto;
  min-width: 100%;
}

<div class="wrapper">
   <table>...</table>
</div>

其他回答

我也遇到了同样的问题。我发现了下面的解决方案,它只在Chrome v31中测试过:

table {
    table-layout: fixed;
}

tbody {
    display: block;
    overflow: scroll;
}

我不能让上面的任何解决方案工作。然而,我发现了一个黑客:

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%; } <div class="container"> <div class="hack1"> <div class="hack2"> <table> <tr> <td>table or other arbitrary content</td> <td>that will cause your page to stretch</td> </tr> <tr> <td>uncontrollably</td> <td>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</td> </tr> </table> </div> </div> </div>

桌子上“超过100%的宽度”真的很适合我。

.table-wrap { 宽度:100%; 溢出:汽车; } 表{ 表布局:固定; 宽度:200%; }

Edit: @WickyNilliams注意到,在表体上设置display: block将剥夺表的语义,因此由于可访问性问题,这不是一个很好的解决方案。

我对@Serge Stroobandt提出的解决方案取得了很好的成功,但我遇到了@Shevy的问题,这些单元格没有填满表的全宽度。我能够通过向tbody添加一些样式来修复这个问题。

table {
  display: block;
  overflow-x: auto;
  white-space: nowrap;
}

table tbody {
  display: table;
  width: 100%;
}

我在Mac上的火狐、Chrome和Safari浏览器上都能使用这种方法。

为div元素添加标签表,style="overflow-x:auto"

<div style="overflow-x:auto">
<table class="table table-bordered">
    <thead>
        <tr>
            <th><b>Name</b></th>
            <th><b>Username</b></th>
            <th><b>Email</b></th>
            <th><b>Avatar</b></th>
            <th><b>Status</b></th>
            <th><b>Action</b></th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>