【问题标题】:Different font height in Firefox and ChromeFirefox 和 Chrome 中不同的字体高度
【发布时间】:2016-07-18 06:32:34
【问题描述】:

我想使用 jQuery 测量 html 元素的字体高度。

Here is a fiddle

//HTML
<h1 id="fonty">Size of font in pixels?</h1>

//CSS
#fonty {
    font-size: 8px;
    font-family: Arial;
}

//Javascript
var height = $("#fonty").height();
console.log(height);

该方法工作正常,但我在 Chrome 和 Firefox 中得到不同的结果。有没有办法在所有浏览器中获得正确/相同的字体大小。

【问题讨论】:

标签: javascript jquery html google-chrome firefox


【解决方案1】:

字体的高度不仅通过 font-size 来衡量,还通过 font-size 和 line-height 以及 padding 值来衡量:

因此,不同的浏览器具有不同的默认值,例如 line-height 1px different in firefox and chrome,因此它们可能会有所不同。

您可以通过显式定义它们来计算它们。

【讨论】:

  • 正确。 line-height: 1 将“修复”这个问题。
  • 另外,这也是您想要使用通用 CSS 重置集的原因之一。
  • 但是现在 height() 方法只返回字体大小。我需要字母的像素高度。
  • 是的。您需要添加它。
猜你喜欢
  • 1970-01-01
  • 2016-08-15
  • 1970-01-01
  • 2018-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-29
  • 1970-01-01
相关资源
最近更新 更多