CSS:为什么当使用溢出:隐藏时,vertical-align:baseline在Firefox上停止工作?

发布于 2024-10-04 16:38:41 字数 882 浏览 3 评论 0原文

您可以通过运行此 测试来重现此问题案例。结果如下面的屏幕截图所示。问题是,在 Firefox 上,当在“块”(屏幕截图中带有灰色背景)上添加 overflow:hidden 时,该块停止按照我希望的方式对齐:而不是块中文本的基线与父框的基线对齐,就好像块的底部与父框的基线对齐一样。正如您在屏幕截图中看到的,Chrome 不会发生这种情况。

  1. 这是 Firefox 的错误吗?
  2. 如何在 Firefox 上获得预期结果(与 overflow:hidden 进行基线对齐)?

Screenshot

注意:使用 vertical-align :“块”上的 middle 并不能消除它,因为我真正想要的是基线对齐。通过设置 padding: 1em 0 .1em 0(在框的顶部有更多的内边距)可以更清楚地看到 vertical-align: middle 并没有进行基线对齐,这会给你:

With Vertical-align: middle

You can reproduce this by running this test case. The results are shown in the screenshot below. The issue is that on Firefox, when adding a overflow: hidden on the "block" (with grey background in the screenshot), the block stop being aligned as I'd like it to be: instead of the baseline of the text in the block being align to the the baseline of the parent box, it is as if the bottom of the block was aligned on the baseline of the parent box. As you can see in the screenshot, this doesn't happen with Chrome.

  1. Is this a Firefox bug?
  2. How to get the expected result on Firefox (baseline alignment with overflow: hidden)?

Screenshot

Note: Using vertical-align: middle on "block" doesn't cut it, as what I really want is baseline alignment. You can see more clearly that vertical-align: middle doesn't do baseline alignment by setting padding: 1em 0 .1em 0 (more padding at the top of the box), which give you:

With vertical-align: middle

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

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

发布评论

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

评论(3

半世晨晓 2024-10-11 16:38:41

它看起来确实像溢出:隐藏是有问题的,因为它从内联块元素中删除了基线。幸运的是,有一个看似多余的 Mozilla CSS 溢出扩展值可以防止溢出,但不会表现出这种错误行为。

试试这个:

.block {
    width: 10em; padding: .3em 0 .1em 0;
    overflow: hidden;
    overflow: -moz-hidden-unscrollable;
    white-space: nowrap;
    display: inline-block;
    border: 1px solid #666; background-color: #eee;    
}

看起来它解决了 Firefox 中的问题,并且不会干扰 Safari。

更新:

看起来 Firefox 和 Opera 正在正确渲染溢出:隐藏的内联块,而 Webkit 浏览器则不然。

根据W3C CSS2 工作草案的视觉格式化模型详细信息 ,

“内联块”的基线是
最后一个行框的基线
正常流程,除非有
没有流入线框或者如果
'overflow' 属性有一个计算的
除“可见”之外的值,其中
情况下基线是下边距
边缘。

It does look like overflow:hidden is buggy in that it removes the baseline from an inline-block element. Fortunately, there's a seemingly redundant Mozilla CSS extension value for overflow that prevents overflow but doesn't exhibit this buggy behaviour.

Try this:

.block {
    width: 10em; padding: .3em 0 .1em 0;
    overflow: hidden;
    overflow: -moz-hidden-unscrollable;
    white-space: nowrap;
    display: inline-block;
    border: 1px solid #666; background-color: #eee;    
}

It looks like it corrects the problem in Firefox and doesn't mess with Safari.

Update:

It looks like Firefox and Opera are rendering overflow:hidden inline blocks correctly and Webkit browsers are not.

According to the W3C CSS2 Working Draft's Visual Formatting Model Details,

The baseline of an 'inline-block' is
the baseline of its last line box in
the normal flow, unless it has either
no in-flow line boxes or if its
'overflow' property has a computed
value other than 'visible', in which
case the baseline is the bottom margin
edge.

败给现实 2024-10-11 16:38:41

尝试将 vertical-align: text-bottom; 添加到 .block

您也可以尝试为 .label设置相等的行高>.block

try adding vertical-align: text-bottom; to .block

you can also try to set equal line-heights for .label and .block

音栖息无 2024-10-11 16:38:41

尝试

.label {
        float: left;
        line-height: 30px;
        margin-right: 5px;
        }

这将在 FF 和 Chrome/Safari 中获得所需的结果。不过没有在IE中测试。

try

.label {
        float: left;
        line-height: 30px;
        margin-right: 5px;
        }

this will get the desired result in both FF and Chrome/Safari. Did not test in IE however.

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