html - 如何制作 CSS 定义的表格单元格滚动条?

标签 html css scroll rendering css-tables

我希望能够设置表格的高度,并强制单元格在大于表格时单独滚动。

考虑以下代码:(查看实际操作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>

如果您打开这段代码(在我的例子中是在 IE8 中),您会注意到当浏览器最大化时,第二个单元格很好地适合表格。理论上,当您缩小浏览器(强制表格也缩小)时,当表格变得太小无法容纳所有内容时,垂直滚动条应该出现在第二个单元格内。但实际上,表格只是垂直增长,超出了 CSS height 属性设置的范围。

希望我已经充分解释了这个场景......

有谁知道我怎样才能让它工作?

最佳答案

一个很老的问题;但当我搜索同一主题时,它在 Google 中仍然排名第二。

我找到的简单解决方案是将“需要滚动 div”的内容包装到另一个 div 中。 不知道为什么;但带有“display:table-cell”的元素不会滚动。 但是它们内部的元素可以。因此,将一个 div 放入其中并将其设置为滚动!。

关于html - 如何制作 CSS 定义的表格单元格滚动条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2741169/

相关文章:

jquery - 单击按钮复制到剪贴板

javascript - 区分用户启动的滚动和页面加载时发生的浏览器启动的滚动

html - Div 容器不按顺序

css - 使用移动 CSS Nav 强制内容向下

javascript - 我可以让视差幻灯片停止在页面的中间而不是顶部吗?

javascript - 更改路线不会在新页面中滚动到顶部

javascript - 向 Javascript 脚本添加更多 ID 或类

javascript - DataTable 在小型设备上无法正确呈现

html - 如何使用 bootstrap 3 将一组按钮居中放置到 div 中

html - Chrome 在使用 jQuery ajax 时删除表单元素