我要在html页面上显示一堆文字.文字看起来像这样:
+-001 This is a Line 00:12:04
002 ----------------------------------
- 003 Everthing looks good so far ------
Run Code Online (Sandbox Code Playgroud)
文本是"预格式化的"并包含许多白色空格和短划线,并且每一行都具有相同的长度(所有字符具有相同的宽度).
有没有办法在html中显示文本而不会丢失格式?
Shi*_*abu 26
将文字包裹在<pre>标签内.
<pre>
+-001 This is a Line 00:12:04
002 ----------------------------------
- 003 Everthing looks good so far ------
</pre>
Run Code Online (Sandbox Code Playgroud)
HTML方法是使用为此类用途而设计的pre元素,但要注意这一点
<pre>标记放在第一行的开头,将</pre>标记放在最后一行的末尾.否则,某些浏览器的行为可能就像在元素的开头或结尾处有一个空行.<和&内容的出现(在某些情况下不需要这样做,但最简单的是忽略它.示例(我添加了包含表达式1 + 1 <3的行):
<pre>+-001 This is a Line 00:12:04
002 ----------------------------------
- 003 Everthing looks good so far ------
- 004 Simple as 1 + 1 < 3 ------</pre>
Run Code Online (Sandbox Code Playgroud)
您可避免溢出<,并&通过使用xmp替代pre,但xmp已被宣布为过时,过时的,不管(但它很好地工作在实践中).
<xmp>+-001 This is a Line 00:12:04
002 ----------------------------------
- 003 Everthing looks good so far ------
- 004 Simple as 1 + 1 < 3 ------</xmp>
Run Code Online (Sandbox Code Playgroud)