Code and Technical Text¶
HTML has a small set of elements for text that is technical: code, keyboard input, program output, and variables. They are useful in tutorials, documentation, and any page that explains how something works.
Inline code with <code>¶
Use <code> for a short piece of code inside a sentence:
Browsers usually show it in a monospace font. The important part is that the markup says "this is code".
Code blocks with <pre> and <code>¶
For more than one line, put <code> inside <pre>:
<pre> preserves spaces and line breaks exactly as you type them. <code> says the content is code. Use both together for code blocks.
Showing HTML as text¶
The browser treats HTML tags as markup. If you want to show a tag to the reader, escape the angle brackets:
That displays as:
Use < for < and > for >. You will need this whenever you write about HTML itself.
Keyboard input with <kbd>¶
<kbd> marks a key or key combination:
It is useful in instructions. Screen readers can identify it as keyboard input rather than ordinary text.
Program output with <samp>¶
<samp> marks sample output from a program:
Use it when the text is something the computer produced.
Variables with <var>¶
<var> marks a variable or placeholder in a mathematical expression or program:
Reference¶
| Element | Use | Example |
|---|---|---|
<code> |
Inline code | <code>print()</code> |
<pre> |
Preserve whitespace | A code block or ASCII diagram |
<kbd> |
Keyboard input | <kbd>Ctrl</kbd> |
<samp> |
Program output | <samp>Build complete</samp> |
<var> |
Variable | <var>total</var> |
Exercise: mark up a recipe tool¶
Create a small page that explains a command-line recipe tool. Include:
- a sentence with inline code for the command name,
- a code block with two commands,
- a sentence describing a keyboard shortcut,
- a sample output line,
- one variable in a sentence about the recipe name.
Check that the code block keeps its indentation and that the angle brackets in any HTML example appear as text rather than disappearing.
Next: Small Semantic Tags