html - 在代码标签中显示新行

问题描述 投票:0回答:3

我正在尝试显示代码,但所有内容都显示在一行中。如何显示多行代码标签?

这是我的代码:

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<title>Documentación: Events</title>
<body>
<code>
    {
    "id_tabla":3,
    "fecha":"02-09-2015",
    "privacidad":0,
    "timediff_m": "00"
    }
</code>
</body>

这就是浏览器中的显示方式:

{"id_tabla":3,"fecha":"02-09-2015","privacidad":0,"timediff_m": "00"}

编辑

我只能使用

<code>
标签

html
3个回答
60
投票

这已经被回答了这里

code {
  display: block;
  white-space: pre-wrap   
}
<code>
    {
    "id_tabla":3,
    "fecha":"02-09-2015",
    "privacidad":0,
    "timediff_m": "00"
    }
</code>


60
投票

使用

<pre>
代替
<code>

<pre>
  {
    "id_tabla": 3,
    "fecha": "02-09-2015",
    "privacidad": 0,
    "timediff_m": "00"
  }
</pre>

如果您想要内联某些内容,则应使用

<code>
;当您需要多行内容时,应使用
<pre>


0
投票

您可以将 与一些 CSS 一起使用

<code style="display: block; white-space: pre-wrap;">
    {
    "id_tabla":3,
    "fecha":"02-09-2015",
    "privacidad":0,
    "timediff_m": "00"
    }
</code>

或使用

<pre> { "id_tabla":3, "fecha":"02-09-2015", "privacidad":0, "timediff_m": "00" } </pre>

请记住

 修改空白处理并保留所有空格,并关闭换行。因此,您可能必须将  与 CSS 样式一起使用。

© www.soinside.com 2019 - 2024. All rights reserved.