【问题标题】:How do you make a CSS-defined table-cell scroll?如何制作 CSS 定义的表格单元格滚动?
【发布时间】:2010-04-29 22:23:15
【问题描述】:

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

考虑以下代码:(查看它的实际操作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 属性设置的范围。

希望我已经充分解释了这种情况......

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

【问题讨论】:

  • 你不能用普通的桌子来做这件事吗?看起来非常h​​acky。
  • @fig-gnuton:实际上,普通的桌子也不起作用(更不用说多么陈旧了)。我尝试过的 HTML 表格一开始就没有响应设定的高度,更不用说正确滚动了。这可能与我在一堆由 ASP.NET 提供支持的更现代的 XHTML 代码中间使用它们有关。我从来没有亲眼见过 HTML 表格在那种情况下表现得很好......
  • Giffyguy,注意到,发布答案。

标签: html css scroll rendering css-tables


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    要获得这种行为,您可能必须避免使用 CSS 表格模型,并创建自己的带有浮动、设置宽度/高度等的表格样式显示。

    【讨论】:

    • 我对这个解决方案的唯一担心是我试图避免在 JavaScript 中手动呈现它。几周以来,我一直在尝试让 JavaScript 设置这些元素的大小,并决定纯声明性的解决方案更可取。真的不可能吗?
    • @Giffyguy,我并不是说你必须使用 JavaScript。只是建议使用典型的 CSS。但也许我没有得到你想要的。
    • 有趣。也许是我没有得到你的建议。所以你的意思是,如果我对所有内容都使用单独的 div,将它们浮动到我想要的地方,并自己设置大小,那么一切都应该工作,对吧?问题是内容是动态的,从数据库加载,并且大小未知。我只是希望能够尽可能多地展示它,但我认为 div 的固定尺寸至少在短期内是可以接受的解决方案。
    • Giffyguy,对 - 如果您设置每个 div 的宽度和高度以及适当的溢出,则进入其中的动态内容是否太大无关紧要 - 您将获得滚动条。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多