HTML Code Text: code, pre, kbd, samp and var
Mark code and preserve spacing
Use code for a fragment of computer code. Use pre for text whose spacing and line breaks carry meaning. Combine them for multiline code samples. A code element displays code; it does not execute it.
<p>Use <code>print()</code> to display a value.</p><pre><code>first = 2second = 3print(first + second)</code></pre>The sample preserves its lines and is displayed as text.
Represent input and output
Use kbd for user input (such as a key or command), samp for sample output from a system and var for a variable in a formula or program description. They communicate different roles even if fonts look alike.
<p> Press <kbd>Enter</kbd> .</p><p> Output: <samp>Ready</samp></p><p> Area = <var>width</var> × <var>height</var></p>Input, output and variables have distinct markup.
Display HTML without parsing it
Putting HTML tags inside code or pre does not stop the browser from treating them as markup. To display a literal opening angle bracket in ordinary HTML text, write <; use > for the closing angle bracket. The next lesson explains character references.
<pre><code><p>Hello</p></code></pre>The literal paragraph markup is visible rather than becoming a paragraph element.
Practice and next steps
Show a two-line code example, a keyboard command and its sample result. Make sure the code remains visible text.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Code and keyboard text — Knowledge Check
Apply the concepts from Code and keyboard text.
Which element preserves ordinary source line breaks by default?
Which element marks sample output?
What happens if you put a raw p tag inside code?
Which element marks the Enter key in an instruction?
Which markup identifies width as a variable in a formula?