code ElementThe code element is an inline element declared by the XHTML 1.1 Text Module (Inline Phrasal)
Elements in the Text Module are:span | br (Inline Structural Support Module)em | strong | q | cite | abbr | acronym | code | var | kbd | samp | dfn (Inline Phrasal Support Module)div | p (Block Structural Support Module)h1 - h6 | blockquote | pre | address (Block Phrasal Support Module)
The code element is used to mark up an extract of computer code, for example program code or XHTML.
The contents of a code element are commonly rendered in a monospace font.
For example:
<div><code>#!/usr/bin/perl<br />print "$_ ..\n" for reverse ( 1 .. 10 );<br />print "Blast Off!\n";</code></div>
This renders as:
#!/usr/bin/perl
print "$_ ..\n" for reverse ( 1 .. 10 );
print "Blast Off!\n";
It is common to enclose longer lengths of code within a pre element
(which is also usually rendered in monospace) so that whitespace formatting is preserved and the
br tags are not necessary.
For example, the following markup shows how the code for a JavaScript function may be displayed in an XHTML document:
<pre><code>function createXHTMLElement ( eltname ) {
var element;
if ( document.createElementNS ) {
element = document.createElementNS( 'http://www.w3.org/1999/xhtml', eltname );
}
else {
element = document.createElement( eltname );
}
return element;
}</code></pre>
which renders as:
function createXHTMLElement ( eltname ) {
var element;
if ( document.createElementNS ) {
element = document.createElementNS( 'http://www.w3.org/1999/xhtml', eltname );
}
else {
element = document.createElement( eltname );
}
return element;
}
Note: You still have to be careful to escape any ampersand (&), less-than (<) and
occasionally greater-than (>) characters within the code in order for your document to be well formed
(see W3C Validation), unless you additionally enclose your code (inside the
code element) in a CDATA Section.
code is an inline element and so can be used, for example, within a paragraph to
mark up a code fragment.
For example:
<p>The method call <code>document.createElementNS( 'http://www.w3.org/1999/xhtml', 'p' )</code>
creates a new paragraph element node in the XHTML namespace.</p>
which renders as:
The method call document.createElementNS( 'http://www.w3.org/1999/xhtml', 'p' )
creates a new paragraph element node in the XHTML namespace.
See also the elements: samp, kbd and var.
There are no #REQUIRED attributes on the code element.
There are no specific attributes declared on the code element.
code element are listed below:class [ type NMTOKENS ]id [ type ID ]style [ type CDATA ], from the Style Attribute Module (deprecated)title [ type Text ]xmlns [ type URI - #FIXED 'http://www.w3.org/1999/xhtml' ]dir [ type Enumeration (ltr | rtl) ], from the Bi-directional Text Modulexml:lang [ type LanguageCode ]All attributes in the
Events Attribute Collection
are supported:onclick, ondblclick, onmousedown, onmouseup, onmouseover, onmousemove, onmouseout, onkeypress, onkeydown, onkeyup
code element is:
( #PCDATA | Inline.mix )*
See Content Model & Nesting for information about Content Model syntax and Nesting Groups.
codeabbr, acronym, br, cite, code, dfn, em, kbd, q, samp, span, strong, varabutton, input, label, select, textareaimgb, big, i, small, sub, sup, ttmapnoscript, scriptdel, insbdoobjectrubycodeabbr, acronym, address, cite, code, dfn, div, em, h1 - h6, kbd, p, pre, q, samp, span, strong, varadd, dt, licaption, td, thbutton, fieldset, label, legendb, big, i, small, sub, sup, ttdel, insbdoobjectrb, rt