【发布时间】:2025-12-12 20:55:02
【问题描述】:
一个直截了当的 CSS 问题。假设我们有这样的样式:
body {
font-size : 16px;
}
p {
font-size : 0.875em; // relative to parent, 14px
line-height : 1em; //relative to this element, 14px
margin-bottom : 1em; //relative to this element, 14px
}
这意味着<p>s 的 font-size 为 14px,line-height 为 14px,margin-bottom 为 14px。
我想要的是相当于:
body : {
font-size : 16px;
}
p {
font-size : 0.875em; // relative to parent, 14px;
line-height : 1em; // relative to parent line-height, 16px;
margin-bottom : 1em; // relative to parent margin-bottom, 16px;
}
我想要这个的原因是因为我想要一个尊重其基线的响应式字体大小。
【问题讨论】:
-
您想要一个取决于父级的 line-height 的 line-height?
-
使用 javascript 获取字体大小,然后进行数学运算并设置行高和边距。或者如果字体大小不是动态的,则只需对其进行硬编码
-
@Giovanni 是的,这就是我想要的。如果可能的话,还有一个边距底部。
-
@Ace,这背后的原因是要有一个响应式字体基线。使用 javascript 重新设置样式会杀死移动浏览器。
-
您可以将
line-height设置为父容器(示例中为body)并将其从元素中删除(示例中为p)jsfiddle