| Depr. | Empty | Version |
|---|---|---|
| No | No | HTML 2 |
| IE5.5+ | FF1+ | SA1.3+ | OP9.2+ | CH2+ |
|---|---|---|---|---|
| Full | Full | Full | Full | Full |
Syntax
Description
The
code element’s purpose is to identify computer code—for
example, a snippet of HTML or XML, or some other machine-readable code, be
that a server-side language such as PERL, or client-side JavaScript. Most
browsers will render code content in a fixed-width
font, such as Courier, but this style can be overridden using
CSS.
The code element is often used
in conjunction with the pre element
(denoting preformatted text), which is used to preserve in source markup
important whitespace and carriage returns that HTML and XHTML would
otherwise ignore.
The example shown above would display in the browser as depicted in Figure 1.
code
Note that because the example is one for which an HTML
snippet is rendered on screen—and is marked up with the
code element—the opening and closing
p tags are written as
<p> and </p>
respectively. This ensures that the browser displays them on screen,
rather than creating a new paragraph within the document that contains
them.
Example
A snippet of HTML on a
reference web site, in this case explaining how to use the
cite element, would be marked up like
this:
<p>To indicate a reference to a printed work, you'd use
the <code>cite</code> attribute as follows: <code><p>My
favorite book is <cite>The Two
Towers</cite></p></code>.</p>
Use This For …
This element is used for code, machine-readable processing instructions, HTML, XML, and markup generally.
Compatibility
| Internet Explorer | Firefox | Safari | Opera | Chrome | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 5.5 | 6.0 | 7.0 | 8.0 | 1.0 | 1.5 | 2.0 | 3.0 | 3.5 | 1.3 | 2.0 | 3.1 | 4.0 | 9.2 | 9.5 | 10.0 | 2.0 |
| Full | Full | Full | Full | Full | Full | Full | Full | Full | Full | Full | Full | Full | Full | Full | Full | Full |
The
code element has good browser support. All the major
browsers render it in a fixed-width display, usually
Courier.