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

发布于 2024-08-30 10:22:59 字数 1709 浏览 6 评论 0原文

我希望能够设置表格的高度,并强制单元格单独滚动(如果它们比表格大)。

考虑以下代码:(请此处查看其实际情况)

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一紙繁鸢 2024-09-06 10:23:00

一个很老的问题;但当我搜索同一主题时,在 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!.

魂ガ小子 2024-09-06 10:23:00

要获得这种行为,您可能必须避免使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文