简体   繁体   English

额外的换行符<samp>与`white-space:pre`一起使用,但不对</samp> <pre>

[英]Extra new-line on <samp> with `white-space: pre`, but not on <pre>

In my code, the <pre> and <samp> tags are styled identically. 在我的代码中, <pre><samp>标记的样式相同。 Nevertheless, the <pre> block doesn't start with a newline while the <samp> block does. 但是, <pre>块不会以换行符开头,而<samp>块却以换行符开头。 How can I remove the newline? 如何删除换行符? What's causing the newline to appear? 是什么导致换行出现? Why does it appear on one but not the other? 为什么它会出现在一个而不是另一个上?

I've tried changing the white-space value to pre-line and pre-wrap , both to no avail. 我试图将white-space值更改为pre-linepre-wrap ,但都无济于事。

 pre, samp { background-color: #000; color: #fff; padding: 1rem; margin: 1rem 0; white-space: pre; display: block; overflow-y: auto; } 
 <pre> &gt; This block doesn't start with a newline. </pre> <samp> &gt; This block <em><u>does</u></em> start with a newline, despite having the same styles as the pre block. </samp> 

Here it is in a JSFiddle . 它在JSFiddle中

As @Xufox says, their text content is actually different. 正如@Xufox所说,它们的文本内容实际上是不同的。 This is a consequence of the HTML parser. 这是HTML解析器的结果。 The rule for a <pre> start tag when in the "in body" tree construction state says : “体内”树构造状态下, <pre>开始标签的规则是:

A start tag whose tag name is one of: "pre", "listing" 一个开始标签,其标签名称是以下之一:“ pre”,“ listing”

If the stack of open elements has ap element in button scope, then close ap element. 如果打开元素的堆栈在按钮作用域中具有ap元素,则关闭ap元素。

Insert an HTML element for the token. 插入令牌的HTML元素。

If the next token is a U+000A LINE FEED (LF) character token, then ignore that token and move on to the next one. 如果下一个令牌是U + 000A LINE FEED(LF)字符令牌,则忽略该令牌并继续下一个令牌。 (Newlines at the start of pre blocks are ignored as an authoring convenience.) (为方便编写,忽略了前置块开头的换行符。)

Set the frameset-ok flag to "not ok". 将frameset-ok标志设置为“ not ok”。

So an initial new line is dropped from a <pre> element, but there is no similar rule for a <samp> element. 因此,从<pre>元素删除了一个新的换行符,但是对于<samp>元素没有类似的规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM