这两个 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/