容器中文本的垂直对齐:WebKit 与 Firefox
问题在于,当包含在具有均匀高度/行高且字体大小不均匀的元素中时,基于 Firefox 和 WebKit 的浏览器似乎会以不同的方式垂直对齐文本(反之亦然)。我看过一些类似的帖子,但我还没有真正看到对我的问题有任何很好的解释。
考虑以下示例:
.box {
font-size: 15px;
font-family: Helvetica, Arial;
background-color: Blue;
height: 20px;
width: 60px;
color: White;
line-height: 20px;
}
<div class="box">
A text.
</div>
有什么方法可以解决这个问题吗?是否有任何“文本对齐”属性或我错过的东西?
The problem is that Firefox and WebKit based browsers seem to align text vertically in different ways when contained in an element that has an even height/line-height and the font-size is uneven (or vice versa). I have looked at some similar threads, but I haven't really seen any great explanations for my question.
Consider the following example:
.box {
font-size: 15px;
font-family: Helvetica, Arial;
background-color: Blue;
height: 20px;
width: 60px;
color: White;
line-height: 20px;
}
<div class="box">
A text.
</div>
Is there any way to fix this? Is there any "text-align" property or something that I missed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是由于浏览器处理子像素文本定位的方式存在差异。如果行高为 20 像素,但字体大小为 15 像素,则文本需要放置在距行框顶部 2.5 像素的位置。 Gecko 实际上就是这样做的(然后根据绘画需要进行抗锯齿或捕捉到像素网格)。 WebKit 只是在布局期间将位置舍入为整数像素。在某些情况下,这两种方法给出的答案相差一个像素。除非你将它们并排比较,否则你怎么能看出它们之间存在差异呢?
无论如何,如果您确实需要的话,请确保您的半行距是整数(即行高减去字体大小是偶数)将使渲染更加一致。
This is due to differences in how browsers handle subpixel text positioning. If your line-height is 20 pixels but font-size is 15 pixels, then the text needs to be positioned 2.5 pixels from the top of the line box. Gecko actually does that (and then antialiases or snaps to the pixel grid as needed on painting). WebKit just rounds positions to integer pixels during layout. In some cases, the two approaches give answers that differ by a pixel. Unless you're comparing them side-by-side, how can you even tell there's a difference?
In any case, making sure that your half-leading is an integer (i.e. that line-height minus font-size is even) will make the rendering more consistent if you really need that.
这是浏览器渲染问题。使用比给定高度小 1px 的 line-height,例如:
This is browser rendering issue. Use line-height 1px less than the given height, for example:
如果您正在寻找一种精确垂直对齐的方法,请查看 Stack Overflow 问题垂直对齐问题:中间; - 我在那里描述了一个解决方案。
如果你想知道为什么 Firebug 和 Chrome 的显示方式不同,这会有点复杂。行高对齐基于字体行渲染,并且可以在浏览器中以非常不同的方式处理字体。例如,字体平滑和字体粗细确实会扰乱您的页面。
另外,您是否为此页面使用 CSS 重置?它还包含与字体相关的调整,并且可以帮助您克服跨浏览器问题。请参阅CSS 工具:重置 CSS。
If you are looking for a way to do an exact vertical align, check out Stack Overflow question Problem with vertical-align: middle; - I described a solution there.
If you want an answer why Firebug and Chrome display this differently, this will be a bit more complicated. Line-height alignment is based on font-line rendering and fonts can be handled in a very different way across the browsers. For example, font-smoothing and font-weight can really mess with your page.
Also, are you using CSS reset for this page? It contains font related adjustments as well, and it may help you to overcome cross-browser issues. Refer to CSS Tools: Reset CSS.
呃,可怕但真实!我刚刚遇到了这个问题,试图在图标上创建微小的计数气泡 - 太小了,我必须紧挨着文本,这样每个像素都会被计数。使行高比文本大小小 1 倍可以平衡 FF 和 Chrome 之间的显示字段。
Ugh, terrible but true! I just ran into this trying to create tiny count bubbles on an icon - so small that I had to get right next to text so every pixel counted. Making the line-height 1x less than text-size leveled the display field between FF and Chrome.