css - 为什么浏览器不呈现由 "white-space: pre"元素内的尾随换行符引起的换行符?

标签 css html

这两个 div 渲染相同。为什么呈现前导换行符而不是尾随换行符(在第二个 div 中)?我读了Line Breaking and Word Boundaries CSS3 规范中的部分,但它没有解释这种行为。

.pre {
  white-space: pre;
  border: 1px solid red;
  margin-bottom: 10px;
}
<div class="pre">
hello
world</div>

<div class="pre">
hello
world
</div>

最佳答案

为什么?好吧,显然是为了避免兼容性问题。

W3C说:

In order to avoid problems with SGML line break rules and inconsistencies among extant implementations, authors should not rely on user agents to render white space immediately after a start tag or immediately before an end tag.

and also :

Note that the white space processing rules have already removed any tabs and spaces after the segment break before these checks [on the value of the white-space property] take place.

现在尚不清楚如果允许浏览器在结束标记之前保留空格,那么哪些会引入不一致,但你知道了。

关于css - 为什么浏览器不呈现由 "white-space: pre"元素内的尾随换行符引起的换行符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43492826/

相关文章:

javascript - 动画 Accordion 高度不起作用

javascript - 通过 html 按钮将对象传递给 javascript 中的函数

html - 如何使两个标题一个在另一个下面并使它们固定在滚动上?

html - 使用 CSS 在父元素中切一个洞

javascript - 我的 Javascript 没有执行将框的背景颜色更改为蓝色。我的代码有什么问题吗?

css - 来自 AngularJS 的字段数组

javascript - 单击事件触发后跨度不显示

html - 有什么方法可以独立于高度增加盒子阴影的宽度吗?

javascript - 使DIV中的内容不可编辑

css - 支持悬停的设备的媒体查询