CSS:line-height:150%与line-height:1.5的真正区别是什么?

作者:知乎用户
链接:https://www.zhihu.com/question/20394889/answer/18436957
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

代码:
<div style="line-height:150%;font-size:16px;">
父元素内容
<div style="font-size:30px;">
Web前端开发<br />
line-height行高问题
</div>
</div>



下图是当line-height:150%的效果,父元素的行高为150%时,会根据父元素的字体大小先计算出行高值然后再让子元素继承。所以当line-height:150%时,字元素的行高等于16px * 150% = 24px:

&amp;amp;lt;img data-rawheight=&quot;75&quot; data-rawwidth=&quot;277&quot; src=&quot;https://i-blog.csdnimg.cn/blog_migrate/379242bcb24b4cf9159094ee151527d3.png&quot; class=&quot;content_image&quot; width=&quot;277&quot;&amp;amp;gt;


下图是当line-height:1.5em的效果,父元素的行高为150%时,会根据父元素的字体大小先计算出行高值然后再让子元素继承。所以当line-height:1.5em时,子元素的行高等于16px * 1.5em = 24px:

&amp;amp;lt;img data-rawheight=&quot;75&quot; data-rawwidth=&quot;277&quot; src=&quot;https://i-blog.csdnimg.cn/blog_migrate/379242bcb24b4cf9159094ee151527d3.png&quot; class=&quot;content_image&quot; width=&quot;277&quot;&amp;amp;gt;


下图是当line-height:1.5的效果,父元素行高为1.5时,会根据子元素的字体大小动态计算出行高值让子元素继承。所以,当line-height:1.5时,子元素行高等于30px * 1.5 = 45px:

&amp;amp;lt;img data-rawheight=&quot;106&quot; data-rawwidth=&quot;277&quot; src=&quot;https://i-blog.csdnimg.cn/blog_migrate/87241bfa4526323f8c802581c41b4a1b.png&quot; class=&quot;content_image&quot; width=&quot;277&quot;&amp;amp;gt;


作者:知乎用户
链接:https://www.zhihu.com/question/20394889/answer/135504395
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

我开始也没搞清楚区别,看票数最高的答案没看懂,自己根据他的条件,按照 line-height 四种不同取值写了例子。

链接:Different value for line-height

截图:

&amp;lt;img src=&quot;https://i-blog.csdnimg.cn/blog_migrate/9fcc3df7c5c9238b6e626ec9db1a008c.jpeg&quot; data-rawwidth=&quot;1204&quot; data-rawheight=&quot;892&quot; class=&quot;origin_image zh-lightbox-thumb&quot; width=&quot;1204&quot; data-original=&quot;https://pic1.zhimg.com/v2-0d71996efa3f7fb28e2a8f1d036715e0_r.jpg&quot;&amp;gt;

总结:line-height 四种不同取值

1. normal,父、子元素的行高由浏览器分别计算得到。

2. number,父、子元素根据各自的 font-size * number 计算得到行高。

3. length(带有 css 单位,如 em,px 等),子元素直接继承父元素的行高。

4. %,子元素直接继承父元素的行高,父元素的行高根据自己的 font-size 计算得到。


  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值