Live Preview
An inline function name marked with <code>:
Call document.querySelector() to select an element.

By the end of this tutorial, you’ll mark inline code snippets and build multiline blocks with semantic HTML.
Wrap function names, variables, and tag names in <code>.
Use code alone for short snippets; pair with pre for multiline blocks.
Tell code apart from kbd and samp.
Display tag names safely with < and >.
Understand that code shows text—it does not execute scripts.
Style inline and block code for tutorials and documentation.
<code> Tag?The code element (<code>) represents a short piece of programmatic text. Browsers usually display it in a monospace font.
<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.
Wrap the code fragment between opening and closing <code> tags. Escape special HTML characters when showing markup:
<p>Use <code>console.log()</code> to debug.</p><code> is an inline element—it nests inside paragraphs and list items.code inside pre to preserve whitespace.<code />) is not valid in HTML.| Use Case | Code Snippet | Result |
|---|---|---|
| Function name | <code>console.log()</code> | Monospace inline snippet |
| HTML tag name | <code><div></code> | Escaped markup shown as text |
| Multiline block | <pre><code>...</code></pre> | Preserves spaces and line breaks |
| Variable reference | let <code>count</code> = 0; | Semantic inline code |
| CSS property | <code>display: flex</code> | Style reference in prose |
| Tag-specific attrs | None | Global attributes only |
<code> vs <pre>, <kbd>, <samp>Each element has a distinct semantic role for technical content:
| Element | Purpose | Example |
|---|---|---|
<code> | Source code fragment | console.log(), <div> |
<pre> | Preformatted text block | Wraps code for multiline snippets |
<kbd> | Keyboard input | Enter, Ctrl+C |
<samp> | Program output | Terminal or console output text |
The <code> tag has no tag-specific attributes. Global attributes apply:
class GlobalCSS hook for background, color, padding, and border styling.
class="inline-code"id GlobalUnique identifier for linking or scripting the code element.
id="snippet-1"title GlobalTooltip with extra context for abbreviated snippets.
title="JavaScript method"lang GlobalDeclare the language of a foreign-language code fragment.
lang="en"Element type InlinePhrasing content that flows inside paragraphs and lists.
<code>snippet</code>Default style BrowserUser-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.
Inline snippets, function references, multiline pre blocks, and styled code patterns with copy-ready examples and live previews.
An inline function name marked with <code>:
Call document.querySelector() to select an element.
The simplest use: mark a short code fragment inside a sentence.
<p>
Use the <code><code></code> tag to mark code in text.
</p>Use <code> in tutorials for function names, HTML tag names, CSS properties, file paths, terminal commands, and API method references.
Reference a JavaScript function inline while explaining what it does.
<p>
Use <code>console.log()</code> to print a message in the browser console.
</p>For longer snippets, nest <code> inside <pre> so indentation and line breaks are preserved.
<pre><code>function greet(name) {
return "Hello, " + name + "!";
}
console.log(greet("World"));</code></pre>Apply a CSS class for consistent background, color, and padding on inline code.
<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>Browsers render code in monospace by default. Customize inline and block presentation with CSS:
font-family Monospace stackbackground Inline highlightpadding Chip-style snippetspre + code Dark code blocks/* 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
Always set aria-label on icon-only buttons.
Semantic code markup helps readers and assistive technologies:
pre + code.< and > when displaying tag names.Mark function names, variables, or markup in running text.
Default user-agent CSS distinguishes code from body text.
Multiline blocks use pre + code together.
Readers instantly see which words are code versus normal prose.
The <code> element is supported in all browsers, including Internet Explorer.
From legacy Internet Explorer to the latest mobile browsers — the code element is one of the most universally supported semantic text elements.
Bottom line: Ship documentation and tutorials with confidence. The <code> element is safe to use in every production environment today.
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.
code for function and variable namespre + code for multiline blocks< and > when showing HTML<code> to execute JavaScriptcode only for monospace decorationcodecode with kbd or samp<code>Bookmark these before you ship — they’ll make every tutorial clear and semantic.
<code> marks function names, variables, and tag names in prose.
A semantic element supported in every modern browser.
EssentialPair with pre to preserve multiline formatting.
Shows text on the page—does not run or execute code.
DistinctionBrowsers render code in monospace; override with CSS as needed.
StylingUse kbd for keyboard input, not code.
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.script element to run JavaScript.code for actual code. Use CSS font-family: monospace for non-code monospace text.Try inline code and multiline pre blocks in the interactive HTML editor.
6 people found this page helpful