HTML <code> Tag

Beginner
6 min read
Updated: Oct 2026
4 examples
Live preview

Definition and Usage

The <code> element marks a short fragment of computer code — a function name, filename, statement, or similar phrase. Browsers typically render it in a monospace font.

For several lines, wrap <code> in <pre> so spaces and line breaks stay intact. Prefer <kbd> for keyboard input and <samp> for program output when that meaning fits better.

Baseline · Widely available.

Not deprecated. Stable across browsers since about July 2015 (and much earlier in practice).

Note.

<code> alone does not add syntax highlighting — only semantic markup and default monospace styling.

HTML Syntax

syntax.html
<code> snippet</code>

Both start and end tags are required. Content is phrasing content. Escape &lt;, &gt;, and &amp; when those characters appear in the snippet.

Attributes

The <code> element has no element-specific attributes — only global attributes.

AttributeDescriptionNotes
None specificNo dedicated attributes on <code> itself.Use class (global) for language hints if a highlighter needs them.

Global Attributes

The <code> tag also supports the Global Attributes in HTML.

Live Preview

Choose a short snippet and see it marked with a real <code> in a sentence.

Inline <code> for a phrase; use <pre><code> for blocks.

Sentence

Call push() to add an item to the array.

<code>push()</code>

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Inline Code in a Sentence

Mark a function name inside a paragraph.

HTML
<p>
  The <code>push()</code> method adds one or more elements
  to the end of an array.
</p>
Try It Yourself

How It Works

  1. Write the sentence that mentions the code.
  2. Wrap only the snippet in <code>.
  3. Close the tag. Both start and end tags are required.
  4. The browser typically shows monospace text for that phrase.

Example 2 — Multi-Line Block with pre

Preserve line breaks and indentation by nesting <code> in <pre>.

HTML
<pre><code>function greet(name) {
  return "Hello, " + name;
}</code></pre>
Try It Yourself

How It Works

  1. Open <pre> so whitespace is kept.
  2. Put <code> inside to mark the text as computer code.
  3. Write the block with real newlines and spaces.
  4. Close both tags in order: </code> then </pre>.

Example 3 — Escape Angle Brackets

Show HTML tags as text by escaping < and >.

HTML
<p>
  Use <code>&lt;button&gt;</code> for actions,
  not fake links.
</p>
Try It Yourself

How It Works

  1. Decide what the reader should see (for example <button>).
  2. In the HTML file, write entities: &lt; and &gt;.
  3. Wrap that text in <code> so it is marked as a code phrase.
  4. The browser displays the angle brackets as characters, not as a real button element.

Example 4 — code vs kbd vs samp

Pick the element that matches meaning: code, keyboard input, or sample output.

HTML
<p>
  Run <code>npm start</code>,
  press <kbd>Enter</kbd>,
  then read <samp>Server ready</samp>.
</p>
Try It Yourself

How It Works

  1. <code> — the command text itself as computer code.
  2. <kbd> — the key the user presses.
  3. <samp> — what the program prints back.
  4. Using the right tag helps screen readers and keeps documentation clear.

Edge Cases & Pitfalls

Most code-tag bugs are missing escapes, wrong companions, or expecting highlighting for free.

blocks

Use pre for multi-line samples

<code> alone collapses like normal phrasing text for layout purposes. Nest it in <pre> to keep lines and indent.

escape

Escape <, >, and &

Unescaped angle brackets can break the page or create real elements. Prefer entities inside HTML source.

meaning

Not every monospace string is code

Keys → <kbd>. Output → <samp>. Variables in math/prose may fit <var>.

style

No built-in syntax colors

Add CSS or a highlighter if you need colors. A class like language-js is a common hook for tools.

Browser Support

The <code> element is core HTML phrasing content in every browser. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

<code>

Widely available since about July 2015 (and long before). Chrome, Firefox, Safari, Edge, and Opera all support the element with default monospace styling.

98% Global support (approx.)
Google Chrome All
Full support
Mozilla Firefox All
Full support
Apple Safari All
Full support
Microsoft Edge All
Full support
Opera All
Full support
Samsung Internet All
Full support
Internet Explorer All
Supported
<code> Widely supported

Bottom line: Safe to use everywhere for code phrases. Pair with pre for blocks, and escape special HTML characters in the source.

Key Takeaways

  • Role: <code> marks computer code; default look is monospace.
  • Blocks: use <pre><code>…</code></pre> for multi-line samples.
  • Escape: write &lt;, &gt;, and &amp; in HTML source when needed.
  • Siblings: kbd for keys, samp for output — not everything monospace is code.

One line: wrap the code phrase in <code>; for a whole listing, nest it in <pre>.

Default CSS Settings

Typical user-agent styling for code:

default.css
code {
  font-family: monospace;
}

You can restyle with CSS (background, padding, font stack). User font preferences may override your font-family choice.

Frequently Asked Questions

The code element marks a short fragment of computer code — a function name, filename, statement, or similar. Browsers typically show it in a monospace font.
Wrap <code> inside <pre>. Alone, code is phrasing content for a phrase or single line; pre preserves whitespace and line breaks for blocks.
Yes, in HTML source write &lt; and &gt; (and usually &amp; for &) so the browser does not treat them as markup.
<code> is computer code. <kbd> is keyboard (or other) input. <samp> is sample output from a program. Use the element that matches the meaning.
No. The element only marks the text as code. Highlighting comes from CSS or a separate highlighter library.
No. It is Baseline and widely available. Prefer it over presentational-only tags when you mean “this is code.”

Next: col

Define column properties in a table with the HTML col element.

col tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful