Skip to the page
html reference · text

HTML <code> / <pre> tags

closing tag
required (both)
void element
no
lives inside
inline (<code>) or block (<pre>)
typically holds
source code

Where it goes

<code> on its own for a word of code mid-sentence. <pre><code>...</code></pre> for a whole block that must keep its formatting.

Example

Use the <code>let</code> keyword.

<pre><code>let x = 5;
console.log(x);</code></pre>

Open this example in the editor →

Common mistakes

  • Using <pre> alone for a block of code. The formatting survives, but nothing says "this is code."
  • Typing a raw < or > inside the code. The browser reads it as a tag. Write &lt; and &gt; instead, even inside <pre>.
  • Indenting the inside of <pre> to match your other HTML. Every one of those spaces shows on the page. Start the content at the left edge.

Related

Updated 24 October 2026. Practice it in the html route, or read more on MDN.