Skip to: content, navigation

by Ian Lloyd

code (HTML element)

Spec
Depr. Empty Version
No No HTML 2
Browser support full matrix
IE5.5+ FF1+ Saf1.3+ Op9.2+
Full Full Full Full
<code>
</code>

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>&lt;p&gt;My
    favorite book is &lt;cite&gt;The Two
    Towers&lt;/cite&gt;&lt;/p&gt;</code>.</p>
Type
inline element
Contains
inline elements only
Contained by
block-level elements, inline elements

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.

Figure 1. HTML markup displayed using code Example of code element

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 &lt;p&gt; and &lt;/p&gt; respectively. This ensures that the browser displays them on screen, rather than creating a new paragraph within the document that contains them.

Use This For …

This element is used for code, machine-readable processing instructions, HTML, XML, and markup generally.

Compatibility

IE5.5Full
6.0Full
7.0Full
Firefox1.0Full
1.5Full
2.0Full
Safari1.3Full
2.0Full
3.0Full
Opera9.2Full
9.5Full

The code element has good browser support. All the major browsers render it in a fixed-width display, usually Courier.

Related Reading

User-contributed notes

There are no comments yet.

Add a note

To post a note on this topic, please log in with your SitePoint username and password. If you don't have an account yet, you can create a new account for free.

Related Products

The Principles of Beautiful Web Design

Best Seller!

You don’t need to go to Art School to design great looking web sites!

Book Cover: The Principles of Beautiful Web Design

Download the FREE sample chapters