是否有一种方法可以将水平滚动条添加到HTML表中?我实际上需要它是可滚动的,垂直和水平取决于表如何增长,但我不能得到任何滚动条出现。
当前回答
这是对Serge Stroobandt的答案的改进,效果非常好。它解决了表没有填充整个页面宽度的问题,如果它有较少的列。
<style>
.table_wrapper{
display: block;
overflow-x: auto;
white-space: nowrap;
}
</style>
<div class="table_wrapper">
<table>
...
</table>
</div>
其他回答
用下面的样式将表格包装成DIV:
div.wrapper {
width: 500px;
height: 500px;
overflow: auto;
}
为此使用CSS属性“overflow”。
简短的总结:
overflow: visible|hidden|scroll|auto|initial|inherit;
e.g.
table {
overflow: scroll;
}
我尝试了以上所有的解决方案,但都有一些问题。
如果我们添加display: 'block'到表格中,单元格不会占用整个宽度。 如果我们将它添加到表包装器中,你的自定义表头,如搜索,过滤器等也会滚动,这看起来很糟糕。
我能够通过将overflow-x: auto添加到表的主体包装器来实现预期的行为。
单元格采用全宽度,即使列较少,滚动条也会根据需要自动出现。
你试过CSS溢出属性了吗?
overflow: scroll; /* Scrollbar are always visible */
overflow: auto; /* Scrollbar is displayed as it's needed */
更新 正如其他用户指出的那样,仅仅添加滚动条是不够的。 所以,请参阅下面的评论和回答并投票。
我不能让上面的任何解决方案工作。然而,我发现了一个黑客:
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>
推荐文章
- 向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