HTML 空白 : spaces before and after <br>

标签 html css whitespace language-lawyer

我试图更好地理解 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作为引用。它指出

  1. If a space (U+0020) at the beginning of a line has 'white-space' set to 'normal', 'nowrap', or 'pre-line', it is removed.
  2. 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.
  3. 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.
  4. 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 地算作一个“字符”,在第一个片段中,它防止尾随空格位于其行的“末尾”。但我真的不知道。

编辑:明确地说,我知道如何使用 &nbsp;随意创造空间。我的问题是什么规则(关于某些规范)会导致上述行为。

最佳答案

好问题!我已经确认了 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/

相关文章:

html - 帮助居中事物

html - IE 中的导航无法正确显示

css - 改变父 div 背景而不是子 div

objective-c - 从字符串中删除空格

javascript - 围绕轴旋转 css 元素

html - 带有Outlook和gmail的HTML电子邮件字体,雅虎supoortable

javascript - 如何在 DIV 中添加文本但不可见或不使用页面上的任何空间?

html - 将列表样式添加回 li 标签以提高可见性 :collapse;

.net - 使用 XDocument 时如何在属性值中保留空格?

iOS:使用 CoreText 更好地说明理由