The <code>, <var>, <samp> & <kbd> elements
HTML has four inline tags for technical text. <code> marks a fragment of computer code, <kbd> marks keys or text the user types, <samp> marks output from a program, and <var> marks a variable in code or maths. Browsers show the first three in a monospace font and <var> in italics.
Last updated: October 3, 2026
Quick facts
| Category | Flow content, phrasing content, palpable content |
| Content model | Phrasing content |
| Tag omission | Neither the start nor the end tag can be omitted |
| Permitted parents | Any element that accepts phrasing content |
| DOM interface | HTMLElement (all four) |
Syntax
Live example
Edit the code below and watch the preview update instantly.
Attributes
| Attribute | Description |
|---|---|
| — | All four accept only the global attributes. |
Common examples
Code inside a sentence
Wrap function names, file names and short snippets in so they stand out from the surrounding text.
A multi-line code block
Put <code> inside <pre> to keep the line breaks and indentation. Add a language- class so highlighting libraries know which language it is.
Keyboard shortcuts and program output
Use <kbd> for keys the reader presses and <samp> for what the computer prints back.
Variables in a formula
Use <var> for variable names in maths or in instructions.
<code> vs <kbd> vs <samp>
All three usually appear in a monospace font, but they mean different things. <code> is something written in a programming or markup language. <kbd> is something the user types or presses. <samp> is what a program shows as output, such as an error message.
A simple way to remember it: <kbd> goes into the computer, <samp> comes out of it, and <code> is the program in between. Choosing the right one helps screen readers and search engines understand technical writing.
| <code> | <kbd> | <samp> | |
|---|---|---|---|
| Means | Computer code | User input | Program output |
| Example | getElementById() | Ctrl+C | 404 Not Found |
Common mistakes to avoid
- Using
<code>just to get a monospace font. If the text isn't code, style a <span> with CSS instead. - Forgetting to escape
<and&. Writing<p>inside<code>creates a real paragraph; write<p>to show the tag. - Multi-line code without
<pre>.<code>alone collapses line breaks and spaces into a single line. - Using
<code>for keys. Keyboard shortcuts belong in<kbd>.
Accessibility & SEO tips
- Make sure code text has enough color contrast — light gray code on a white background is a common readability problem.
- Long code lines should scroll inside their box (
overflow-x: autoon the<pre>) instead of making the whole page scroll sideways on phones. - Search engines index code as normal text, so tutorials with real, copyable code examples can rank for programming questions.
- For short inline code inside a sentence, keep it brief — a long run of monospace text inside a paragraph is hard to read on small screens; move longer snippets into their own
<pre>block.
Browser support
Supported in every browser, including Chrome, Edge, Firefox, Safari and their mobile versions — it has been part of HTML since the very first versions.