<pre>: The Preformatted Text element

<pre> HTML 元素表示预格式化文本,该文本将完全按照 HTML 文件中的写入方式呈现。文本通常使用非比例字体或 monospaced 字体呈现。该元素内的空白按书写方式显示。

¥The <pre> HTML element represents preformatted text which is to be presented exactly as written in the HTML file. The text is typically rendered using a non-proportional, or monospaced, font. Whitespace inside this element is displayed as written.

默认情况下,<pre>block-level 元素,即其默认 display 值为 block

¥By default, <pre> is a block-level element, i.e. its default display value is block.

Try it

如果必须在 <pre> 标记内显示保留字符,例如 <>&",则必须使用各自的 HTML 实体 对这些字符进行转义。

¥If you have to display reserved characters such as <, >, &, and " within the <pre> tag, the characters must be escaped using their respective HTML entity.

属性

¥Attributes

该元素仅包含 全局属性

¥This element only includes the global attributes.

cols Non-standard Deprecated

包含一行应具有的首选字符数。它是 width 的非标准同义词。要达到这样的效果,请改用 CSS width

width Deprecated Non-standard

包含一行应具有的首选字符数。虽然技术上仍然实现,但该属性没有视觉效果;要达到这样的效果,请改用 CSS width

wrap Non-standard Deprecated

是一个提示,指示溢出必须如何发生。在现代浏览器中,此提示被忽略,并且不会产生任何视觉效果;要达到这样的效果,请改用 CSS white-space

无障碍问题

¥Accessibility concerns

为使用预格式化文本创建的任何图片或图表提供替代描述非常重要。替代描述应清楚、简洁地描述图片或图表的内容。

¥It is important to provide an alternate description for any images or diagrams created using preformatted text. The alternate description should clearly and concisely describe the image or diagram's content.

患有低视力状况并借助屏幕阅读器等辅助技术进行浏览的人们可能无法理解按顺序读出预格式化文本字符时所代表的内容。

¥People experiencing low vision conditions and browsing with the aid of assistive technology such as a screen reader may not understand what the preformatted text characters are representing when they are read out in sequence.

<figure><figcaption> 元素的组合,辅以 pre 元素上的 ARIA rolearia-label 属性,允许将预格式化的 ASCII 艺术声明为带有替代文本的图片,并且 figcaption 充当图片的标题。

¥A combination of the <figure> and <figcaption> elements, supplemented by the ARIA role and aria-label attributes on the pre element allow the preformatted ASCII art to be announced as an image with alternative text, and the figcaption serving as the image's caption.

示例

¥Example

html
<figure>
  <pre role="img" aria-label="ASCII COW">
      ___________________________
  &lt; I'm an expert in my field. &gt;
      ---------------------------
          \   ^__^
           \  (oo)\_______
              (__)\       )\/\
                  ||----w |
                  ||     ||
  </pre>
  <figcaption id="cow-caption">
    A cow saying, "I'm an expert in my field." The cow is illustrated using
    preformatted text characters.
  </figcaption>
</figure>

示例

¥Examples

基本示例

¥Basic example

HTML

html
<p>Using CSS to change the font color is easy.</p>
<pre>
body {
  color: red;
}
</pre>

结果

¥Result

转义保留字符

¥Escaping reserved characters

HTML

html
<pre>
let i = 5;

if (i &lt; 10 &amp;&amp; i &gt; 0)
  return &quot;Single Digit Number&quot;
</pre>

结果

¥Result

技术总结

¥Technical summary

内容类别 流量内容,可触及的内容。
允许的内容 措辞内容.
标签遗漏 无,开始和结束标记都是强制性的。
允许的父级 任何接受 流动内容 的元素。
隐式 ARIA 角色 generic
允许的 ARIA 角色 任何
DOM 接口 HTMLPreElement

规范

Specification
HTML Standard
# the-pre-element

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看

¥See also