- Home
- /
- Tutorials
- /
- HTML Tutorial
- /
- HTML <code> Tag
HTML Tags
HTML <code> Tag
The code tag marks a piece of computer code inside your text, like a function name or a short command, and shows it in a monospace font. For a whole block of code, put it inside a pre tag.
It tells browsers, search engines and screen readers that this is code, not ordinary words. It does not add colors or syntax highlighting; that needs CSS or a JavaScript library.
Syntax
html
<code>your code here</code>It has no attributes of its own. Global attributes such as class work, and many highlighting libraries use a class like class="language-js" to know which language it is.
Example
html
<!DOCTYPE html>
<html>
<head>
<title>HTML Code Tag Example</title>
</head>
<body>
<h2>Using the <code> Tag</h2>
<p>To display a message in JavaScript, use:
<code>alert("Hello World!");</code>
</p>
</body>
</html>The command alert("Hello World!"); appears in a monospace font inside the normal sentence.
A block of code
For several lines, wrap <code> in the pre tag. pre keeps the line breaks and spaces, and code says what it is.
A multi-line code block
html
<pre><code>function greet(name) {
return "Hello, " + name;
}</code></pre>Showing HTML code
If the code you want to show contains HTML tags, the browser will treat them as real tags. Write < as < and > as >:
Showing a tag as text
html
<p>Use the <code><strong></code> tag for important text.</p>That displays "Use the <strong> tag…" with the tag visible, instead of making the rest of the sentence bold.
Browser Support
Chrome | Edge | Firefox | Safari | Opera | IE9+ |
|---|---|---|---|---|---|
| Yes | Yes | Yes | Yes | Yes | Yes |
code, kbd, samp or var?
- <code>
: code someone writes, likeconsole.log(). <kbd>: keys someone presses, like Ctrl + S. See the kbd tag.<samp>: what a program prints, like an error message.<var>: a variable name in maths or a formula.
HTML <code> Tag exercises
Mark inline code
Write this paragraph with npm install marked as code: Run npm install first.
Result
