我对以下CSS的行为感到困惑,在这个小提琴中也对此进行了说明。
<style type="text/css">
p {
font-size: 14px;
}
.percentage {
line-height: 150%;
}
.em-and-a-half {
line-height: 1.5em;
}
.decimal {
line-height: 1.5;
}
.smaller {
font-size:50%;
}
.caption {
font-weight: bolder;
font-size: 80%;
}
</style>
<p class="caption">default line spacing</p>
<p>This tutorial provides a brief introduction to the
programming <span class="">language</span> by using one of its picture-drawing
libraries. This tutorial provides a brief introduction to the programming language. This tutorial provides a brief introduction to the programming language.</p>
<p>This tutorial provides a brief introduction to the
programming <span class="smaller">language</span> by using one of its picture-drawing
libraries. This tutorial provides a brief introduction to the programming language. This tutorial provides a brief introduction to the programming language.</p>
<p class="caption">line-height: 150%</p>
<p class="percentage">This tutorial provides a brief introduction to the
programming <span class="">language</span> by using one of its picture-drawing
libraries. This tutorial provides a brief introduction to the programming language. This tutorial provides a brief introduction to the programming language.</p>
<p class="percentage">This tutorial provides a brief introduction to the
programming <span class="smaller">language</span> by using one of its picture-drawing
libraries. This tutorial provides a brief introduction to the programming language. This tutorial provides a brief introduction to the programming language.</p>
<p class="caption">line-height: 1.5em</p>
<p class="em-and-a-half">This tutorial provides a brief introduction to the
programming <span class="">language</span> by using one of its picture-drawing
libraries. This tutorial provides a brief introduction to the programming language. This tutorial provides a brief introduction to the programming language.</p>
<p class="em-and-a-half">This tutorial provides a brief introduction to the
programming <span class="smaller">language</span> by using one of its picture-drawing
libraries. This tutorial provides a brief introduction to the programming language. This tutorial provides a brief introduction to the programming language.</p>
<p class="caption">line-height: 1.5</p>
<p class="decimal">This tutorial provides a brief introduction to the
programming <span class="">language</span> by using one of its picture-drawing
libraries. This tutorial provides a brief introduction to the programming language. This tutorial provides a brief introduction to the programming language.</p>
<p class="decimal">This tutorial provides a brief introduction to the
programming <span class="smaller">language</span> by using one of its picture-drawing
libraries. This tutorial provides a brief introduction to the programming language. This tutorial provides a brief introduction to the programming language.</p>
前两段具有默认的行距。第二段有一个较小的词。但这不会影响该段中的行距。并非应该如此,但是-
接下来的两段有line-height: 150%
。同样,第二段有一个较小的词。但是这一次,由于原因不清楚,较小的字体在前两行之间(至少在Safari,Chrome,Firefox和Explorer中)创建了额外的空间。这是我试图修复的CSS中的原始问题。(我推测这与浏览器缩小单词然后垂直向下移动以重新对齐基线有关。)
接下来的两段有line-height: 1.5em
。我的理解是1.5em
和150%
。确实,行为是相同的:第二段的前两行之间有多余的空间。
但是这里很奇怪:接下来的两段都有line-height: 1.5
-没有指定单位。这次,多余空间的问题消失了。
总而言之,当父级和子级的行高不同时(通过继承无单位值),CSS似乎给出一致的行距结果,而当父级和子级的行高相同时,CSS给出的行距结果不一致。
因此,我的问题是:
我知道CSS规范之间1.5
和150%
或它的同义词之间存在故意的语义差异1.5em
。(即:将无单位值传递给子元素,并使用子字体的字体大小计算其线高,而百分比或em值将导致为父元素计算线高,然后将该计算值传递给这个孩子。)但是,这如何解释这里看到的行为差异?多余的空间从哪里来?如果是某些CSS定位规则的结果,那么该规则是什么?
或者,如果这些示例都应以相同的方式呈现,那么哪个实现不正确?(关于第2季度的说明:在不同的浏览器上呈现渲染异常的事实强烈表明,没有一个实现不正确,这将带您回到问题(1)。)
在实际应用中,是有缺点切换到无单位的测量样 (答案:否)1.5
的 line-height
?
根据提出的答案中的线索,我认为在这些示例中看到的渲染行为是违反直觉的,但是是正确的,并且受规范中几个规则与整个CSS框模型的相互作用的要求。
CSS通过公式line-height
= L + AD来计算框所需的前导L ,其中AD是字体的“从顶部到底部的距离”。然后“在A的前面加上一半,在D的下面加上另一半”。因此,有文字font-size:16px
和line-height:24px
会的上方和下方领先4PX。文本font-size:8px
和line-height:24px
将拥有领先的上面和下面加上了8px。
但是,默认情况下,“用户代理必须按其相关基准对齐字形。” 。这开始解释这里发生了什么。当line-height
用百分比或em指定时,子项将继承计算值(此处为smaller
跨度)。这意味着smaller
跨度line-height
与父块相同。但是由于使用L + AD公式,该跨度的文本在顶部和底部具有更多的前导,因此基线在其框中位于较高的位置。浏览器smaller
垂直向下推跨度以匹配基线。
但是浏览器遇到了一个新问题-如何处理封闭块中的行距,该行已被基线调整过程打乱。规范也解决了这一问题:line-height
块级元素的“指定元素内线框的最小高度”。意思是,CSS不会保证您将获得精确的结果line-height
,只是您至少会得到该数目。因此,浏览器在封闭块中将各行分开,以便重新对齐的子框适合。
这是反意图的原因是,它与大多数文字处理器和页面布局程序的工作方式相反。在这些程序中,段落中较小的文本段通过其基线(如CSS)对齐,但是行高被强制为基线之间的距离,而不是围绕较小文本的框。但这不是错误-CSS是围绕box模型设计的。所以最后,我们可以说这种间距行为是该模型的结果。
这仍然让我们用无单位的行高来解释示例中的行为:
首先,请注意,当line-height
指定no时,浏览器默认将应用无单位的行高。这是规范所必需的:line-height
is的初始值normal
定义为“具有与<number>相同的含义”,并且规范建议其值为“ 1.0到1.2之间”。这与我们在上面的示例中看到的一致,其中带有line-height: 1.5
的段落与没有设置行高的段落具有相同的行为(即,它们隐含地获得line-height: normal
)
正如其他人指出的那样,当段落具有时line-height: 1.5
,smaller
跨度不会继承所计算的段落的行高。相反,smaller
跨度将根据其自身的字体大小来计算其自身的行高。当段落具有时line-height: 1.5; font-size: 14px
,则其计算的行高为14px * 1.5 = 21px。如果smaller
span仅具有属性font-size: 50%
,则其字体大小为14px * 50%= 7px,其行高为7px * 1.5 = 10.5px(通常将其四舍五入为整个像素)。但总的来说,该smaller
框的大小是周围文字的一半。
和以前一样,浏览器将使smaller
跨度与相邻基线垂直对齐。但是这一次,因为周围的框smaller
比周围的文本短,所以此重新对齐在封闭块中没有任何副作用。它已经适合了,因此不需要像以前一样扩展父段的行。
两种情况都代表规范的一致实施。这是个好消息,因为这意味着我们可以预测行间距行为。
这使我们回到了这个问题的原始原因。虽然我现在知道CSS盒子模型需要这种行为,但是作为一名练习打字员,这很少是我想要的行为。我想要的是段落中的行具有一致且精确的行距,即使该段落中的某些文本跨度较小。
不幸的是,似乎没有办法像文字处理程序或页面布局程序中那样直接在CSS中强制精确的行距。同样,这是因为CSS框模型:它不使用基线到基线的行距模型,line-height
而是指定为最小度量值,而不是最大度量值。
但是我们至少可以说,无单位的行高值可以在CSS中产生精确的行距的最佳近似值。像我这样的挑剔的排版人员应该对它们感到满意,因为无单位的值已得到规范的认可,并且它们在浏览器中产生一致的结果。他们不是黑客,也不是不推荐使用的东西。
需要注意的是,它们仍然只是一个近似值。无单位的行高值不会更改基础的CSS框模型,也不会更改CSS框定位规则。因此,在某些情况下,他们可能无法获得预期的结果。但是,永远保持警惕是好的排版的代价。小心点。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句