溢出:在表格单元格中滚动 - 在 Chrome 中有效,在其他地方无效

发布于 2024-10-31 07:15:08 字数 1309 浏览 1 评论 0原文

我试图让滚动条显示在表格单元格中,其内容可能会比原始表格单元格大小更大,并且我不希望整个表格伸展。

这是 jfiddle 上的示例。正如你所看到的,它在 Google Chrome 中工作,但在 Firefox 和 IE 中不起作用:它们的行为就像根本没有溢出:滚动一样。没有在 Safari 中尝试过,但我怀疑这也行不通。

那么,到底是谁的错,是 Chrome 实施了不应该支持的东西,还是其他所有的人? 更新:需要明确的是,溢出的不是单元格本身,而是我放入其中的 div。代码如下所示:

<table>
    <tr>
        <td class="header">
            <div class="header">Header</div>
        </td>
    </tr>
    <tr>
        <td>
            <div class="scroll">
                ... VERY BIG CONTENT HERE ... 
            </div>
        </td>
    </tr>
    <tr>
      <td class="footer">
        <div class="footer">Footer</div>
      </td>
    </tr>
</table>

您可以在 jfiddle 上查看全部内容。

我想要实现的是一种布局,通过该布局,页眉和页脚之间的可用空间被完全填充并且如果空间耗尽,那么会获得一个滚动条,而不是整个窗口。我已经明白,即使是在 Stack Overflow 上涉水,仅使用 CSS,仅使用 DIV,这是无法实现的(使用绝对定位不是一个选项,因为元素是从正常流程中取出的,并且“极端”窗口大小调整将导致元素重叠)。

更新: flexbox 模型 似乎正是如此在这些情况下需要。然而,我认为我们仍然不能依赖它的广泛实施。

I'm trying to get a scrollbar show up in a table cell whose content may grow bigger than the original table cell size, and I don't want the whole table to stretch out.

Here's the example on jfiddle. As you can see, it works in Google Chrome, but doesn't work in Firefox nor in IE: they just behave as if there was no overflow:scroll at all. Haven't tried it in Safari, but I suspect that won't work either.

So, who's at fault here, Chrome for implementing something which isn't supposed to be supported, or all the others? Update: to be clear, it's not the cell itself that is being overflowed, but a div I put into it. The code looks like this:

<table>
    <tr>
        <td class="header">
            <div class="header">Header</div>
        </td>
    </tr>
    <tr>
        <td>
            <div class="scroll">
                ... VERY BIG CONTENT HERE ... 
            </div>
        </td>
    </tr>
    <tr>
      <td class="footer">
        <div class="footer">Footer</div>
      </td>
    </tr>
</table>

You can see it all over at jfiddle.

What I'm trying to achieve is a layout by which the available space between the header and the footer is completely filled out and if the space gets exhausted then it gets a scrollbar, not the whole window. I've come to understand, even by wading here on Stack Overflow, that With CSS alone, using just DIV's, this is not achievable (using absolute positioning isn't an option, because the elements are taken out of the normal flow and an "extreme" window resizing will cause elements to overlap).

Update: the flexbox model seems to be exactly what is needed in these cases. However, I suppose we can't rely on it being implemented broadly, still.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

温柔一刀 2024-11-07 07:15:08

这就是您想要实现的目标 http://www.cssplay.co.uk/layouts /basics2.html

Is this what you are trying to achieve http://www.cssplay.co.uk/layouts/basics2.html?

鱼忆七猫命九 2024-11-07 07:15:08

如果您查看 CSS 规范,您会发现在表元素上使用 positionoverflow 等规则的结果实际上是未定义的。事实上,表格有自己复杂的布局规则,与许多 CSS 规则相冲突,而且规范几乎让浏览器自行决定如何做。

因此,回答你的部分问题,这确实不是任何人的错。没有哪个浏览器是正确或错误的。

至于该怎么做,你最好的选择是不使用表格,如果失败,请尝试在表格单元格中嵌入一个 div 并应用你的规则。

已更新您的编辑

您尝试执行的操作与表布局算法相冲突。默认情况下,表格的作用是调整单元格的大小以适应行/列的内容。尽管滚动条你试图阻止它做规范中未定义的事情。浏览器没有“正确”的方法来完成您对表格单元格的要求。

除了依靠黑客和运气之外,您唯一真正的解决方案是倾听人们的意见,并停止尝试使用表格来实现此布局。您的设计中没有什么是通过 3 个 div 和正确的布局类型无法实现的 - 特别是如果您不关心 IE6。这是您应该追求的理想解决方案。

If you look in the CSS spec you'll find the result of using rules like position and overflow on table elements are actually undefined. In fact tables have their own complicated layout rules that conflict with many CSS rules and the spec pretty much leaves it up to browsers to do what they want.

So to answer part of your question, it's really nobody's fault. No browser is right or wrong.

As for what to do about it you best option is to not use a table and failing that try embedding a div in the table cell and apply your rules to that.

UPDATED FOR YOUR EDIT

What you are trying to do conflicts with the table layout algorithm. What tables do by default is resize their cells to fit the contents of the row/column. Scrollbar notwithstanding You're trying to stop it doing that which is something not defined in the spec. There is no 'correct' way for browsers to do what you're asking with a table cell.

Your only real solution, other than relying on hacks and luck, is to listen to what people are saying and stop trying to achieve this layout using a table. There is nothing in your design that couldn't be achieved with 3 divs and right type of layout - especially if you don't care about IE6. That is the ideal solution you should be aiming for.

八巷 2024-11-07 07:15:08

难道你不能把它分成三个单独的表格,中间的表格被一个 div 包围,这样你就可以对其应用滚动吗?

div {
    overflow-y:scroll;
    //set height to however large you would like your scrollable area to be
}
<table>
    <tr>
        <td class="header">
            <div class="header">Header</div>
        </td>
    </tr>
</table
<div>
    <table>
        <tr>
            <td>
                <div class="scroll">
                    ... VERY BIG CONTENT HERE ... 
                </div>
            </td>
        </tr>
    </table>
</div>
<table>
    <tr>
        <td class="footer">
            <div class="footer">Footer</div>
        </td>
    </tr>
</table>

Couldn't you just make that into three separate tables with the middle table surrounded by a div so you can apply a scroll to it?

div {
    overflow-y:scroll;
    //set height to however large you would like your scrollable area to be
}
<table>
    <tr>
        <td class="header">
            <div class="header">Header</div>
        </td>
    </tr>
</table
<div>
    <table>
        <tr>
            <td>
                <div class="scroll">
                    ... VERY BIG CONTENT HERE ... 
                </div>
            </td>
        </tr>
    </table>
</div>
<table>
    <tr>
        <td class="footer">
            <div class="footer">Footer</div>
        </td>
    </tr>
</table>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文