Examples of displaying sample code

When putting sample code on an HTML page, there are several ways to do it. There are pros and cons to each approach. That the method supports markup could be a pro or a con depending on whether you want to mark up your code, or show the markup itself. (eg. I want to show <b>bold</b> and not bold, or vice versa.)

Using CODE

Here is some code.
Line break and spacing must   be coded. with <br />. < less than. & ampersand are escaped.
Anything that looks like a <tag> must be escaped. Tags are rendered.

Using PRE

Code using PRE needs escaped < > &
    line breaks and spacing are preserved. I can still escape </pre>
Pre still marks up tags

Using xmp

with xmp, I can put anything including < & and >. Line breaks and spacing are preserved. <b>tags</b> are not rendered. &amp; entities are not recognized. The only thing I cannot type or display is

Using textarea


  1. Textarea is best for arbitrary code. Great if you want to show markup
  2. pre is great for marking up code with styles such as bold and italics
  3. code is pratcically useless for blocks, but works well for inline styling.
  4. xmp is the best for arbitrary code, but should never be relied upon.