HTML: <code> tag
This HTML tutorial explains how to use the HTML element called the <code> tag with syntax and examples.
Description
The HTML <code> tag defines a fragment of computer code in the HTML document. Browsers traditionally render the text found within the <code> tag in monospace font. This tag is also commonly referred to as the <code> element.
Syntax
In HTML, the syntax for the <code> tag is:
<body>
<code>Computer code goes here</code>
</body>
Sample Output
Attributes
Only the Global Attributes apply to the <code> tag. There are no attributes that are specific to the <code> tag.
Note
- The HTML <code> element is found within the <body> tag.
- The text within the <code> tag is displayed in the browser's default monospace font.
Browser Compatibility
The <code> tag has basic support with the following browsers:
- Chrome
- Android
- Firefox (Gecko)
- Firefox Mobile (Gecko)
- Internet Explorer (IE)
- Edge Mobile
- Opera
- Opera Mobile
- Safari (WebKit)
- Safari Mobile
Example
We will discuss the <code> tag below, exploring examples of how to use the <code> tag in HTML5, HTML 4.01 Transitional, XHTML 1.0 Transitional, XHTML 1.0 Strict, and XHTML 1.1.
HTML5 Document
If you created a new web page in HTML5, your <code> tag might look like this:
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>HTML5 Example by www.techonthenet.com</title>
</head>
<body>
<code>Dim LRow As Integer</code>
</body>
</html>
In this HTML5 Document example, we have created a <code> tag that contains the text "Dim LRow As Integer" which would display in the browser's default monospace font.
HTML 4.01 Transitional Document
If you created a new web page in HTML 4.01 Transitional, your <code> tag might look like this:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>HTML 4.01 Transitional Example by www.techonthenet.com</title>
</head>
<body>
<code>Dim LRow As Integer</code>
</body>
</html>
In this HTML 4.01 Transitional Document example, we have created a <code> tag that contains the text "Dim LRow As Integer" which would display in the browser's default monospace font.
XHTML 1.0 Transitional Document
If you created a new web page in XHTML 1.0 Transitional, your <code> tag might look like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>XHMTL 1.0 Transitional Example by www.techonthenet.com</title>
</head>
<body>
<code>Dim LRow As Integer</code>
</body>
</html>
In this XHTML 1.0 Transitional Document example, we have created a <code> tag that contains the text "Dim LRow As Integer" which would display in the browser's default monospace font.
XHTML 1.0 Strict Document
You can not use the <code> tag in XHTML 1.0 Strict.
XHTML 1.1 Document
You can not use the <code> tag in XHTML 1.1.