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.
Syntax
HTML Syntax
syntax.html
<code>snippet</code>
Both start and end tags are required. Content is phrasing content. Escape <, >, and & when those characters appear in the snippet.
Reference
Attributes
The <code> element has no element-specific attributes — only global attributes.
Attribute
Description
Notes
None specific
No dedicated attributes on <code> itself.
Use class (global) for language hints if a highlighter needs them.
Run npm start, press Enter, then read Server ready.
How It Works
<code> — the command text itself as computer code.
<kbd> — the key the user presses.
<samp> — what the program prints back.
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.
Compatibility
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 ChromeAll
Full support
Mozilla FirefoxAll
Full support
Apple SafariAll
Full support
Microsoft EdgeAll
Full support
OperaAll
Full support
Samsung InternetAll
Full support
Internet ExplorerAll
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.
Remember
Key Takeaways
Role:<code> marks computer code; default look is monospace.
Blocks: use <pre><code>…</code></pre> for multi-line samples.
Escape: write <, >, and & 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>.
UA style
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 < and > (and usually & 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.”