HTML <code> Tag

Beginner
⏱️ 5 min read
📚 Updated: Jun 2026
🎯 4 Examples
Text & Semantics

What You’ll Learn

By the end of this tutorial, you’ll mark inline code snippets and build multiline blocks with semantic HTML.

01

Code Syntax

Wrap function names, variables, and tag names in <code>.

02

Inline vs Block

Use code alone for short snippets; pair with pre for multiline blocks.

03

Semantic Distinction

Tell code apart from kbd and samp.

04

Escape HTML

Display tag names safely with &lt; and &gt;.

05

Display Only

Understand that code shows text—it does not execute scripts.

06

CSS Styling

Style inline and block code for tutorials and documentation.

What Is the <code> Tag?

The code element (<code>) represents a short piece of programmatic text. Browsers usually display it in a monospace font.

💡
Display only — does not run code

<code> shows text on the page. To execute JavaScript, use a script element or an event handler outside the code tag.

Common uses include function and variable names in tutorials, HTML tag names in documentation, file paths, terminal commands, and API method references.

📝 Syntax

Wrap the code fragment between opening and closing <code> tags. Escape special HTML characters when showing markup:

syntax.html
<p>Use <code>console.log()</code> to debug.</p>

Syntax Rules

  • <code> is an inline element—it nests inside paragraphs and list items.
  • Use it for actual code fragments: functions, variables, tag names, commands.
  • For multiline blocks, nest code inside pre to preserve whitespace.
  • Self-closing syntax (<code />) is not valid in HTML.

⚡ Quick Reference

Use CaseCode SnippetResult
Function name<code>console.log()</code>Monospace inline snippet
HTML tag name<code>&lt;div&gt;</code>Escaped markup shown as text
Multiline block<pre><code>...</code></pre>Preserves spaces and line breaks
Variable referencelet <code>count</code> = 0;Semantic inline code
CSS property<code>display: flex</code>Style reference in prose
Tag-specific attrsNoneGlobal attributes only

⚖️ <code> vs <pre>, <kbd>, <samp>

Each element has a distinct semantic role for technical content:

ElementPurposeExample
<code>Source code fragmentconsole.log(), &lt;div&gt;
<pre>Preformatted text blockWraps code for multiline snippets
<kbd>Keyboard inputEnter, Ctrl+C
<samp>Program outputTerminal or console output text

🧰 Attributes

The <code> tag has no tag-specific attributes. Global attributes apply:

class Global

CSS hook for background, color, padding, and border styling.

class="inline-code"
id Global

Unique identifier for linking or scripting the code element.

id="snippet-1"
title Global

Tooltip with extra context for abbreviated snippets.

title="JavaScript method"
lang Global

Declare the language of a foreign-language code fragment.

lang="en"
Element type Inline

Phrasing content that flows inside paragraphs and lists.

<code>snippet</code>
Default style Browser

User-agent stylesheet applies monospace font-family by default.

font-family: monospace;

Style presentation with CSS classes rather than using code only for visual monospace decoration.

Examples Gallery

Inline snippets, function references, multiline pre blocks, and styled code patterns with copy-ready examples and live previews.

Live Preview

An inline function name marked with <code>:

Call document.querySelector() to select an element.

Basic Inline Code

The simplest use: mark a short code fragment inside a sentence.

basic-code.html
<p>
  Use the <code>&lt;code&gt;</code> tag to mark code in text.
</p>
Try It Yourself

📚 Common Use Cases

Use <code> in tutorials for function names, HTML tag names, CSS properties, file paths, terminal commands, and API method references.

Function Name in a Paragraph

Reference a JavaScript function inline while explaining what it does.

function-name.html
<p>
  Use <code>console.log()</code> to print a message in the browser console.
</p>
Try It Yourself

Multiline Block with pre and code

For longer snippets, nest <code> inside <pre> so indentation and line breaks are preserved.

pre-code-block.html
<pre><code>function greet(name) {
  return "Hello, " + name + "!";
}

console.log(greet("World"));</code></pre>
Try It Yourself

