说明:
这里发生了一些事情。
在您的示例中,small 元素是inline-level element,这意味着它的垂直对齐方式由vertical-align property 确定。
vertical-align的默认值为baseline,这意味着small元素的基线将与父框的基线对齐:
将框的基线与父框的基线对齐。如果框没有基线,则将下边距边缘与父级的基线对齐。
接下来,您需要考虑line-height property 以及它是如何calculated。您还需要考虑leading and half-leading。在 CSS 中,半前导是通过找出元素的 line-height 和 font-size 之间的差异,将其分成两半,然后在文本上方和下方放置计算出的空间量来确定的。
为了说明,这里有一个示例图片展示了这一点 (taken from W3.org):
由于line-height是20px,并且small元素有13px的font-size,那么我们可以确定3.5px的空格是在small元素的文字上方和下方添加的:
(20px - 13px) / 2 = 3.5px
同样,如果我们计算具有16px 的font-size 的环绕文本节点的半前导,那么我们可以确定在周围文本的上方和下方添加了2px 的空格。
(20px - 16px) / 2 = 2px
现在,如果我们将这些半前导空间计算与vertical-align 属性相关联,您会注意到实际上在small 元素的基线下方添加了更多空间。这就解释了为什么包含small 元素的p 元素的计算高度大于另一个p 元素的计算高度。
话虽如此,您会期望p 元素的计算高度随着small 元素的font-size 的减小而继续增加。为了进一步说明这一点,您会注意到当small 元素的font-size 设置为6px 时,p 元素的计算高度为23px。
p { line-height: 20px; }
small { font-size: 6px; }
<p>some normal-sized text</p>
<p>some <small>small</small>-sized text</p>
可能的解决方法:
既然我们知道高度差是由添加到baseline 的额外空间造成的,我们可以将small 元素的vertical-align 值更改为top:
p { line-height: 20px; }
small { vertical-align: top; }
<p>some normal-sized text</p>
<p>some <small>small</small>-sized text</p>
或者,您可以为 small 元素指定 line-height 或 17px,这将导致在元素上方和下方添加 2px 的空间(与为我们上面计算的周围文本)。
// Surrounding text that is 16px:
(20px - 16px) / 2 = 2px
// Small element text that is 13px:
(17px - 13px) / 2 = 2px
p { line-height: 20px; }
small { line-height: 17px; }
<p>some normal-sized text</p>
<p>some <small>small</small>-sized text</p>
但是,您真的不想计算其中的任何内容并对其进行硬编码,这意味着您应该只使用相对的 line-height 并省略 px 单位。
由于font-size 是16px 并且所需的line-height 值是20px,您可以将line-height 除以font-size 并得到1.25:
p { line-height: 1.25; }
<p>some normal-sized text</p>
<p>some <small>small</small>-sized text</p>
如果您不想使用相对的line-height: 1.25,并且想继续使用line-height: 20px,那么您当然可以将small 元素的line-height 值重置回初始值,即normal.
p { line-height: 20px; }
small { line-height: normal; }
<p>some normal-sized text</p>
<p>some <small>small</small>-sized text</p>