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

直接上一段实例代码,注释部分有相应代码的讲解。

<!DOCTYPE html>
<html>
<head lang="en">
  <meta charset="UTF-8">
  <title>CSS:line-height:150%与line-height:1.5的区别是什么?</title>
</head>
<body>
<!--150%是根据父元素的字体大小计算出行高,并且子元素依然沿用这个计算后的行高。而1.5则是根据子元素自己字体的大小去乘以1.5来计算行高。另,1.5em等也是按照150%的情况来算的。-->

<!--line-height:150%的效果,父元素的行高为150%时,会根据父元素的字体大小先计算出行高值然后再让子元素继承。所以当line-height:150%时,字元素的行高等于16px * 150% = 24px-->
<div style="line-height:150%;font-size:16px;">
  父元素内容
  <div style="font-size:30px;">
    Web前端开发<br/>
    line-height行高问题
  </div>
</div>
<hr>
<!--line-height:1.5em的效果,父元素的行高为150%时,会根据父元素的字体大小先计算出行高值然后再让子元素继承。所以当line-height:1.5em时,子元素的行高等于16px * 1.5em = 24px-->
<div style="line-height: 1.5em;;font-size:16px;">
  父元素内容
  <div style="font-size:30px;">
    Web前端开发<br/>
    line-height行高问题
  </div>
</div>
<hr>
<!--line-height:1.5的效果,父元素行高为1.5时,会根据子元素的字体大小动态计算出行高值让子元素继承。所以,当line-height:1.5时,子元素行高等于30px * 1.5 = 45px-->
<div style="line-height: 1.5;;font-size:16px;">
  父元素内容
  <div style="font-size:30px;">
    Web前端开发<br/>
    line-height行高问题
  </div>
</div>
</body>
</html>
下图是当line-height:150%的效果,父元素的行高为150%时,会根据父元素的字体大小先计算出行高值然后再让子元素继承。所以当line-height:150%时,字元素的行高等于16px * 150% = 24px:

<img data-rawheight="75" data-rawwidth="277" src="https://pic2.zhimg.com/cd8d76c78e80e3183a1c241dfb39f2c5_b.jpg" class="content_image" width="277">


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

<img data-rawheight="75" data-rawwidth="277" src="https://pic2.zhimg.com/cd8d76c78e80e3183a1c241dfb39f2c5_b.jpg" class="content_image" width="277">


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

<img data-rawheight="106" data-rawwidth="277" src="https://pic1.zhimg.com/1a56e5fabcf173ae074e0f4ed9e61e3c_b.jpg" class="content_image" width="277">


  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
