Technical writing has its own vocabulary of elements, and most people learn one of them. The code element gets used for anything monospaced, which is understandable given they all render similarly, but the distinctions are meaningful and cost nothing to observe.
Code
The code element marks a fragment of computer code: a function name, a property, a snippet of a command.
<p>Use the <code>display</code> property to change layout behaviour.</p> It renders in a monospaced font by default and is inline, so it sits within a sentence. On its own it does not preserve whitespace, which is why a multi line sample needs help.
Code blocks
For a block of code, combine it with the preformatted element. The preformatted element preserves whitespace and line breaks exactly, and the code element supplies the meaning.
<pre><code>function total(items) { return items.length; }</code></pre> Note that the opening tags are adjacent with no line break between them. Because the preformatted element preserves whitespace, a newline there becomes a blank first line in the rendered output. The same applies at the end.
All angle brackets inside must be escaped as entities, or the browser will interpret them as markup and your example will render instead of displaying. Any documentation tool has a function to do this automatically, and doing it by hand is a reliable source of mistakes.
Keyboard input
The kbd element marks input a user is expected to type or press.
<p>Press <kbd>Ctrl</kbd> + <kbd>S</kbd> to save.</p> Marking each key separately makes it possible to style them as individual key caps, which is the presentation most people expect. It also distinguishes the keys from the plus sign joining them, which is instruction rather than input.
Sample output
The samp element marks output produced by a program: an error message, a returned value, terminal output.
<p>The build failed with <samp>module not found</samp>.</p> The distinction from code matters in tutorials, where a reader needs to tell at a glance which lines they should type and which the machine produced. Using the same element for both loses exactly the distinction they need.
Variables
The var element marks a variable, either in the programming sense or the mathematical one. It renders in italics by default.
<p>Replace <var>filename</var> with the name of your file.</p> This is the correct element for a placeholder in a command that the reader must substitute, which is otherwise usually indicated with angle brackets that then need escaping.
Other useful inline elements
Several more are worth knowing. The abbreviation element marks a shortened form, with the expansion in a title attribute, though as ever a title attribute is unreliable so the first mention should usually spell it out in the text as well.
Superscript and subscript handle mathematical notation, chemical formulae and ordinal indicators. Small marks side comments such as legal text, which is a meaning rather than a size. The s element marks content that is no longer accurate, such as a superseded price, which differs from the del element used for tracked deletions.
Copy buttons and usability
Any site presenting commands or snippets benefits from a control that copies the block to the clipboard, since selecting multiline preformatted text by hand is awkward on a phone and error prone anywhere. This is a button rather than a link, because it performs an action instead of navigating.
Two details are worth getting right. Confirm the copy visibly, since without feedback people click repeatedly and are unsure whether anything happened. And copy the raw text rather than whatever the highlighter produced, or line numbers and prompt characters end up pasted into a terminal where they will not run.
Syntax highlighting
None of these elements colour code by language. Highlighting is done by a script that parses the content and wraps individual tokens in spans, which are then coloured by CSS. Popular libraries do this on page load, and some build tools do it ahead of time, which is faster and avoids a flash of unhighlighted code.
Whichever route you take, the underlying markup should still be a preformatted element containing a code element. The highlighting sits on top of correct structure rather than replacing it, so the content remains meaningful if the script fails.
Long lines and overflow
Because preformatted content does not wrap by default, a long line produces horizontal overflow that can break a layout on a narrow screen. The usual fix is to allow the block to scroll horizontally, which keeps the code intact and contains the overflow. Forcing a wrap is an alternative, though it can make indentation misleading in a language where whitespace matters.