我的网站将有一些内联代码(“当使用foo()函数…”)和一些块片段。这些往往是XML,并且有很长的行,我更喜欢浏览器来包装(即,我不想使用<pre>)。我还想把CSS格式放在块片段上。

似乎我不能同时使用<code>,因为如果我把CSS块属性放在上面(用display: block;),它会破坏内联代码段。

我很好奇人们是怎么做的。使用<code>块,和<samp>内联?使用<code><blockquote>或类似的东西?

我希望实际的HTML尽可能简单,避免类,因为其他用户将维护它。


当前回答

对于正常内联<代码>使用:

<code>...</code>

并且对于需要使用blocked <code>的每个地方

<code style="display:block; white-space:pre-wrap">...</code>

或者,定义一个<codenza>标记用于换行衬块<code>(无类)

<script>
</script>
<style>
  codenza, code {}     /*  noop mnemonic aide that codenza mimes code tag  */
  codenza {display:block;white-space:pre-wrap}
</style>`

测试: (注意:以下是一个使用data: URI协议/方案的小规则,因此%0A nl格式代码在保存这样的格式时是必不可少的,当剪切和粘贴到URL栏进行测试时-因此view-source: (ctrl-U)在%0A下面的每一行之前看起来都很好)

data:text/html;charset=utf-8,<html >
<script>document.write(window.navigator.userAgent)</script>
<script></script>
<style>
  codenza, code {}     /*  noop mnemonic aide that codenza mimes code tag  */
  codenza {display:block;white-space:pre-wrap}
</style>
<p>First using the usual &lt;code> tag
<code>
  %0A     function x(arghhh){ 
  %0A          return "a very long line of text that will extend the code beyond the boundaries of the margins, guaranteed for the most part, well maybe without you as a warrantee (except in abnormally conditioned perverse environs in which case a warranty is useless)"
  %0A     }
</code>
and then 
<p>with the tag blocked using pre-wrapped lines
<code style=display:block;white-space:pre-wrap> 
  %0A     function x(arghhh){ 
  %0A          return "a very long line of text that will extend the code beyond the boundaries of the margins, guaranteed for the most part, well maybe without you as a warrantee (except in abnormally conditioned perverse environs in which case a warranty is useless)"
  %0A     }
</code>
<br>using an ersatz tag
<codenza>
  %0A     function x(arghhh){ 
  %0A          return "a very long line of text that will extend the code beyond the boundaries of the margins, guaranteed for the most part, well maybe without you as a warrantee (except in abnormally conditioned perverse environs in which case a warranty is useless)"
 %0A     }
</codenza>
</html>

其他回答

对于正常内联<代码>使用:

<code>...</code>

并且对于需要使用blocked <code>的每个地方

<code style="display:block; white-space:pre-wrap">...</code>

或者,定义一个<codenza>标记用于换行衬块<code>(无类)

<script>
</script>
<style>
  codenza, code {}     /*  noop mnemonic aide that codenza mimes code tag  */
  codenza {display:block;white-space:pre-wrap}
</style>`

测试: (注意:以下是一个使用data: URI协议/方案的小规则,因此%0A nl格式代码在保存这样的格式时是必不可少的,当剪切和粘贴到URL栏进行测试时-因此view-source: (ctrl-U)在%0A下面的每一行之前看起来都很好)

data:text/html;charset=utf-8,<html >
<script>document.write(window.navigator.userAgent)</script>
<script></script>
<style>
  codenza, code {}     /*  noop mnemonic aide that codenza mimes code tag  */
  codenza {display:block;white-space:pre-wrap}
</style>
<p>First using the usual &lt;code> tag
<code>
  %0A     function x(arghhh){ 
  %0A          return "a very long line of text that will extend the code beyond the boundaries of the margins, guaranteed for the most part, well maybe without you as a warrantee (except in abnormally conditioned perverse environs in which case a warranty is useless)"
  %0A     }
</code>
and then 
<p>with the tag blocked using pre-wrapped lines
<code style=display:block;white-space:pre-wrap> 
  %0A     function x(arghhh){ 
  %0A          return "a very long line of text that will extend the code beyond the boundaries of the margins, guaranteed for the most part, well maybe without you as a warrantee (except in abnormally conditioned perverse environs in which case a warranty is useless)"
  %0A     }
</code>
<br>using an ersatz tag
<codenza>
  %0A     function x(arghhh){ 
  %0A          return "a very long line of text that will extend the code beyond the boundaries of the margins, guaranteed for the most part, well maybe without you as a warrantee (except in abnormally conditioned perverse environs in which case a warranty is useless)"
 %0A     }
</codenza>
</html>

以Prism.js: https://prismjs.com/#examples为例

它使<pre><code>工作并且很有吸引力。

这适用于我在前端显示代码:

<style>
.content{
    height:50vh;
    width: 100%;
    background: transparent;
    border: none;
    border-radius: 0;
    resize: none;
    outline: none;
}
.content:focus{
    border: none;
    -webkit-box-shadow: none;
    -moz-box-shadow: none;
    box-shadow: none;
}
</style>

<textarea class="content">
<div>my div</div><p>my paragraph</p>
</textarea>

查看现场演示:https://jsfiddle.net/bytxj50e/

就我个人而言,我会使用<code>,因为这在语义上是最正确的。然后,为了区分内联代码和块代码,我添加了一个类:

<code class="inlinecode"></code>

对于内联代码或:

<code class="codeblock"></code>

用于代码块。这取决于哪一种不太常见。

显示HTML代码,使用(过时的)<xmp>标记:

< xmp > < div > <input placeholder='write something' value='test'> < / div > < / xmp >

很遗憾这个标签已经弃用了,但它仍然可以在浏览器上工作,它是一个糟糕的标签。不需要逃离里面的任何东西。真高兴!


显示HTML代码,原样,使用<textarea>标签:

<textarea readonly rows="4" style="background:none;边界:没有;调整:没有;大纲:没有;宽度:100%;" > < div > <input placeholder='write something' value='test'> < / div > textarea > < /