极客时间返利平台,你可以在上边通过山月的链接购买课程,并添加我的微信 (shanyue94) 领取返现。
山月训练营之面试直通车 服务上线了,从准备简历、八股文准备、项目经历准备、面试、面经、面经解答、主观问题答复、谈薪再到入职的一条龙服务。

# line-height 的值分别取 [2, 2em, 200%] 有什么区别?

Issue

欢迎在 Gtihub Issue 中回答此问题: Issue 550 (opens new window)

代码见: lineHeight - codepen (opens new window)

line-height 是相对于元素自身的字体大小来取值,但同时会被继承。在实际工作中,取值 2em 或者 200% 有可能遇到未预测的内容。

比如:

  • 父元素: fontSize: 18px; lineHeight: 1.5em(27px,150% 同理); ,它的 lineHeight 计算下来为 27px,会被子元素继承
  • 子元素: fontSize: 30px,子元素的 lineHeight 被继承为 27px,出现问题

以下为演示代码,见: lineHeight - codepen (opens new window)

<div class="box green">
  <h1>lineHeight: 1.5; 这是没有问题的框框</h1>
  lineHeight: 1.5; 这是没有问题的框框 lineHeight: 1.5; 这是没有问题的框框
</div>

<div class="box red">
  <h1>lineHeight: 1.5em; 这是有问题的框框</h1>
  lineHeight: 1.5em; 这是有问题的框框 lineHeight: 1.5em; 这是有问题的框框
</div>

<div class="box orange">
  <h1>lineHeight: 150%; 这是有问题的框框</h1>
  lineHeight: 150%; 这是有问题的框框 lineHeight: 150%; 这是有问题的框框
</div>
.green {
  line-height: 1.5;
  border: solid limegreen;
}

.red {
  line-height: 1.5em;
  border: solid red;
}

.orange {
  line-height: 150%;
  border: solid orange;
}

h1 {
  font-size: 30px;
}

.box {
  width: 18em;
  display: inline-block;
  vertical-align: top;
  font-size: 16px;
}
Last Updated: 11/27/2021, 6:11:48 PM