大网数据广告活动
Web 标准与前端开发

<pre>

· 52CE 我爱测

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

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

HTML Demo: <pre>

<pre>
  L          TE
    A       A
      C    V
       R A
       DOU
       LOU
      REUSE
      QUE TU
      PORTES
    ET QUI T'
    ORNE O CI
     VILISÉ
    OTE-  TU VEUX
     LA    BIEN
    SI      RESPI
            RER       - Apollinaire
</pre>
pre {
  font-size: 0.7rem;
  margin: 0;
}

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

<table class="properties"> <tbody> <tr> <th scope="row"><a href="/zh-CN/docs/Web/HTML/Guides/Content_categories">内容类别</a></th> <td>流内容 ( <a href="/zh-CN/docs/Web/HTML/Guides/Content_categories#Flow_content">flow content</a>) , 可触知的内容 (palpable content).</td> </tr> <tr> <th scope="row">允许的 内容</th> <td><a href="/zh-CN/docs/Web/HTML/Guides/Content_categories#Phrasing_content">Phrasing content</a>.</td> </tr> <tr> <th scope="row">标签省略</th> <td>不允许,开始标签和结束标签都不能省略。</td> </tr> <tr> <th scope="row">允许的 父元素</th> <td>任何可以接受流内容 ( <a href="/zh-CN/docs/Web/HTML/Guides/Content_categories#Flow_content">flow content</a>) 的元素</td> </tr> <tr> <th scope="row">允许的 ARIA 角色</th> <td>任何</td> </tr> <tr> <th scope="row">DOM 接口</th> <td>HTMLPreElement</td> </tr> </tbody> </table>

属性

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

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

示例

HTML

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

结果

示例

无障碍

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 figure and figcaption elements, supplemented by a combination of an 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>

规范

浏览器兼容性

参见

  • CSS: white-space, word-break

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。