Firefox 和 Chrome 中的 CSS 行高不同

标签 css google-chrome firefox cross-browser

目标:仅在文本框中显示前四行。

我测试了JSFiddle Demo使用 Chrome 43.0.2357.132(64 位)和 Firefox 39,在 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;
}
<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>

最佳答案

您可以显式设置 line-height

line-height: 1.2;

工作示例(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;
}
<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>

似乎 Firefox 的默认 line-height1.1,但 Chrome 的默认 line-height1.2.

关于Firefox 和 Chrome 中的 CSS 行高不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31386307/

相关文章:

javascript - 需要使用eclipse为Chrome制作一个打包的应用程序

firefox - 从 .bat 文件/命令刷新 Firefox

html - HTML/CSS 中的列 + 滚动

html - 如何检测我的浏览器是否为 Firefox?

javascript - CSS 缩放变换使用 CPU 进行重绘

javascript - 在 Chrome 中一次打开多个链接作为新标签

android - 相同的 CSS 规则,不同的结果

css - 动画 translateY 在 Firefox 中的剪辑路径上

javascript - 使用 batch/cmd 打开网页并执行 javascript

html - 在没有 JavaScript 的情况下选中复选框时隐藏 div