WebKit 与 Mozilla 框中字体字形的垂直对齐
此测试图像显示了 Safari 与 Firefox(Safari 5.0.5 和 Firefox 5.0)在框中文本的位置有多么不同.1 适用于 Mac OS X 10.6.7)。请注意,在 Firefox(而不是 Safari)中,sans-serif
的“S”是如何连接到框的顶部的。差异似乎取决于所使用的字体,有些甚至是一致呈现的。
我读过人们说是因为 font-size
和 line-height
之间的舍入问题(并通过设置比大小更小的高度来修复),但我认为我的示例反驳了这一点,其中 Firefox 中的 sans-serif/helvetica 始终与框中的顶部对齐。
在我看来,Safari 比 Firefox 更正确,即文本通常更围绕中线。
有没有什么好的方法可以让它们更加一致?我的目标只是符合标准的浏览器。
- NB1:这与
vertical-align
无关。 - NB2:我过去调查过类似问题,但没有完全令人满意结果。
我的测试代码: http://jsbin.com/omaboc
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<style type="text/css">
body {
font-size: 50px;
line-height: 1em;
}
div {
background: #b5e260;
margin-bottom: 5px;
}
</style>
</head>
<body>
<div style="font-family: sans-serif">Some text @ this box</div>
<div style="font-family: serif">Some text @ this box</div>
<div style="font-family: arial">Some text @ this box</div>
<div style="font-family: helvetica">Some text @ this box</div>
<div style="font-family: courier">Some text @ this box</div>
<div style="font-family: georgia">Some text @ this box</div>
</body>
</html>
This test image shows how wildly different Safari positions text inside a box vs Firefox (Safari 5.0.5 and Firefox 5.0.1 for Mac OS X 10.6.7). Notice how the "S" for sans-serif
is butted up to the top of the box in Firefox and not Safari. The difference seem to vary depending on typeface used, where some are even consistently rendered.
I've read people saying that this is because of rounding issues between font-size
and line-height
(and fixed by setting smaller height than size), but I think that's disproved by my example where sans-serif/helvetica in Firefox always aligns top in the box.
To me it looks like Safari gets it more right than Firefox, i.e. text is generally more around a middle line.
Is there a good way to get them more consistent? My target is only standards-compliant browsers.
- NB1: This has nothing to do with
vertical-align
. - NB2: I investigated a similar problem in the past with no completely satisfactory outcome.
My test code: http://jsbin.com/omaboc
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<style type="text/css">
body {
font-size: 50px;
line-height: 1em;
}
div {
background: #b5e260;
margin-bottom: 5px;
}
</style>
</head>
<body>
<div style="font-family: sans-serif">Some text @ this box</div>
<div style="font-family: serif">Some text @ this box</div>
<div style="font-family: arial">Some text @ this box</div>
<div style="font-family: helvetica">Some text @ this box</div>
<div style="font-family: courier">Some text @ this box</div>
<div style="font-family: georgia">Some text @ this box</div>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不幸的是,这个问题没有解决方案。几乎每个浏览器中的文本呈现方式都不同,甚至在主要浏览器版本之间有时也会发生变化。在某些浏览器中,它是由操作系统的特定字体渲染系统和设置决定的。一种字体渲染类型与另一种字体渲染类型之间一直存在并且永远都会存在权衡,并且每一种字体渲染都会随着不同类型硬件上不同类型的显示而变化。
抱歉,您将不得不忍受浏览器之间的非像素完美字体,直到单一浏览器、单一操作系统(没有可用的显示设置选项)、单一显示器类型和尺寸以及单一显卡完全垄断。换句话说,只要您的网站支持多个设备、浏览器、显示器等,它就永远不会完美地匹配像素
。(感谢您先做了自己的作业和测试。您的问题在提出之前经过了充分的研究和思考。我希望经过所有这些工作后,我们本可以给您一个更好的答案。)
Unfortunetly, there isn't a solution to this problem. Text rendering is done differently in almost every browser, and even between major browser versions it sometimes changes. In some browsers it is determined by the particular font rendering systems and settings from the OS. There have always been and always will be trade offs for one type of font rendering to another, and every one of those will change with different types of displays on different types of hardware.
Sorry, you'll have to live with non-pixel perfect fonts between browsers until there is a complete monopoly by a single browser, a single OS with no available display setting choices, a single monitor type and size, and a single video card. In other words, it's never going to match pixels perfectly as long as your site supports multiple devices, browsers, displays, etc.
(Kudos for doing your own homework and testing first though. Your question was well researched and thought before asking. I wish we could have given you a better answer after all that work.)
您是否尝试过使用样式表文件顶部的重置?
重置样式表的目标是减少浏览器在默认行高、边距和标题字体大小等方面的不一致。
您可以找到解释&代码在这里: http://meyerweb.com/eric/tools/css/reset/
我希望这对您有帮助!
Have you tried using the Reset at the top of your stylesheet file??
The goal of a reset stylesheet is to reduce browser inconsistencies in things like default line heights, margins and font sizes of headings, and so on.
You can find the explanation & the code here: http://meyerweb.com/eric/tools/css/reset/
I hope this would help you!