【发布时间】:2021-07-05 08:45:16
【问题描述】:
我有一个困扰多年的问题。
带有line-height: 1 的文本会很紧凑,并且会到达周围元素的顶部和底部。
具有其他行高的文本将更具可读性,但同时,它将不再到达周围元素的顶部和底部。
这是问题的代码示例和 hackish 解决方案。更改行高或字体大小会破坏它。
这是一个可靠的解决方案吗?
.wrap {
display: flex;
}
.first {
background: red;
width: 1rem;
}
.second,
.second2{
line-height: 2;
}
.second2 {
margin-top: -9px;
margin-bottom: -7px;
}
<strong>Problem:</strong> The letter "A" does not align with the red top.<br><br>
<div class="wrap">
<div class="first"></div>
<div class="second">A text that<br>spans on multiple rows</div>
</div>
<br><br>
<strong>Hackish fix:</strong> The letter "A" does align with the red top.<br><br>
<div class="wrap">
<div class="first"></div>
<div class="second2">A text that<br>spans on multiple rows</div>
</div>
把line-height取到了极致,就是为了展示效果。
【问题讨论】:
-
我怀疑是否有可靠的修复(相关:stackoverflow.com/a/62303653/8620333)。作为旁注,这是设计使然,那么为什么您要修复本应以这种方式工作的东西呢?
-
@TemaniAfif 如果您仍然需要行高,您还能如何让文本与框的顶部对齐?
-
我在上面链接的解决方案。我没有看到更强大的。在所有情况下,您都需要有一个额外的属性来纠正这个差距。您可以通过引入 calc() 来优化它,使其更通用。
-
我认为
text-edge: text有朝一日会支持更好的解决方案,但目前只能使用 hacky 解决方案。 -
@Alohci 好消息!这意味着我毕竟没有疯!
标签: css line-height