是否有一种方法可以将水平滚动条添加到HTML表中?我实际上需要它是可滚动的,垂直和水平取决于表如何增长,但我不能得到任何滚动条出现。
当前回答
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浏览器上都能使用这种方法。
其他回答
不管怎样,我找到的最好答案是: https://github.com/filamentgroup/tablesaw/issues/58#issuecomment-63966574
table.tablesaw
{
table-layout: fixed;
max-width: none;
width: auto;
min-width: 100%;
}
将表格插入到div中,这样表格就会有完整的长度
HTML
<div class="scroll">
<table> </table>
</div>
CSS
.scroll{
overflow-x: auto;
white-space: nowrap;
}
如前所述,使用display:block;在桌子上是坏的。我尝试了这个帖子中的大多数答案,没有一个像我想要的那样有效。如果你的HTML是这样结构的:
<div>
<table>
<tbody>
你想让父div水平滚动,你可以试试下面的方法:
.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%; } <div class="x-auto"> <table class="table text-left"> <tbody> <tr> <th>Head1</th> <th>Head2</th> </tr> <tr> <td>Some short text!</td> <td>Some really long text, like really really really really really really really really really really really really long!</td> </tr> </tbody> </table> </div>
我不能让上面的任何解决方案工作。然而,我发现了一个黑客:
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>
.wrapper {
width: 0;
min-width: 100%; //width 0, but min-width 100?? yes, I know...
overflow: auto;
}
<div class="wrapper">
<table></table>
</div>
表可以有任何宽度。我通常使用100%或max-content作为表。
推荐文章
- 向HTML表中添加水平滚动条
- 在notepad++中格式化代码
- 表单中包含表单,可以吗?
- 如何创建表只使用<div>标签和Css
- html5 - canvas元素-多层
- 如何确保<select>表单字段被禁用时提交?
- 如何在HTML文本换行?
- 如何在JavaScript中转义单引号(')?
- Chrome开发工具:如何找出什么是覆盖CSS规则?
- 如何使用HTML/CSS在文本中插入空格/制表符
- 用背景图像填充SVG路径元素
- 将JSON显示为HTML
- HTML被认为是一种编程语言吗?
- 如何修复favicon.ico错误:“Failed to load resource: net::ERR_EMPTY_RESPONSE”
- 大Glyphicons