HTML <pre> 元素表示预定义格式文本。在该元素中的文本通常按照原文件中的编排,以等宽字体的形式展现出来,文本中的空白符(比如空格和换行符)都会显示出来。(紧跟在 <pre> 开始标签后的换行符也会被省略)

 

 

注意: 你需要将该元素里的 '<' 字符转义为 '&lt;' 以保证代码里的关闭代码不被浏览器解释为标签。

内容类别 流内容( flow content) , 可触知的内容(palpable content).
允许的 内容 Phrasing content.
省略标签 不允许,开始标签和结束标签都不能省略。
允许的 父元素 任何可以接受流内容( flow content) 的元素
允许的 ARIA 角色 任何
DOM 接口 HTMLPreElement

属性

这个元素只具有全局属性。

cols
定义每行的最大字符数。这是一个非标准的属性,作用与width相同。要实现这样的效果,应该使用CSS。
width
包含每行的最大字符数。虽然在技术上仍被实现,但这个属性没有视觉效果。要实现这样的效果,应该使用CSS。
wrap
提示溢出怎样发生。在现代浏览器中,这个提示会被忽略,且被没有视觉效果。要实现这样的效果,应该使用CSS。

例子

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

结果

 

 

规范

规范 状态 备注
HTML Living Standard
<pre>
Living Standard No significant change from HTML Living Standard
HTML5
<pre>
Recommendation No significant change from HTML 4.01 Specification
HTML 4.01 Specification
<dl>
Recommendation Deprecated the cols attribute

浏览器兼容性

 

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
preChrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
cols
DeprecatedNon-standard
Chrome No support NoEdge No support NoFirefox No support 1 — 29IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoEdge Mobile No support NoFirefox Android No support 4 — 29Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support No
width
DeprecatedNon-standard
Chrome Full support Yes
Notes
Full support Yes
Notes
Notes Specifying the width attribute has no layout effect.
Edge Full support Yes
Notes
Full support Yes
Notes
Notes Specifying the width attribute has no layout effect.
Firefox Full support 1
Notes
Full support 1
Notes
Notes Since Firefox 29, specifying the width attribute has no layout effect.
IE Full support Yes
Notes
Full support Yes
Notes
Notes Specifying the width attribute has no layout effect.
Opera Full support Yes
Notes
Full support Yes
Notes
Notes Specifying the width attribute has no layout effect.
Safari Full support Yes
Notes
Full support Yes
Notes
Notes Specifying the width attribute has no layout effect.
WebView Android Full support Yes
Notes
Full support Yes
Notes
Notes Specifying the width attribute has no layout effect.
Chrome Android Full support Yes
Notes
Full support Yes
Notes
Notes Specifying the width attribute has no layout effect.
Edge Mobile Full support Yes
Notes
Full support Yes
Notes
Notes Specifying the width attribute has no layout effect.
Firefox Android Full support 4
Notes
Full support 4
Notes
Notes Since Firefox 29, specifying the width attribute has no layout effect.
Opera Android Full support Yes
Notes
Full support Yes
Notes
Notes Specifying the width attribute has no layout effect.
Safari iOS Full support Yes
Notes
Full support Yes
Notes
Notes Specifying the width attribute has no layout effect.
Samsung Internet Android Full support Yes
wrap
DeprecatedNon-standard
Chrome ? Edge No support NoFirefox Full support 1IE ? Opera ? Safari ? WebView Android ? Chrome Android ? Edge Mobile No support NoFirefox Android Full support 4Opera Android ? Safari iOS ? Samsung Internet Android ?

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
Non-standard. Expect poor cross-browser support.
Non-standard. Expect poor cross-browser support.
Deprecated. Not for use in new websites.
Deprecated. Not for use in new websites.
See implementation notes.
See implementation notes.

 

参见

文档标签和贡献者

最后编辑者: gafish,