HTML code Tag
Learn how to use the HTML <code> tag to semantically mark up fragments of computer code within your web content.
Introduction to the HTML code Tag
The HTML<code>
tag is used to define a fragment of computer code. By default, browsers typically display content within <code>
tags in a monospace font, such as Courier or Consolas.
This tag is intended for short, inline code snippets or for marking up code within a larger block of preformatted text (often in conjunction with the <pre>
tag).
Basic Syntax
Inline Code:html
<p>In JavaScript, the <code>console.log()</code> function is used to print output to the console.</p>
<p>To declare a variable, you can use <code>let myVar = 10;</code>.</p>
Block of Code (using
<pre>
for preformatted text):
For displaying multi-line blocks of code while preserving whitespace (like line breaks and indentation), the <code>
tag is often wrapped by a <pre>
(preformatted text) tag.
html
<pre>
<code>
function greet(name) {
return "Hello, " + name + "!";
}
console.log(greet("World"));
</code>
</pre>
Key Characteristics
- Semantic Meaning: It indicates that the enclosed text is a piece of computer code. - Inline Element: By itself,<code>
is an inline element. It doesn't start on a new line unless it's the only content within a block-level element or inside a <pre>
tag.
- Default Styling: Typically rendered in a monospace font. This can be customized with CSS.
<code>
vs. <pre>
- <code>
: Marks text as computer code. It does not preserve whitespace on its own (multiple spaces collapse, line breaks are not rendered as such unless inside <pre>
).
- <pre>
: Defines preformatted text. Text within <pre>
is displayed in a fixed-width font (usually monospace) and preserves both spaces and line breaks. It's ideal for displaying blocks of code where indentation and line structure are important.
Using them together (<pre><code>...</code></pre>
) is a common and semantically correct pattern for blocks of code. The <pre>
handles the whitespace and block formatting, while the <code>
indicates the content is code.
<code>
vs. <samp>
vs. <kbd>
vs. <var>
HTML provides several tags for different types of computer-related text:
- <code>
: For a fragment of computer code.
- <samp>
(Sample Output): For sample output from a computer program or script.
`html
If you run the script, you will see: Process completed successfully.
`
- <kbd>
(Keyboard Input): For text that represents user keyboard input.
`html
To save the file, press Ctrl + S.
`
- <var>
(Variable): For a variable in a mathematical expression or programming context.
`html
In the equation E = mc2, c is the speed of light.
`
Using the most specific semantic tag is always preferred.
Styling with CSS
While the default monospace font is often suitable, you can style<code>
elements further:
css
code {
font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace;
background-color: #f4f4f4;
padding: 2px 4px;
border-radius: 3px;
font-size: 0.9em;
}
pre code {
display: block; /* Makes the code block take full width within pre */
padding: 1em;
overflow-x: auto; /* Add horizontal scroll for long lines */
}
/* For syntax highlighting, you'd typically use a JavaScript library
that adds classes to spans within the code block, which you then style. */
.token.comment {
color: slategray;
}
.token.keyword {
color: #0077cc;
}
For syntax highlighting, JavaScript libraries like Prism.js or highlight.js are commonly used.
Conclusion
The<code>
tag is the correct semantic element for marking up computer code snippets. For inline code, use it directly. For multi-line code blocks, wrap it with a <pre>
tag to preserve formatting. Distinguish its use from related tags like <samp>
, <kbd>
, and <var>
for optimal semantic clarity.