HTML: the code element

The <code> element marks a short fragment of computer code or a code-related name in text.

What you will learn

Inline code

<p>Run <code>npm install</code> in the project folder.</p>

Use <code> when code appears within a sentence. It gives the fragment a programmatic meaning; CSS can control its font and color.

Multi-line code

<pre><code class="language-javascript">function greet(name) {
  return `Hello, ${name}`;
}</code></pre>

<pre> preserves whitespace and line breaks. Combining it with <code> communicates that the preserved text is source code. A syntax-highlighting class can help visual readers, but it should not be the only way to understand the example.

Related text elements

Common mistakes