Firefox 和 Chrome 中的 CSS 行高不同
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31386307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CSS line-height not the same in Firefox and Chrome
提问by basZero
Goal: Just show the first four lines in a text box.
目标:只需在文本框中显示前四行。
I tested JSFiddle Demowith Chrome 43.0.2357.132 (64-bit) and Firefox 39 and in Chrome the text box shows perfectly the first 4 lines (remaining lines are hidden) whereas in Firefox the line-height
appears larger, therefore the fourth line got cut.
我用 Chrome 43.0.2357.132(64 位)和 Firefox 39测试了JSFiddle Demo,在 Chrome 中,文本框完美地显示了前 4 行(剩余的行被隐藏),而在 Firefox 中line-height
看起来更大,因此第四行被剪掉了。
How can I solve this with CSS?
我怎样才能用 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>
采纳答案by Alexander O'Mara
You could explicitly set the line-height
.
您可以显式设置line-height
.
line-height: 1.2;
Working Example (JSFiddle):
工作示例(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>
It appears Firefox has a default line-height
of 1.1
, but Chrome has a default line-height
of 1.2
.
似乎 Firefox 的默认line-height
值为1.1
,但 Chrome 的默认line-height
值为1.2
。
回答by Stickers
In general the default line-height
value is normal
, on MDNit says:
通常默认line-height
值是normal
,在MDN 上它说:
normal
- Depends on the user agent. Desktop browsers (including Firefox) use a default value of roughly1.2, depending on the element'sfont-family
.
normal
- 取决于用户代理。桌面浏览器(包括 Firefox)使用大约1.2的默认值,具体取决于元素的font-family
.
To fix the inconsistency results from different browsers, you could try apply a number
or length
or percentage
value for it, also specify a web-safe font for font-family
.
要修复来自不同浏览器的不一致结果,您可以尝试为其应用 a number
or length
orpercentage
值,同时为font-family
.
Also see this related post - jQuery/CSS: line-height of “normal” == ?px
另请参阅此相关帖子 - jQuery/CSS: line-height of “normal” == ?px
.txt {
width:300px;
height:47px;
overflow:hidden;
border-bottom:1px solid #aaa;
margin-bottom:2em;
font-size:0.8em;
font-family:arial; /*new*/
line-height:1.2; /*new*/
}
<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>
回答by IMI
Line-height is only part of the solution.
行高只是解决方案的一部分。
As the other answers state, it varies by browser and you will want to set the value for greater consistency. My suggestion is to use em
values.
正如其他答案所述,它因浏览器而异,您需要设置该值以获得更大的一致性。我的建议是使用em
值。
Secondly, you will want the height of your container to be a multiple of the line-height. So, if you want a container that is 4 lines high and your line-height is 1.2em, then you will want a container height of 4.8em (1.2em x 4 lines).
其次,您希望容器的高度是行高的倍数。因此,如果您想要一个高度为 4 行且行高为 1.2em 的容器,那么您需要一个高度为 4.8em(1.2em x 4 行)的容器。
.txt {
width:300px;
height:4.8em; /*4x the line-height to show 4 lines that are not cropped */
overflow:hidden;
border-bottom-style:solid;
border-bottom-width:1px;
border-bottom-color:#aaaaaa;
margin-bottom:2em;
font-size:0.8em;
line-height:1.2em; /* set a relative line height */
}
<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>
回答by Mouhamad Kawas
Each browser has a different default font-family, so you should specify the font-family.
每个浏览器都有不同的默认字体系列,因此您应该指定字体系列。
.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;
font-family: tahoma;
}
回答by Akshay Gurav
You can add line height code for mozilla using:
您可以使用以下方法为 mozilla 添加行高代码:
@-moz-document url-prefix() {
*,body{
line-height: as per your requirement;
}
}