<code>

Term side of the Card.
Topic: Formatting
Unit title: <code>
Categories: Tag
Explanation side of the Card

Explanation: The element that is used to display a fragment of computer code or other text that should be presented in a monospaced font. It's often used within technical documentation, tutorials or any context where code needs to be highlighted or shown differently from regular text.

Browser compatibility:
  • The chrome browser supports this feature starting from version 1.
  • The edge browser supports this feature starting from version 12.
  • The firefox browser supports this feature starting from version 1.
  • The safari browser supports this feature starting from version 4.
Availability: 

Example

Wronghtml
<p>
  Use the <code><div></code> element.
</p>
Righthtml
<p>
  Use the <code>&lt;div&gt;</code> element.
</p>

An unescaped <div> is parsed as a real tag, so the text disappears; &lt; 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.

Analytics cookiesWe'd like to use analytics cookies to see which pages are useful. They're set only if you accept. Privacy Policy