Firefox和Chrome中的CSS行高不一样

bas*_*ero 11 css firefox google-chrome cross-browser

目标:只需在文本框中显示前四行.

我用Chrome 43.0.2357.132(64位)和Firefox 39 测试了JSFiddle Demo,在Chrome中,文本框显示前4行(其余行被隐藏),而在Firefox中则line-height显得更大,因此第四行被删除.

如何用CSS解决这个问题?

.txt {
    width:300px;
    height:48px;
    overflow:hidden;
    border-bottom-style:solid;
    border-bottom-width:1px;
    border-bottom-color:#aaaaaa;
    margin-bottom:2em;
    font-size:0.8em;
}
Run Code Online (Sandbox Code Playgroud)
<div class="txt">
    This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text. 
</div>
Run Code Online (Sandbox Code Playgroud)

Ale*_*ara 6

你可以明确地设置line-height.

line-height: 1.2;
Run Code Online (Sandbox Code Playgroud)

工作示例(JSFiddle):

.txt {
    width:300px;
    height:48px;
    overflow:hidden;
    border-bottom-style:solid;
    border-bottom-width:1px;
    border-bottom-color:#aaaaaa;
    margin-bottom:2em;
    font-size:0.8em;
    line-height: 1.2;
}
Run Code Online (Sandbox Code Playgroud)
<div class="txt">
    This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text. 
</div>
Run Code Online (Sandbox Code Playgroud)

看来Firefox有一个默认line-height1.1,而Chrome有一个默认line-height1.2.

  • 在撰写本文时(2020 年),即使您明确将 font-size(以 px 为单位)和 line-height 设置为乘数,您也可能会在 Firefox 和 Chrome 上得到不同的结果。这是 Chrome 中的一个已知错误 - https://bugs.chromium.org/p/chromium/issues/detail?id=1023654。Firefox 会将两个值相乘并保留任何小数像素,而 Chrome 会向下舍入到最接近的整数像素。例如,如果渲染 `&lt;p style='font-size: 14px; line-height: 1.75;'&gt;Text&lt;/p&gt;` 在两种浏览器上,您将在 Firefox 上获得 24.5px,在 Chrome 上获得 24px。 (4认同)
  • 明确设置行高(无论如何我都这样做)对我没有帮助。Firefox 和 Safari 中的结果肯定存在差异。ig行高:1.5em;在两种浏览器上不会以相同的像素现实结束。 (3认同)

Sti*_*ers 6

一般来说,默认line-height值是normal,在MDN上它说:

\n\n
\n

normal- 取决于用户代理。桌面浏览器(包括 Firefox)使用大约1.2的默认值,具体取决于元素的font-family.

\n
\n\n

要修复不同浏览器结果不一致的问题,您可以尝试为其应用numberlengthpercentage值,并为font-family.

\n\n

另请参阅此相关帖子 - jQuery/CSS: line-height of \xe2\x80\x9cnormal\xe2\x80\x9d == ?px

\n\n

\r\n
\r\n
.txt {\r\n    width:300px;\r\n    height:47px;\r\n    overflow:hidden;\r\n    border-bottom:1px solid #aaa;\r\n    margin-bottom:2em;\r\n    font-size:0.8em;\r\n    font-family:arial; /*new*/\r\n    line-height:1.2; /*new*/\r\n}
Run Code Online (Sandbox Code Playgroud)\r\n
<div class="txt">\r\n    This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text. This is just a temporary text. \r\n</div>
Run Code Online (Sandbox Code Playgroud)\r\n
\r\n
\r\n

\n