优化下面代码.bg { width: 100%; height: 100vh; background-image: url('../../assets/img/info-bg.png'); background-size: 100% 100%; background-repeat: no-repeat; position: relative; font-family: AlibabaPuHuiTiR; .goBack { position: absolute; top: 34px; right: 65px; cursor: pointer; color: #ffffff; width: 181px; padding: 15px 10px; background: rgba(24, 31, 30, 0.52); border: 1px solid #4a524e; border-radius: 5px; font-size: 18px; font-family: AlibabaPuHuiTiR; z-index: 111; display: flex; flex-direction: row; justify-content: space-between; align-items: center; } .home-left { position: absolute; top: 18%; left: 40px; width: 41%; height: 76%; font-size: 24px; color: #ffffff; } .unit { font-size: 24px; color: #636363; } .home-left-title { font-size: 24px; color: #ffffff; line-height: 36px; } .home-right { position: absolute; top: 18%; right: 88px; width: 46%; height: 78%; } .model { display: flex; justify-content: center; align-items: center; height: 90%; } #threeContained { width: 100%; height: 100%; } .model-qk-img { width: 82%; height: 90%; background-image: url('../../assets/img/howo.png'); background-size: 100% 100%; background-repeat: no-repeat; } .model-zk-img { width: 56%; height: 90%; background-image: url('../../assets/img/heavyT.png'); background-size: 100% 100%; background-repeat: no-repeat; } .model-gj-img { width: 82%; height: 90%; background-image: url('../../assets/img/transit.png'); background-size: 100% 100%; background-repeat: no-repeat; } .car-online { margin-bottom: 50px; } } .day-data { display: flex; flex-direction: row; justify-content: space-between; align-items: center; height: 29%; margin-left: 30px; } .day-val { width: 40%; } .prefix { display: inline-block; width: 6px; height: 14px; background: #ffffff; margin-right: 20px; } .zh-title { margin-left: 30px; padding-top: 30px; font-size: 30px; font-weight: 700; text-align: left; color: #ffffff; line-height: 32px; letter-spacing: 0.3px; font-family: AlibabaPuHuiTiB; } .en-title { margin-left: 30px; font-size: 14px; font-weight: 400; text-align: left; color: #ffffff; line-height: 32px; letter-spacing: -0.91px; font-family: AlibabaPuHuiTiR; }
05-31
Here are some suggestions to optimize the code: 1. Use shorthand properties whenever possible. For example, instead of writing `background-size: 100% 100%;`, you can write `background-size: cover;`. 2. Consolidate similar styles into classes, instead of repeating them for every element. For example, you can create a class for the font family and apply it to all elements that use that font. 3. Remove unnecessary styles that are not being used or overwritten by other styles. 4. Use more specific selectors to target elements, instead of relying on the order of elements in the HTML. This will make the code more robust and easier to maintain. 5. Consider using a CSS preprocessor like Sass or Less, which can help you write cleaner and more organized code. Here's an example of how the code could be optimized: ``` .bg { width: 100%; height: 100vh; background: url('../../assets/img/info-bg.png') no-repeat center center / cover; position: relative; font-family: AlibabaPuHuiTiR; } .goBack { position: absolute; top: 34px; right: 65px; cursor: pointer; color: #fff; width: 181px; padding: 15px 10px; background: rgba(24, 31, 30, 0.52); border: 1px solid #4a524e; border-radius: 5px; font-size: 18px; font-family: AlibabaPuHuiTiR; z-index: 111; display: flex; flex-direction: row; justify-content: space-between; align-items: center; } .home-left { position: absolute; top: 18%; left: 40px; width: 41%; height: 76%; font-size: 24px; color: #fff; } .unit { font-size: 24px; color: #636363; } .home-left-title { font-size: 24px; color: #fff; line-height: 36px; } .home-right { position: absolute; top: 18%; right: 88px; width: 46%; height: 78%; } .model { display: flex; justify-content: center; align-items: center; height: 90%; } #threeContained { width: 100%; height: 100%; } .model-qk-img { width: 82%; height: 90%; background: url('../../assets/img/howo.png') no-repeat center center / cover; } .model-zk-img { width: 56%; height: 90%; background: url('../../assets/img/heavyT.png') no-repeat center center / cover; } .model-gj-img { width: 82%; height: 90%; background: url('../../assets/img/transit.png') no-repeat center center / cover; } .car-online { margin-bottom: 50px; } .day-data { display: flex; justify-content: space-between; align-items: center; height: 29%; margin-left: 30px; } .day-val { width: 40%; } .prefix { display: inline-block; width: 6px; height: 14px; background: #fff; margin-right: 20px; } .zh-title { margin-left: 30px; padding-top: 30px; font-size: 30px; font-weight: 700; text-align: left; color: #fff; line-height: 32px; letter-spacing: 0.3px; font-family: AlibabaPuHuiTiB; } .en-title { margin-left: 30px; font-size: 14px; font-weight: 400; text-align: left; color: #fff; line-height: 32px; letter-spacing: -0.91px; font-family: AlibabaPuHuiTiR; } .font-alibaba { font-family: AlibabaPuHuiTiR; } .font-alibaba-bold { font-family: AlibabaPuHuiTiB; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值