对于内联和块代码片段,<code> vs <pre> vs <samp>

Ste*_*ett 314 html semantic-markup

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

似乎我不能同时使用<code>它们,因为如果我把CSS块属性放在它上面(带display: block;),它将打破内联片段.

我很好奇人们在做什么.使用<code>的块,<samp>内联?使用<code><blockquote>或类似的东西?

我想保持实际的HTML尽可能简单,避免类,因为其他用户将维护它.

Jos*_*Lee 334

使用<code>内联代码可以包装和<pre><code>块代码不能换行.<samp>是用于示例输出,所以我会避免使用它来表示示例代码(读者要输入的代码).这就是Stack Overflow的功能.

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

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

pre元素表示预格式化文本块,其中结构由印刷约定而不是元素表示.

可以使用pre元素的一些示例:

  • 包括计算机代码的片段,其结构根据该语言的惯例表示.

[...]

为了表示计算机代码块,pre元素可以与代码元素一起使用; 为了表示计算机输出块,pre元素可以与samp元素一起使用.类似地,可以在pre元素内使用kbd元素来指示用户要输入的文本.

在下面的代码段中,提供了一个计算机代码示例.

<p>This is the <code>Panel</code> constructor:</p>
<pre><code>function Panel(element, canClose, closeHandler) {
  this.element = element;
  this.canClose = canClose;
  this.closeHandler = function () { if (closeHandler) closeHandler() };
}</code></pre>
Run Code Online (Sandbox Code Playgroud)

  • 我不同意.与预格式化文本相反的是文档中的纯文本.使用CSS制作`<code>`块级别是非语义的.这种方式[在HTML5中推荐](http://www.w3.org/TR/html5/grouping-content.html#the-pre-element). (9认同)
  • 这可能是正确的方法,但我仍然认为这是愚蠢的.HTML-devs预见到需要一个`<code>`标签,但我们决定只写一行?或者我想,因为他们不想有两个标签,一个块和一个内联?仍然......用CSS制作`<code>`块级别有什么问题?我以为我们应该写"语义"HTML.`<code>`是好的和语义的,但`<pre>`不是那么多. (8认同)
  • -1.OP的核心问题是如何完成包装的块片段.您已经解决了内联代码以及必须*不包装的块代码,但这并未解决OP的主要问题. (5认同)
  • @Steve Bennett,在CSS`white-space:normal;`虽然我不明白为什么你会为代码做这个.另外这个`<pre> <code>`的东西是愚蠢的,`<pre>`标签在标准中非常清楚地定义为@jleedev所提到的"计算机代码"(和其他东西).是因为你认为`<code>`是一个更好的名字?抱歉,这不会使它更具语义性.标签"<address>"有一个类似的情况,它听起来并不像"作者",但是标准说这就是它的用途,所以就是这样. (3认同)
  • `&lt;pre&gt;` 的问题在于它也修改了空格处理:所有空格都被保留,并且包装被关闭。除非有办法关闭它? (2认同)

Ste*_*ett 76

我完全错过的东西:<pre>可以用CSS控制非包装行为.所以这给出了我正在寻找的确切结果:

code { 
    background: hsl(220, 80%, 90%); 
}

pre {
    white-space: pre-wrap;
    background: hsl(30,80%,90%);
}
Run Code Online (Sandbox Code Playgroud)
Here's an example demonstrating the <code>&lt;code&gt;</code> tag.

<pre>
Here's a very long pre-formatted formatted using the &lt;pre&gt; tag. Notice how it wraps?  It goes on and on and on and on and on and on and on and on and on and on...
</pre>
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/9mCN7/


sle*_*man 36

我个人使用,<code>因为这是最正确的语义.然后,为了区分内联代码和块代码,我要添加一个类:

<code class="inlinecode"></code>
Run Code Online (Sandbox Code Playgroud)

对于内联代码或:

<code class="codeblock"></code>
Run Code Online (Sandbox Code Playgroud)

用于代码块.取决于哪个不太常见.

  • @Steve:主要的是定义一个默认的`<code>`块,没有用于最常见用例的类.然后任何想要做不常见事情的人只需要添加这个类.以任何其他方式执行仍将要求用户键入额外的.这样,用户可以将其视为添加特殊标记而不是使用完全不同的结构. (3认同)

小智 14

对于正常的内联 <code>使用:

<code>...</code>
Run Code Online (Sandbox Code Playgroud)

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

<code style=display:block;white-space:pre-wrap>...</code>
Run Code Online (Sandbox Code Playgroud)

或者,<codenza>为break衬块定义一个标签<code> (无类)

<script>
</script>
<style>
  codenza, code {}     /*  noop mnemonic aide that codenza mimes code tag  */
  codenza {display:block;white-space:pre-wrap}
</style>`
Run Code Online (Sandbox Code Playgroud)

测试:(注意:以下是使用data:URI协议/方案的scURIple ,因此%0Anl格式代码在剪切并粘贴到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>
Run Code Online (Sandbox Code Playgroud)


vsy*_*ync 13

使用(过时)标记按原样显示HTML代码<xmp>:

<xmp>
<div>
  <input placeholder='write something' value='test'>
</div>
</xmp>
Run Code Online (Sandbox Code Playgroud)

很遗憾这个标签已被弃用,它仍然适用于浏览器,它是一个糟糕的标签.没有必要逃避它内部的任何东西.真高兴啊!


www*_*Com 7

考虑TextArea

人们通过谷歌找到这一点,并寻找更好的方法来管理他们的片段的显示也应该考虑<textarea>这给了很多在宽度/高度控制,滚动等注意到@vsync提到的过时的标签<xmp>,我觉得<textarea readonly>是一个很好的替代品用于显示HTML,而无需转义其中的任何内容(</textarea>可能出现在其中的位置除外)。

例如,要显示具有受控制的换行符的单行,请考虑<textarea rows=1 cols=100 readonly> html或其他带有制表符和CrLf的字符 </textarea>

<textarea rows=5 cols=100 readonly>Example text with Newlines,
tabs & space,
  html tags etc <b>displayed</b>.
    However, note that &amp; still acts as an escape char..
      Eg: &lt;u&gt;(text)&lt;/u&gt;
</textarea>
Run Code Online (Sandbox Code Playgroud)

比较所有...

<h2>Compared: TEXTAREA, XMP, PRE, SAMP, CODE</h2>
<p>Note that CSS can be used to override default fixed space fonts in each or all these.</p>
    
    
<textarea rows=5 cols=100 readonly>TEXTAREA: Example text with Newlines,
tabs & space,
  html tags etc <b>displayed natively</b>.
    However, note that &amp; still acts as an escape char..
      Eg: &lt;u&gt;(text)&lt;/u&gt;</textarea>

<xmp>XMP: Example text with Newlines,
tabs & space,
  html tags etc <b>displayed natively</b>.
    However, note that &amp; (&) will not act as an escape char..
      Eg: &lt;u&gt;(text)&lt;/u&gt;
</xmp>

<pre>PRE: Example text with Newlines,
tabs & space,
  html tags etc <b>are interpreted, not displayed</b>.
    However, note that &amp; still acts as an escape char..
      Eg: &lt;u&gt;(text)&lt;/u&gt;
</pre>

<samp>SAMP: Example text with Newlines,
tabs & space,
  html tags etc <b>are interpreted, not displayed</b>.
    However, note that &amp; still acts as an escape char..
      Eg: &lt;u&gt;(text)&lt;/u&gt;
</samp>

<code>CODE: Example text with Newlines,
tabs & space,
  html tags etc <b>are interpreted, not displayed</b>.
    However, note that &amp; still acts as an escape char..
      Eg: &lt;u&gt;(text)&lt;/u&gt;
</code>
Run Code Online (Sandbox Code Playgroud)


Sto*_*ely 6

这是一个简单的、非 JavaScript 的 HTML 解决方案,使用起来非常简单,并且优于使用<pre><code>元素,或者总是矫枉过正的笨拙的 JavaScript 解决方案。这个技巧我已经用了20年了!它适用于所有新旧浏览器。今天的孩子们未能学习旧的方法。

它允许您的用户快速剪切和粘贴代码示例。它还允许您快速、轻松地将代码放入 HTML 元素中,而无需转义<>在使用<code>.

使用该<textarea>元素来共享代码,如下所示:

<textarea class="code" contenteditable="true" spellcheck="false" aria-label='Code Sample'>
  My Sample Bookmark:
  <a href="#bookmark1" id="b1" title="View my bookmark" target="_blank" rel="noreferrer nofollow noopener" accesskey="a" tabindex="0" aria-label="Bookmark">Got to My Bookmark</a>
</textarea>
Run Code Online (Sandbox Code Playgroud)

...使用一些简单的 CSS 样式...

<style>
        textarea.code {
            display: block;
            width: 90%;
            min-height: 5em;
            overflow-y: auto;
            overflow-x: hidden;
            font-family: monospace;
            border: 1px solid #bbb;
            padding: 1em;
            white-space:pre-wrap;
        }
</style>
Run Code Online (Sandbox Code Playgroud)

请注意,它看起来像常规等宽字体<code>,但却是块级的,支持文本格式,例如<pre>,长文本现在换行,代码框大小是可控的,并且允许更灵活地显示大型 HTML 或用户可以更轻松访问的脚本块。

顺便说一句......你仍然可以使用<pre><code>. 对于较小的例子我仍然这样做。并且不用担心使用的语义或可访问性问题,<textarea>因为它是替换元素并且具有更通用的用途。如果您担心,只需向您的 中添加一个 ARIA 标签<textarea>,就像我上面所做的那样。