Styled code with CSS

Apply a CSS class for consistent background, color, and padding on inline code.

styled-code.html
<style>
  .inline-code {
    font-family: Consolas, monospace;
    background: #ecfdf5;
    color: #065f46;
    padding: 0.15rem 0.45rem;
    border-radius: 4px;
  }
</style>
<p>Set <code class="inline-code">type="button"</code> on every button.</p>
Try It Yourself

Styling <code> with CSS

Browsers render code in monospace by default. Customize inline and block presentation with CSS:

font-family Monospace stack
background Inline highlight
padding Chip-style snippets
pre + code Dark code blocks
code-styles.css
/* Inline code chip */
code {
  font-family: Consolas, monospace;
  background: #f1f5f9;
  padding: 0.15rem 0.4rem;
  border-radius: 4px;
}

.inline-code {
  background: #ecfdf5;
  color: #065f46;
  border: 1px solid #a7f3d0;
}

pre code {
  display: block;
  background: #0f172a;
  color: #e2e8f0;
  padding: 0.75rem;
}

Live styled inline code

♿ Accessibility

Semantic code markup helps readers and assistive technologies:

  • Use code for real code — not just to get monospace styling on random words.
  • Keep inline snippets short — long blocks belong in pre + code.
  • Escape HTML characters — use &lt; and &gt; when displaying tag names.
  • Provide context — explain what the snippet does in surrounding text.

🧠 How <code> Works

1

Author wraps a code fragment

Mark function names, variables, or markup in running text.

Markup
2

Browser applies monospace style

Default user-agent CSS distinguishes code from body text.

Rendering
3

Optional pre preserves layout

Multiline blocks use pre + code together.

Blocks
=

Clear, readable tutorials

Readers instantly see which words are code versus normal prose.

Universal Browser Support

The <code> element is supported in all browsers, including Internet Explorer.

Baseline · Since HTML 4

Code snippets that work everywhere

From legacy Internet Explorer to the latest mobile browsers — the code element is one of the most universally supported semantic text elements.

100% Core tag support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
<code> tag 100% supported

Bottom line: Ship documentation and tutorials with confidence. The <code> element is safe to use in every production environment today.

Conclusion

The <code> tag is a small but important HTML element for tutorials and documentation. Use it for short inline fragments, pair it with <pre> for multiline blocks, and style it with CSS for a polished reading experience.

Remember that code displays text only—it never executes scripts. Reserve it for real code fragments, not generic monospace decoration.

💡 Best Practices

✅ Do

  • Use code for function and variable names
  • Combine pre + code for multiline blocks
  • Escape < and > when showing HTML
  • Style with CSS classes for consistency

❌ Don’t

  • Expect <code> to execute JavaScript
  • Use code only for monospace decoration
  • Put huge files inside inline code
  • Confuse code with kbd or samp

Key Takeaways

Knowledge Unlocked

Six truths every developer should know about <code>

Bookmark these before you ship — they’ll make every tutorial clear and semantic.

6
Core concepts
02

Valid HTML5

A semantic element supported in every modern browser.

Essential
📝 03

pre + code Blocks

Pair with pre to preserve multiline formatting.

Reference
🚫 04

Display Only

Shows text on the page—does not run or execute code.

Distinction
🎨 05

Default Monospace

Browsers render code in monospace; override with CSS as needed.

Styling
06

kbd for Keys

Use kbd for keyboard input, not code.

Pattern

❓ Frequently Asked Questions

It marks a fragment of computer code such as a function name, variable, or HTML tag name inside normal text.
Use code alone for short inline snippets. Wrap multiline blocks in pre and code to preserve spacing and line breaks.
code is for source code. kbd is for keyboard keys the user presses, like Enter or Ctrl+C.
No. It only displays text. Use a script element to run JavaScript.
No. Reserve code for actual code. Use CSS font-family: monospace for non-code monospace text.

Practice Code Snippets

Try inline code and multiline pre blocks in the interactive HTML editor.

Try pre + code →

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.

6 people found this page helpful