--- title:
slug: Web/HTML/Element/pre tags: - HTML - HTML分组内容 - 元素 translation_of: Web/HTML/Element/pre ---{{HTMLRef}}HTML <pre> 元素表示预定义格式文本。在该元素中的文本通常按照原文件中的编排,以等宽字体的形式展现出来,文本中的空白符(比如空格和换行符)都会显示出来。(紧跟在 <pre> 开始标签后的换行符也会被省略)
{{EmbedInteractiveExample("pages/tabbed/pre.html", "tabbed-standard")}}The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
注意: 你需要将该元素里的 '<' 字符转义为 '<' 以保证代码里的关闭代码不被浏览器解释为标签。
内容类别 | 流内容( flow content) , 可触知的内容(palpable content). |
---|---|
允许的 内容 | Phrasing content. |
省略标签 | {{no_tag_omission}} |
允许的 父元素 | 任何可以接受流内容( flow content) 的元素 |
允许的 ARIA 角色 | 任何 |
DOM 接口 | {{domxref("HTMLPreElement")}} |
这个元素只具有全局属性。
<p>Using CSS to change the font color is easy.</p>
<pre>
body {
color: red;
}
</pre>
{{EmbedLiveSample("Example")}}
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.
A combination of the {{HTMLElement("figure")}} and {{HTMLElement("figcaption")}} elements, supplemented by a combination of an {{htmlattrxref("id")}} and the ARIA role
and aria-labelledby
attributes allow the preformatted text to be announced as an image, with the figcaption
serving as the image's alternate description.
<figure role="img" aria-labelledby="cow-caption">
<pre>
___________________________
< I'm an expert in my field. >
---------------------------
\ ^__^
\ (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>
规范 | 状态 | 备注 |
---|---|---|
{{SpecName('HTML WHATWG', 'grouping-content.html#the-pre-element', '<pre>')}} | {{Spec2('HTML WHATWG')}} | No significant change from {{SpecName("HTML WHATWG")}} |
{{SpecName('HTML5 W3C', 'grouping-content.html#the-pre-element', '<pre>')}} | {{Spec2('HTML5 W3C')}} | No significant change from {{SpecName("HTML4.01")}} |
{{SpecName('HTML4.01', 'struct/text.html#h-9.3.4', '<dl>')}} | {{Spec2('HTML4.01')}} | Deprecated the cols attribute |
{{Compat("html.elements.pre")}}