比较 Chrome 和 Firefox 的字体大小问题

发布于 2024-10-16 10:14:20 字数 549 浏览 7 评论 0原文

我建立了一个网站,问题是,chrome 显示的 font-size 1px 比 Firefox 大。我尝试了几种方法来匹配字体大小,以 px 为单位指定,以 % 为单位将正文设置为 100%,然后将元素设置为 0.875em。这些都不起作用。它仍然在 Chrome 中显示多 1 像素。

这是我用于字体大小的代码:

body {
  font-size: 100%;
}
* {
  margin:0;
  padding:0; 
  text-decoration: none; 
  font-family:helvetica, arial, sans-serif;
}
#geral {
  width:1000px; 
  margin:0 auto; 
  position:relative; 
  font-size:0.875em;
}

#geral 包裹整个网站并且 CSS 上没有其他字体大小声明,可以在我发布的链接中查看源代码。

我想知道是否有办法解决这个问题,或者我是否必须为每个浏览器指定不同的字体大小?

I built a site and the problem is, chrome display font-size 1px bigger than Firefox. I tried several ways to match the font-size, specified it in px, in % set the body to 100% and then the elements to 0.875em. None of those work. It stills display 1 pixel greater in chrome.

This is the code I'm using for font-sizes:

body {
  font-size: 100%;
}
* {
  margin:0;
  padding:0; 
  text-decoration: none; 
  font-family:helvetica, arial, sans-serif;
}
#geral {
  width:1000px; 
  margin:0 auto; 
  position:relative; 
  font-size:0.875em;
}

Where the #geral wraps the entire site and there is no other font-size statement on the CSS, the source can be viewed in the link I posted.

I wonder if there is a way to fix that or if I'll have to specify different font-sizes for each browser?

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

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

发布评论

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

