CSS:为什么当使用溢出:隐藏时,vertical-align:baseline在Firefox上停止工作?
您可以通过运行此 测试来重现此问题案例。结果如下面的屏幕截图所示。问题是,在 Firefox 上,当在“块”(屏幕截图中带有灰色背景)上添加 overflow:hidden
时,该块停止按照我希望的方式对齐:而不是块中文本的基线与父框的基线对齐,就好像块的底部与父框的基线对齐一样。正如您在屏幕截图中看到的,Chrome 不会发生这种情况。
- 这是 Firefox 的错误吗?
- 如何在 Firefox 上获得预期结果(与
overflow:hidden
进行基线对齐)?
注意:使用 vertical-align :“块”上的 middle
并不能消除它,因为我真正想要的是基线对齐。通过设置 padding: 1em 0 .1em 0
(在框的顶部有更多的内边距)可以更清楚地看到 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.
- Is this a Firefox bug?
- How to get the expected result on Firefox (baseline alignment with
overflow: hidden
)?
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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它看起来确实像溢出:隐藏是有问题的,因为它从内联块元素中删除了基线。幸运的是,有一个看似多余的 Mozilla CSS 溢出扩展值可以防止溢出,但不会表现出这种错误行为。
试试这个:
看起来它解决了 Firefox 中的问题,并且不会干扰 Safari。
更新:
看起来 Firefox 和 Opera 正在正确渲染溢出:隐藏的内联块,而 Webkit 浏览器则不然。
根据W3C CSS2 工作草案的视觉格式化模型详细信息 ,
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:
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,
尝试将
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
尝试
这将在 FF 和 Chrome/Safari 中获得所需的结果。不过没有在IE中测试。
try
this will get the desired result in both FF and Chrome/Safari. Did not test in IE however.