为什么粗体单色在 Windows 上垂直移动?
在 Windows 上的 Firefox 3.6、IE7 和 Opera 10 中,此 HTML 有一个奇怪的行为:
<html><head></head>
<style>
span {
font-family: monospace; background-color: green;
}
span.b {
font-weight: bold;
}
</style>
<body>
<span>Text</span><span class="b">Text</span><span>Text</span>
</body>
</html>
中间的粗体跨度向下移动了一个像素。其他字体不会出现这种情况。
这是为什么?我该如何修复它?
In Firefox 3.6, IE7 and Opera 10 on Windows, this HTML has an odd behavior:
<html><head></head>
<style>
span {
font-family: monospace; background-color: green;
}
span.b {
font-weight: bold;
}
</style>
<body>
<span>Text</span><span class="b">Text</span><span>Text</span>
</body>
</html>
The bold span in the middle is shifted down by one pixel. That doesn't happen for other fonts.
Why is that? How can I fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
很好的问题。花了半个小时试图找出原因,但无济于事。 Marcgg 的解决方案似乎也不起作用,偏移量仍然存在。谷歌没有发现任何结果。这似乎只发生在 Windows 上,不仅在你提到的浏览器中,而且在 Opera 9 和 IE6 中也发生。但在 Firefox 2.0 中则不然。令人费解。
到目前为止,对我来说唯一有效的解决方案是:
即指定 Courier 而不是等宽字体。这在我测试的所有浏览器(Windows 2000 下的 IE6、Opera 9、FF 2.0;Ubuntu 下的 Opera 10、FF 3 和 Konqueror)中呈现一致。至于为什么,我还是不知道。
Excellent question. Just spent half an hour trying to figure out the why, to no avail. Marcgg's solution doesn't seem to work, either, the offset is still there. Google turns up nothing. This only seems to happen on Windows, not just in the browsers you mention, but also in Opera 9 and IE6. Not in Firefox 2.0 though. Puzzling.
The only solution that has worked so far for me is this:
I.e., specifying Courier instead of monospace. This renders consistently in all browsers I tested (IE6, Opera 9, FF 2.0 under Windows 2000; Opera 10, FF 3, and Konqueror under Ubuntu). As to why, I still have no idea.
这只是一种光学效应。抓取屏幕截图并放大:您会看到文本基线根本没有改变。
如果您选择不同的颜色对比度,它应该会自行修复。
更新
替代文本 http://img690.imageshack.us/img690/421/opticaleffect.png
It's just an optical effect. Grab a screenshot and zoom in: you'll see the text baseline does not change at all.
It should fix itself if you choose a different colour contrast.
Update
alt text http://img690.imageshack.us/img690/421/opticaleffect.png
问题是字体“Courier New”,这是在请求“等宽”时使用的大多数 Windows 浏览器的默认字体。对于粗体变体,基线位于不同的偏移处:
使用此代码,您可以看到中间文本向上移动(=不同的基线偏移),但背景颜色现在正确对齐。
解决方案是请求“Courier”作为字体并避免“Courier New”。
The problem is the font "Courier New" which is the default font used my most Windows browsers when "monospace" is requested. For the bold variant, the baseline is at a different offset:
With this code, you can see that the middle text is shifted up (= different baseline offset) but the background color is now aligned properly.
The solution is to request "Courier" as a font and to avoid "Courier New".