html - pre 标签中的空格不会被忽略

标签 html whitespace pre

我读到“pre”标签会将所有空格和制表符折叠成一个空格,但对我来说不是这样。

为什么会这样?它取决于网络浏览器还是我应该做更多的事情?

最佳答案

来自 HTML 4.01 Specification

The PRE element tells visual user agents that the enclosed text is "preformatted". When handling preformatted text, visual user agents:

  • May leave white space intact.
  • May render text with a fixed-pitch font.
  • May disable automatic word wrap.
  • Must not disable bidirectional processing

PRE 标签将在您输入时留下空白。目的仅此而已。如果您不使用 pre 标记,标准 HTML 将折叠您所写的空白。如果您对保留 空白而不是折叠 感兴趣,请使用 PRE。

这是一个 example在 JSFiddle 中。

这将保留空白:

 <pre>
    Spaces
     and more
      galore
 </pre>

这将折叠空白区域:

 <div>
   All
    together
     now
 </div>

关于html - pre 标签中的空格不会被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6992627/

相关文章:

jquery - 主体的wrapInner()直到指定元素

internet-explorer - 帮助解决空白 : nowrap and normal in IE

html - 为什么浏览器呈现为常规空格字符?

javascript - 当容器文本中的最后一个文本行达到目标边距时,如何重新启动滚动标记?

html - 带有返回符号的CSS自动换行

css - 将代码行号与 CSS 对齐

javascript - 将 vb 脚本替换为 Javascript 以兼容所有浏览器

javascript - 错误 JSON.parse : unexpected end of data at line 1 column 1 of the JSON data

html - 为什么这个 "<p><div><br></div></p>"在 DOM 中显示很奇怪?

php - 删除某些浏览器中页眉和主要内容之间页面顶部的空白