<code>
Example
<p>
Use the <code><div></code> element.
</p><p>
Use the <code><div></code> element.
</p>An unescaped <div> is parsed as a real tag, so the text disappears; < shows it as text.
Common mistakes
- Writing a raw tag like
<div>inside<code>, so it is parsed and disappears.
Key points
- Inline code uses
<code>, and block code uses<code>inside<pre>. - Special characters like
<and&inside<code>are escaped. - Keyboard input uses
<kbd>and program output uses<samp>, not<code>.
Interview questions
Does code keep spaces and line breaks?
No, <code> collapses whitespace like normal text. Wrap it in <pre> to keep spacing.
How do I set the language for syntax highlighting?
Add a class such as 'language-js' to <code>. Highlighting libraries read it; the browser itself does not highlight code.