HTML pre 标签

htmlweb developmentfront end technology更新于 2026/1/28 15:08:17

HTML 中的 <pre> 标签用于设置预格式化的文本。其中的文本会保留空格和换行符,也就是说,文本在网页中的显示效果与在 HTML 代码中添加的效果相同。

现在让我们看一个实现 <pre> 标签的示例 −

示例

<!DOCTYPE html>
<html>
<body>
<h2>Demo Heading</h2>
<pre>
   This is a demo text
   and will appear
   in the same format as
   it
   is visible
   here. The pre tag displays
   the text in a fixed-width
   font. It preserves
   both spaces and
   line breaks as you can see
   here.
</pre>
</body>
</html>

输出

在上面的例子中,我们设置了一些包含意外空格和换行符的文本。这样做只是为了演示如何使用 <pre> 将文本显示为 −。

<pre>
This is a demo text
and will appear
in the same format as
it
is visible
here. The pre tag displays
the text in a fixed-width
font. It preserves
both spaces and
line breaks as you can see
here.
</pre>

现在,由于我们已经使用了 <pre> 标签,上面的文本将通过保留空格和换行符显示在网页上。


相关文章


有用资源