【问题标题】:What is the definition of "the baseline of parent box"?“父框的基线”的定义是什么?
【发布时间】:2016-07-11 15:21:58
【问题描述】:

我无法理解10 Visual formatting model details – W3C 的以下摘录。

摘录:

基线: 将框的基线与父框的基线对齐。如果框没有基线,则将底部边距边缘与父级的基线对齐。

在这种情况下,“父框的基线”是什么意思? “父框”是指行框还是父元素建立的框?如何计算“父框的基线”?

【问题讨论】:

  • 我认为您混淆了与此模型相关的几个定义。我认为您正在寻找支柱的定义。

标签: css


【解决方案1】:

查看此答案的底部以获取此答案的交互式版本,这将帮助您自行验证此答案中的陈述。此答案基于 Vincent De Oliveira 在his blog 上的帖子。必读。

什么是基线?

下面的图片和许多其他类似的图片都指定了基线,但它到底是什么?

内嵌文本元素的高度不同...

为了测量内联文本元素的高度,我将使用术语 content-area。对于 Windows,这些元素的内容区域计算如下:

Content-area=(Win Ascent + Win Descent)/Em Size * Font Size

请看下面的几个例子:

  • Merriweather Sans 的内容区域为 25 像素,因为 (2014+560)/2048*20≈25 像素
  • Noto Sans 的内容区域为 27 像素,因为 (2189+600)/2048*20≈27 像素

您可以找到这些值,例如,使用 FontForge,如下面的屏幕截图所示:

Merriweather Sans(右),Noto Sans(左)

在这种情况下,对于每种字体,我们都有 [content-area]=[line-box],这是一种特殊情况,您可以使用开发工具测量元素的内容区域:

如何对齐文本元素以使文本可读?

为此,您需要使用基线。

在下面的示例中,您可以看到增加 line-height 不会增加 content-box(彩色区域),但会增加橙色内联元素的 line-box。此外,请注意两个元素都沿其基线相互对齐。基线由每种字体分别定义。但是,当两种不同的字体沿它们的基线对齐时,生成的文本很容易阅读。

这是一张基线显示为绿线的图像:

在下面的示例中,橙色元素相对于父内容区域的顶部对齐。但是,番茄色元素相对于父元素的 strut 的基线对齐。 strut 是一个零宽度字符,它启动父级的 line-box。

看起来番茄色元素向上移动了,但事实并非如此。它相对于支柱的位置没有改变。

结论

基线是字体作者用来设计字体并使不同字体“相互兼容”的一条线。选择基线是为了使由不同字体组成的文本(它们都有自己的内容区域)可以沿基线​​对齐并保持易于阅读。

父基线是父支柱的基线,它是一个零宽度字符,具有定义的基线,有助于对齐内联元素。


本答案的互动版

@import url('https://fonts.googleapis.com/css?family=Merriweather+Sans');
@import url('https://fonts.googleapis.com/css?family=Noto+Sans');

span {
  margin: 0;
}

p.pr {
  background-color: blue;
}

span.mw {
  font-family: 'Merriweather Sans', sans-serif;
  font-size: 20px;
  line-height: 40px;
  background-color: orange;
}

span.ar {
  font-family: 'Noto Sans', sans-serif;
  font-size: 20px;
  background-color: tomato;
}
<h1>What is baseline?</h1>
The image below, and many other images alike specify a basline, but what is it exactly?
<img src="https://i.imgur.com/SJrxQHj.png">
<br><br>
<h1>Inline text elements are not the same height...</h1>
To measure the height of an inline text element I will use the term <b>content-area</b>. Content-area of these elements for windows is calculated as follows:
<p>Content-area=(Win Ascent + Win Descent)/Em Size * Font Size</p>
<p>See a few examples below:</p>
<ul>
  <li>Merriweather Sans has a content-area of 25px because (2014+560)/2048*20≈25px <span class="mw">Abg</span></li>
  <li>Noto Sans has a content-area of 27px because (2189+600)/2048*20≈27px <span class="ar">Abg</span></li>
</ul>
<p>You can find this values using for example FontForge, as shown on the screenshots below:</p>
<table>
  <tr>
    <td><img src="https://i.imgur.com/h0th3Rv.png"></td>
    <td><img src="https://i.imgur.com/aRymbaf.png"></td>
  </tr>
</table>
In this case for each font we have [content-area]=[line-box] and this is a special case in which you can measure the content-area of the element with dev-tools:
<img src="https://i.imgur.com/4kuFCIf.png">
<h1>How to align text elements to make text readable?</h1>
<p>To do this you use the baseline.</p>
In the example below you can see that increasing the line-height doesn't increase the content-box (the colored area), but it does increase the line-box of the orange inline element. Furthermore, notice that <b>both elements are mutually aligned along their baseline which is defined by the font itself.</b> The baseline is defined by each font separately. However, when two different fonts align along their baseline, the resulting text is easily readable.
<p class="pr">
  <span class="mw">Abg (line-height=40px)</span><span class="ar">Abg</span>
