HTML <code> Tag

The HTML <code> tag represents a fragment of computer code.

The code fragment could be an XML element name, a filename, a computer program, or any other string that a computer would recognize.

Browsers usually display <code> content in a monospace font (also called a fixed-width or non-proportional font) such as Courier (unless style sheets have been used to specify a different font).

Syntax

The <code> tag is written as <code></code> with the code fragment inserted between the start and end tags.

Like this:

Copy to Clipboard
<code>Computer code here...</code>

Examples

Basic tag usage

You can insert short fragments of code within a paragraph or any other text.

CodeResult

The ColdFusion DateFormat() function formats the date passed in as a parameter.

Displaying HTML Code

A common usage of the <code> is to display HTML code within a web page. To display HTML code, you need to use the correct HTML entities to ensure the HTML code is actually displayed (and not rendered) by the browser.

Specifically, you need to use &lt; in place of the less-than symbol (<) and &gt; in place of the greater-than symbol (>).

Like this:

CodeResult

To display a blockquote, use the <blockquote> element.

Multiple Lines

Multiple lines of code can be marked up by surrounding the <code> tags with <pre> tags.

Without the <pre> Tag

Here's an example of what happens if you don't include the <pre> tag when working with multiple lines.

CodeResult

Here's the basic code for creating a table in HTML:

<table> <tr> <td></td> <td></td> </tr> </table>

With the <pre> Tag

And here it is after adding the <pre> tag.

CodeResult

Here's the basic code for creating a table in HTML:


<table>
<tr>
<td></td>
<td></td>
</tr>
</table>

Specifying the Computer Language

There is no formal way to specify the langauge of the computer code contained within the <code> tags. The HTML specification recommends specifying the language using the class attribute. For example, by using a prefix such as language- to the class name.

In this example we use class="language-javascript" to specify the programming language used.

CodeResult

You could always use the alert() function to alert the user.

Differences Between HTML 4 & HTML 5

None.

To see more detail on the two versions see HTML5 <code> Tag and HTML4 <code> Tag. Also check out the links to the official specifications below.

Template

Here's a template for the <code> tag with all available attributes for the tag (based on HTML5). These are grouped into attribute types, each type separated by a space. In many cases, you will probably only need one or two (if any) attributes. Simply remove the attributes you don't need.

Note that the <code> element does not actually have any local attributes (i.e. attributes that are specific to the element), but the following global attributes and event handlers are available to the element (and all other HTML elements).

For more information on attributes for this tag, see HTML5 <code> Tag and HTML4 <code> Tag.

Copy to Clipboard
<code 
 accesskey=""
 class=""
 contenteditable=""
 contextmenu=""
 dir=""
 draggable=""
 dropzone=""
 hidden=""
 id=""
 itemid=""
 itemprop=""
 itemref=""
 itemscope=""
 itemtype=""
 lang=""
 spellcheck=""
 style=""
 tabindex=""
 title=""
 translate=""

 onabort=""
 onautocomplete=""
 onautocompleteerror=""
 onblur=""
 oncancel=""
 oncanplay=""
 oncanplaythrough=""
 onchange=""
 onclick=""
 onclose=""
 oncontextmenu=""
 oncuechange=""
 ondblclick=""
 ondrag=""
 ondragend=""
 ondragenter=""
 ondragexit=""
 ondragleave=""
 ondragover=""
 ondragstart=""
 ondrop=""
 ondurationchange=""
 onemptied=""
 onended=""
 onerror=""
 onfocus=""
 oninput=""
 oninvalid=""
 onkeydown=""
 onkeypress=""
 onkeyup=""
 onload=""
 onloadeddata=""
 onloadedmetadata=""
 onloadstart=""
 onmousedown=""
 onmouseenter=""
 onmouseleave=""
 onmousemove=""
 onmouseout=""
 onmouseover=""
 onmouseup=""
 onmousewheel=""
 onpause=""
 onplay=""
 onplaying=""
 onprogress=""
 onratechange=""
 onreset=""
 onresize=""
 onscroll=""
 onseeked=""
 onseeking=""
 onselect=""
 onshow=""
 onsort=""
 onstalled=""
 onsubmit=""
 onsuspend=""
 ontimeupdate=""
 ontoggle=""
 onvolumechange=""
 onwaiting=""
	>
</code>

Tag Details

For more details about the <code> tag, see HTML5 <code> Tag and HTML4 <code> Tag.

Specifications

Here are the official specifications for the <code> element.

What's the Difference?

W3C creates "snapshot" specifications that don't change once defined. So the HTML5 specification won't change once it becomes an official recommendation. WHATWG on the other hand, develops a "living standard" that is updated on a regular basis. In general, you will probably find that the HTML living standard will be more closely aligned to the current W3C draft than to the HTML5 specification.

Enjoy this page?

Sponsored Link: Design CODE-FREE Websites

Check a Domain:

ZappyHost Logo
Webydo Banner Ad