如何制作 CSS 定义的表格单元格滚动?
我希望能够设置表格的高度,并强制单元格单独滚动(如果它们比表格大)。
考虑以下代码:(请此处查看其实际情况)
<div style="display: table; position: absolute;
width: 25%; height: 80%; min-height: 80%; max-height: 80%;
left: 0%; top: 10%; right: 75%; bottom: 10%; border: solid 1px black;">
<div style="display: table-row;">
<div style="display: table-cell; border: solid 1px blue;">
{Some dynamic text content}<br/>
This cell should shrink to fit its contents.
</div>
</div>
<div style="display: table-row;">
<div style="display: table-cell; border: solid 1px red;
overflow: scroll;">
This should only take up the remainder of the table's vertical space.
This should only take up the remainder of the table's vertical space.
This should only take up the remainder of the table's vertical space.
This should only take up the remainder of the table's vertical space.
This should only take up the remainder of the table's vertical space.
This should only take up the remainder of the table's vertical space.
This should only take up the remainder of the table's vertical space.
This should only take up the remainder of the table's vertical space.
</div>
</div>
</div>
如果您打开此代码(在 IE8 中,就我而言)您会注意到,当浏览器最大化时,第二个单元格非常适合表格。理论上,当您缩小浏览器(强制表格也缩小)时,当表格变得太小而无法容纳所有内容时,第二个单元格内应该出现垂直滚动条。但实际上,表格只是垂直增长,超出了 CSS height
属性设置的范围。
希望我已经充分解释了这个场景......
有谁知道我如何让它发挥作用?
I want to be able to set the height of the table, and force the cells to scroll individually if they are larger than the table.
Consider the following code: (see it in action here)
<div style="display: table; position: absolute;
width: 25%; height: 80%; min-height: 80%; max-height: 80%;
left: 0%; top: 10%; right: 75%; bottom: 10%; border: solid 1px black;">
<div style="display: table-row;">
<div style="display: table-cell; border: solid 1px blue;">
{Some dynamic text content}<br/>
This cell should shrink to fit its contents.
</div>
</div>
<div style="display: table-row;">
<div style="display: table-cell; border: solid 1px red;
overflow: scroll;">
This should only take up the remainder of the table's vertical space.
This should only take up the remainder of the table's vertical space.
This should only take up the remainder of the table's vertical space.
This should only take up the remainder of the table's vertical space.
This should only take up the remainder of the table's vertical space.
This should only take up the remainder of the table's vertical space.
This should only take up the remainder of the table's vertical space.
This should only take up the remainder of the table's vertical space.
</div>
</div>
</div>
If you open this code (in IE8, in my case) you'll notice that the second cell fits in the table nicely when the browser is maximized. In theory, when you shrink the browser (forcing the table to shrink as well), a vertical scrollbar should appear INSIDE the second cell when the table becomes too small to fit all of the content. But in reality, the table just grows vertically, beyond the bounds set by the CSS height
attribute(s).
Hopefully I've explained this scenario adequately...
Does anyone know how I can get this to work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个很老的问题;但当我搜索同一主题时,在 Google 中仍然排名第二。
我发现的简单解决方案是将“需要滚动 div”的内容包装到另一个 div 中。
不知道为什么;但带有“display:table-cell”的元素不会滚动。
但它们内部的元素可以。所以在里面放一个div并设置它滚动!。
A very old question; but still No. 2 in Google when I searched for the same topic.
The simple solution what I found is to Wrap the contents of the "need to scroll div" into another div.
Don't know why; but elements with "display:table-cell" don't scroll.
But elements inside them can. So put a div inside and set it to scroll!.
要获得这种行为,您可能必须避免使用 CSS 表格模型,并创建自己的表格样式显示,包括浮动、设置宽度/高度等。
To get that behavior you may have to just avoid the CSS table model, and create your own table-style display with floating, setting widths/heights, etc.