Description
HTML element <code>
is used to define a piece of computer code, which can be like HTML, CSS, Javascript, or any other programming language.
The content of the <code>
element is displayed as monospaced (fixed-width) font, such as Courier on most browsers.
In order to display the computer code properly with retaining all the spaces, the <pre>
element is usually used as a container for the <code>
element.
The below table summarizes its usage.
Usage Details | |
Placement | It is displayed as an Inline element. |
Contents | It can contain Inline elements and text. |
Tags | Both the opening and closing tags are required. |
Versions | HTML 2, 3.2, 4, 4.01, and 5 |
Syntax
Here is the basic syntax of the <code>
element.
<code>...</code>
Examples
In the below example, the <code>
element is used to refer to a computer code.
- For the first piece of code, only the
<code>
element is used. - For the second piece of code, the
<pre>
element is used as a container for the<code>
element.
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<h1>HTML Element - code</h1>
<p>A simple way to initialize a variable is <code>var x=1;</code>.</p>
<pre>
<code>
var x = 1;
for(x=1; x<10; x++){
console.log(x);
}
</code>
</pre>
</body>
</html>
Attributes
The following table shows the list of supported and unsupported attributes for the <code>
tag.
Attribute Type | Details |
Element-Specific Attributes | The tag <code> doesn't have any element-specific attributes. |
Global Attributes | Like all other HTML tags, the tag <code> supports the HTML Global Attributes. |
Event Attributes | The tag <code> also supports the HTML Event Attributes. |
Browser Compatibility
The tag <code>
is supported in all modern browsers.
- Google Chrome 1+
- Internet Explorer or Edge 2+
- Firefox 1+
- Apple Safari 1+
- Opera 4+