HTML Computer Code Elements
HTML provides several tags for displaying computer code and technical output: <code> for inline code snippets, <pre> for preformatted text that preserves whitespace, <kbd> for keyboard input, and <samp> for sample program output.
Combining <pre> and <code> together is the standard way to display multi-line code blocks while preserving indentation and line breaks exactly as written.
<pre><code>your code here</code></pre>Code and pre
<code> styles text as monospace inline. <pre> preserves whitespace and line breaks exactly as typed, useful for code blocks or ASCII art.
Keyboard and sample output
<kbd> represents keyboard input like Ctrl+C. <samp> represents sample output from a program, often used in documentation and tutorials.
<pre><code>function greet() {
console.log("Hi");
}</code></pre>function greet() {
console.log("Hi");
}pre preserves the indentation and line breaks of the code exactly.
<p>Press <kbd>Ctrl</kbd> + <kbd>C</kbd> to copy.</p>Press Ctrl + C to copy.kbd visually represents keys the user should press.
Key points
- <code> displays inline code in monospace font.
- <pre> preserves whitespace and line breaks exactly.
- <kbd> represents keyboard input.
- <samp> represents sample program output.
