Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<code> vs <pre> vs <samp> for inline and block code snippets

My site is going to have some inline code ("when using the foo() function...") and some block snippets. These tend to be XML, and have very long lines which I prefer the browser to wrap (i.e., I don't want to use <pre>). I'd also like to put CSS formatting on the block snippets.

It seems that I can't use <code> for both, because if I put CSS block attributes on it (with display: block;), it will break the inline snippets.

I'm curious what people do. Use <code> for blocks, and <samp> for inline? Use <code><blockquote> or something similar?

I'd like to keep the actual HTML as simple as possible, avoiding classes, as other users will be maintaining it.

like image 764
Steve Bennett Avatar asked Jan 06 '11 03:01

Steve Bennett


People also ask

Is pre tag inline or block?

HTML pre element is an inline element.

What is the difference between pre and code?

Another point not brought up yet is that <code> is an inline element, and therefore can be placed within other non-block tags (i.e. <p>), whereas a <pre> tag is a block element, and as example, would not render as expected within a <p>.

Which tag is used for longer block of code?

<code> HTML Tag The <code> element is used to define enclosed text as computer code. It is often paired with the <pre> element to preserve line breaks and indentation when presenting blocks of computer code.


2 Answers

Use <code> for inline code that can wrap and <pre><code> for block code that must not wrap. <samp> is for sample output, so I would avoid using it to represent sample code (which the reader is to input). This is what Stack Overflow does.

(Better yet, if you want easy to maintain, let the users edit the articles as Markdown, then they don’t have to remember to use <pre><code>.)

HTML5 agrees with this in “the pre element”:

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>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>
like image 104
Josh Lee Avatar answered Oct 02 '22 22:10

Josh Lee


Something I completely missed: the non-wrapping behaviour of <pre> can be controlled with CSS. So this gives the exact result I was looking for:

code {       background: hsl(220, 80%, 90%);   }    pre {      white-space: pre-wrap;      background: hsl(30,80%,90%);  }
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>

http://jsfiddle.net/9mCN7/

like image 37
Steve Bennett Avatar answered Oct 02 '22 22:10

Steve Bennett