评论(8

那片花海 2024-10-23 10:14:20

我建议您使用 CSS 重置,例如 YUI 中的重置。它将使您的页面在所有浏览器中更加一致,包括字体渲染。它与 IE 和其他浏览器有最大的区别,但它消除了各种不一致的情况。

I suggest you use a CSS reset like the one from YUI. It will make your pages much more consistent across all browsers, including font rendering. It makes the biggest difference with IE and the other browsers, but it gets rid of all manner of inconsistencies.

尬尬 2024-10-23 10:14:20

Fwiw 在这个日期,我自己最近刚刚了解到,良好的 CSS 编码实践是仅为 HTML 或 BODY 元素定义绝对字体大小,并相对地定义所有其他字体大小,即根据此大小(即使用 em%)。

如果您这样做,您只需从其他浏览器(Gecko、IE 等)中挑选出 webkit 浏览器(Chrome、Safari)即可。因此,例如,您可能已在样式表中定义,

body {
  font-size: 16px;
}

然后在样式表的底部,您可以包含此内容

@media screen and (-webkit-min-device-pixel-ratio:0) { 
  Body {
    font-size: 20px;      
    }
}

(另请参阅Chrome 条件注释

这对我有用。但一个副作用是也会重新调整相对大小的任何非文本元素,这可能是理想的,也可能不是。

Fwiw at this date, I myself have just recently learned that good CSS-coding practice is to define absolute font-size only for the HTML or BODY element, and to define all other font-sizes relatively, that is, in terms of this size (i.e., using em or %).

If you do that, you only need single out webkit browsers (Chrome, Safari) from the others (Gecko, IE, etc.). So, for example, you might have defined in your stylesheet,

body {
  font-size: 16px;
}

Then at the bottom of the stylesheet, you can include this

@media screen and (-webkit-min-device-pixel-ratio:0) { 
  Body {
    font-size: 20px;      
    }
}

(See also Chrome conditional comments)

This works for me. But one side-effect is to also rescale any non-text elements that are sized relatively, and this may or may not be desirable.

2024-10-23 10:14:20
<script>

     if(navigator.userAgent.indexOf("Chrome") != -1 ) 
    {
         var fontsize = "<style>body{font-size: 125%;}</style>";
    }
    else if(navigator.userAgent.indexOf("Opera") != -1 )
    {
         var fontsize = "<style>body{font-size: 100%;}</style>";
    }
    else if(navigator.userAgent.indexOf("Firefox") != -1 ) 
    {
         var fontsize = "<style>body{font-size: 100%;}</style>";
    }
    else if((navigator.userAgent.indexOf("MSIE") != -1 ) || (!!document.documentMode == true )) //IF IE > 10
    {
         var fontsize = "<style>body {font-size: 100%;}</style>";
    }  
    else 
    {
         var fontsize = "<style>body {font-size: 100%;}</style>";
    }
    </script>

<script>document.writeln(fontsize);</script>
<script>

     if(navigator.userAgent.indexOf("Chrome") != -1 ) 
    {
         var fontsize = "<style>body{font-size: 125%;}</style>";
    }
    else if(navigator.userAgent.indexOf("Opera") != -1 )
    {
         var fontsize = "<style>body{font-size: 100%;}</style>";
    }
    else if(navigator.userAgent.indexOf("Firefox") != -1 ) 
    {
         var fontsize = "<style>body{font-size: 100%;}</style>";
    }
    else if((navigator.userAgent.indexOf("MSIE") != -1 ) || (!!document.documentMode == true )) //IF IE > 10
    {
         var fontsize = "<style>body {font-size: 100%;}</style>";
    }  
    else 
    {
         var fontsize = "<style>body {font-size: 100%;}</style>";
    }
    </script>

<script>document.writeln(fontsize);</script>
以可爱出名 2024-10-23 10:14:20

在这里工作正常:

Chrome 9.0:
在此处输入图像描述

Firefox 4.0 beta 10:
在此处输入图像描述

Works fine here:

Chrome 9.0:
enter image description here

Firefox 4.0 beta 10:
enter image description here

带刺的爱情 2024-10-23 10:14:20

em 是可缩放的,而 px 则不可缩放。将字体设置为定义的 px 大小,应该没问题。 em 在某些情况下可能是理想的,但如果您担心 1px,那么您应该设置严格的像素大小。

编辑:只需重读,我发现您已经尝试将高度设置为像素。没有任何线索,因为我没有在这里安装 Chrome 来测试。 :(

em is scalable and px is not. Set the font to a defined px size and you should be ok. em can be desirable in certain circumstances, but if you are worried about 1px then you should set strict pixel sizes.

EDIT: Just reread and I see you have tried setting the height as pixels already. Don't have a clue then as I don't have Chrome installed here to test. :(

审判长 2024-10-23 10:14:20

如果您有要打印的网页,

添加 css

<link rel="stylesheet" type="text/css" href="report.css" media="print" />

请在 css 文件中

body {
    padding: 3px;
    margin: 0.5px;
    background-position: center;
    color: #000000;
    background: #ffffff;
    font-family: Arial;
    font-size: 13pt;
}

,这对我有用

if you have web page to print then

add css

<link rel="stylesheet" type="text/css" href="report.css" media="print" />

in css file

body {
    padding: 3px;
    margin: 0.5px;
    background-position: center;
    color: #000000;
    background: #ffffff;
    font-family: Arial;
    font-size: 13pt;
}

this works for me

有深☉意 2024-10-23 10:14:20

我也遇到过这个问题,我决定尽可能使用 font-size:small (或 x-small 等)。这为您提供了基本的可缩放字体大小范围,而无需寻找繁琐的 css 或搞乱 JS。它适用于 IE、FF 和 Chrome。

I too have had this problem and I've decided, where possible to go with font-size: small (or x-small etc). This gives you a basic range of scalable font sizes without having to look for fiddily css or messing around with JS. It works with IE, FF and Chrome.

明明#如月 2024-10-23 10:14:20

当我使用这个 css 时,我在 Firefox 和 Chrome 之间的渲染方面获得了足够好的相似性(它可以工作,因为 Firefox 尚未实现“缩放”):

body {zoom: .7}

但它仍然不是像素完美的。

请注意,我在任何地方都使用“rem”单位而不是“px”单位,因为当我在页面上使用多种字体时,效果更好。

请注意,我已将 Windows 显示分辨率设置为 150%,以弥补我视力不佳的问题。当我将其设置回100%时,尺寸不同的问题仍然存在。所以这不是问题。

我确信没有标准规定页面在不同浏览器上必须看起来相同,但使用“缩放”似乎在这方面有所帮助。

除非这个比例因子问题是由于我的计算机配置错误造成的。

I'm getting a good enough similarity in rendering between Firefox and Chrome when I use this css (it works because Firefox does not yet implement 'zoom'):

body {zoom: .7}

But it still isn't pixel perfect.

Note that I use 'rem' units instead of 'px' units everywhere, as this works better when I use more than one font on a page.

Note that I've set Windows display resolution to 150% to compensate for my poor vision. When I set it back to 100%, the problem of different sizes remains. So this isn't the issue.

I'm sure there is no standard that says that pages must look the same on different browsers, but using 'zoom' seems to help in this regard.

Unless this scale factor problem is due to a misconfiguration on my computer.

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