我试图更好地理解 HTML 空白处理模型。现在我正在比较两个 HTML 片段:
<div>a <br>z</div>
和
<div>a<br> z</div>
第一个片段在重新生成时会产生两行:"a "
和 "z"
(所以第一行有一个尾随空格。)
第二个片段产生两行:"a"
和 "z"
.第二行没有前导空格。
我的问题是:为什么?我目前正在使用这个 http://www.w3.org/TR/CSS2/text.html#white-space-model作为引用。它指出
- If a space (U+0020) at the beginning of a line has 'white-space' set to 'normal', 'nowrap', or 'pre-line', it is removed.
- All tabs (U+0009) are rendered as a horizontal shift that lines up the start edge of the next glyph with the next tab stop. Tab stops occur at points that are multiples of 8 times the width of a space (U+0020) rendered in the block's font from the block's starting content edge.
- If a space (U+0020) at the end of a line has 'white-space' set to 'normal', 'nowrap', or 'pre-line', it is also removed.
- If spaces (U+0020) or tabs (U+0009) at the end of a line have 'white-space' set to 'pre-wrap', UAs may visually collapse them.
天真的阅读会表明,由于要删除行的开头 或 结尾的空格(当 'white-space' 为 'normal' 时),第一个我的片段应该没有尾随空间。但事实并非如此。
这是怎么回事?
我目前的理论是 <br>
被 secret 地算作一个“字符”,在第一个片段中,它防止尾随空格位于其行的“末尾”。但我真的不知道。
编辑:明确地说,我知道如何使用
随意创造空间。我的问题是什么规则(关于某些规范)会导致上述行为。
最佳答案
好问题!我已经确认了 Chrome 和 Firefox 中的行为,并确认它与 <br>
无关。 ,因为它也是由 white-space: pre-line
中的普通换行符触发的条件:
<div style="white-space:pre-line">a
z</div>
我已经 sent an email to the list要求澄清这个问题,并询问我们是否应该更改规范以匹配实现,或者在浏览器上提交错误以匹配规范。
关于HTML 空白 : spaces before and after <br>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32493442/