What is the HTML <code> element?

What is the <code> element?

In HTMLHypertext Markup Language, the <code> element is used to show content as a fragment of computer code.

By default, monospace font style is used to display the text/piece of code in between the opening <code> and closing </code> tags.

<code> supports almost every latest browser or browser engine.


This element is still available in the latest version of HTML, but you can also use CSSCascading Style Sheets.

Syntax


<code> .....Content Here.....</code>

Attributes

The code element supports global attributes as well as event attributes. Some useful properties are mentioned below.

Useful attributes

Special attributes

Description

<samp>

Quoted or Sample output from a computer program.

<pre>

Preformatted text means same output as it is in Html document.

<var>

Define a variable name e.g. for mathematical expression.

<kbd>

Keyboard Input means a span of inline text.

<command>

Not Available (Discontinued)


<code> Element Supported Browsers

Code

Example 1

The code snippet below shows how to use the <code> element in HTML documents.

Explanation

The first line between the <p> tags will get green after execution. Therefore, its font style will also change.

We use a <code> element in line 10.

Example 2

Explanation

The code snippet represents how the <code> element can be used to show code in HTML as it is.

Free Resources