HTML Marking up computer code

From WikiOD

Syntax[edit | edit source]

  • <pre>Formatted text</pre>
  • <code>Inline Code</code>

Remarks[edit | edit source]

The code element should be used for any kind of "string that a computer would recognize" (HTML5), for example:

  • source code
  • terms from markup/programming languages (element names, function names, etc.)
  • file names

Related elements

For variables, the var element can be used.

For computer output, the samp element can be used.

For user input, the kbd element can be used.

=== Block with

 and  ===
If the formatting (white space, new lines, indentation) of the code matters, use the pre element in combination with the code element:
    x = 42
    if x == 42:
        print "x is …          … 42"

You still have to escape characters with special meaning in HTML (like < with &lt;), so for displaying a block of HTML code (<p>This is a paragraph.</p>), it could look like this:

    &lt;p>This is a paragraph.&lt;/p>

Inline with [edit | edit source]

If a sentence contains computer code (for example, the name of an HTML element), use the code element to mark it up:

<p>The <code>a</code> element creates a hyperlink.</p>