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 < and > 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.