</p>
This is an image with the baseline showed as a green line: <img src="https://i.imgur.com/OlHqb3J.png">
<br><br>
In the example below, the orange element is aligned in respect to the top of the parent's content area. However, the tomato colored element is aligned in respect to the baseline of the <b>strut</b> of the parent. A strut is a zero-width character which starts the line-box of the parent.
<br><br>
It might seem that the tomato colored element has moved up, but it is not the case. Its position relative to the strut has not changed.
<p class="pr">
  <span class="mw" style="vertical-align:top;">Abg</span><span class="ar">Abg</span>
</p>
<h1>Conclusion</h1>
<p>A baseline is a line which typeface authors use to design their fonts. The baseline is chosen so that a text composed out of different fonts, which all have their own content-area, can be aligned along the baseline and remain easily readable.</p>
<p>A parent baseline is a baseline of the parent's strut, which is a zero-width character with a defined baseline which helps to align inline elements.</p>

【讨论】:

  • 因为你在解释一个复杂的问题方面做得很好,所以我要从中挑一个小洞。看看jsbin.com/kiyofofoka/edit?html,css,output。您会看到.child 元素,即vertical-align:baseline 没有与strut 对齐,而是与其父元素的基线对齐,而父元素本身故意偏离了strut 的基线。这就是规范在 Boltclock 引用中的含义:“关于父内联元素,到父块容器元素的支柱”
  • 顺便说一句,“strut 是一个零宽度字符,它启动父级的 line-box。”最好将其表示为“支柱是一个零宽度字符,它以 包含块每个 行框开头。
  • @Alohci 感谢您提供相关信息,这些信息改进了答案并进一步阐明了这个概念。在答案中,我将文本节点视为匿名元素,它是显式元素(如 body、span、p 等)的子元素。但是您的更正可能更符合规范,因此当我将您的建议应用于答案时花点时间修复答案中的格式。现在我遇到了格式问题;
  • 另外应该注意的是,基线的精确定义应该根据字体的几何形状给出,但是这样的定义会使答案过于复杂。因此,我选择提供一些提示,其余的留给读者凭直觉。
  • 请注意一个小错误,“右”和“左”在 FontForge 屏幕截图的标题中互换(“Merriweather Sans(右),Noto Sans(左)”),与可见的窗口标题。
【解决方案2】:

我也没找到具体的定义,根据规范:

以下值仅对父内联元素或父块容器元素的支柱有意义

可以推断,如果父框有一个文本节点作为子节点,则子文本节点的基线就是父框的基线。

例如:

.parent {
  display: inline;
  background:pink;
  font-size:40px;
  font-family:Microsoft Yahei;
  line-height:2;
  vertical-align:baseline;
}
.element-child {
  font-size:25px;
}
<div class="parent">
   textNode child of parent 
  <span class="element-child">
    element-child with another font-size
  </span>
</div>

无论有无子文本节点,父框的基线与子文本节点的基线位置相同。

【讨论】:

    【解决方案3】:

    规范告诉您父框指的是上面的几段:

    以下值仅对父内联元素或父块容器元素的支柱有意义。

    所以父框指的是由父元素生成的框,您可以像计算任何其他元素一样计算父框的基线。

    【讨论】:

    • -1 因为你试图用他不太可能理解的东西来解释操作者不理解的东西,在没有任何证据的情况下假设他知道什么是“支柱”,或者“......你计算父框的基线就像你计算任何其他元素......”。例如,您为什么会假设他知道如何“计算父框的基线”?为什么你会假设他知道什么是“支柱”?像这样的答案只会散播混乱,让人们对一些关于一个困难主题的有效问题感到愚蠢。
    • @Bright:问题的上下文清楚地表明,提问者正在决定“父框”是指行框还是父元素建立的框。您引用的句子是专门针对该上下文设计的-提问者不需要知道什么是支柱。而且我不认为他们知道如何计算父框的基线——我说他们可以像计算任何其他元素一样这样做。可以合理地假设他们已经知道如何计算元素本身的基线,只是询问如何将其与父元素对齐。
    • 问题从标题“父框的定义是什么”开始提问。就您的回答而言,您已经回答了一个问题,该问题询问“父框的基线”在特定上下文中指的是什么。此外,在我看来,该操作的定义令人困惑。但是再想一想,这个问题在这方面似乎确实模棱两可,并且可以从两个方面来理解。
    【解决方案4】:

    查看this picture 以了解baseline 到底是什么。

    现在,让我们演示一下文档想说的话:

    当您定义vertical-align: baseline 时,该元素将与父元素基线的基线对齐,如上图所示。

    【讨论】:

    • -1 不,不是子元素本身与父元素的基线对齐,而是子元素的下边距边缘,并且仅当子元素的框没有时才会发生一个基线。此外,这不是这个问题的答案。您已经描述了在已经计算了父元素的基线时如何将子元素框的底部边缘与父元素的基线对齐。 op 想知道在没有给出父级基线时如何计算它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-01
    • 1970-01-01
    • 2014-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多