我的网站将有一些内联代码(“当使用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>表示可以换行的内联代码,使用<pre><code>表示不能换行的块代码。<samp>是用于示例输出的,因此我将避免使用它来表示示例代码(读者将输入)。这就是Stack Overflow所做的。

(更好的是,如果你想要易于维护,让用户编辑文章作为Markdown,那么他们不必记得使用<pre><code>。)

HTML5在“pre元素”中同意这一点:

The pre element represents a block of preformatted text, in which structure is represented by typographic conventions rather than by elements. Some examples of cases where the pre element could be used: Including fragments of computer code, with structure indicated according to the conventions of that language. […] To represent a block of computer code, the pre element can be used with a code element; to represent a block of computer output the pre element can be used with a samp element. Similarly, the kbd element can be used within a pre element to indicate text that the user is to enter. In the following snippet, a sample of computer code is presented.

<p>这是<code>面板</code>构造函数:</p> <pre><code>函数面板(element, canClose, closeHandler) { 这一点。元素=元素; 这一点。canClose =取消; 这一点。closeHandler = function () {if (closeHandler) closeHandler()}; 代码}< / > < / >之前

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

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

对于内联代码或:

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

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

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

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

我完全错过了:<pre>的非包装行为可以用CSS控制。这给出了我想要的结果:

代码{ 背景:hsl(220, 80%, 90%); } {前 空白:pre-wrap; 背景:高速逻辑(30、80%、90%); } 下面的例子演示了<code>&lt;code&gt;</code>标签。 < >之前 这是一个很长的预格式化格式使用&lt;标签。注意到它是如何换行的吗?没完没了,没完没了,没完没了…… < / >之前

http://jsfiddle.net/9mCN7/

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

<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/