Code & Keyboard Input
Learn to display code snippets, keyboard shortcuts, and computer output with semantic HTML elements.
How do you display code in HTML?
Wrap inline code in <code> and multi-line code in <pre><code>…</code></pre>, which keeps the whitespace and line breaks. Use <kbd> for keys the user should press, <samp> for program output and <var> for variables. Escape < as < inside code blocks.
<p>Use the <code>display</code> property.</p>
<pre><code>function greet() {
return "hi";
}</code></pre>
<p>Save with <kbd>Ctrl</kbd> + <kbd>S</kbd>.</p>
<p>Output: <samp>File saved</samp></p>Try It: Code Elements
Experiment with different code-related HTML elements. See how they display code, keyboard shortcuts, and computer output.
Code Editor
<p>Use the <code>display</code> property.</p>
<pre><code>function greet(name) {
return `Hello, ${name}`;
}</code></pre>
<p>Save with <kbd>Ctrl</kbd> + <kbd>S</kbd>.</p>
<p>The program printed: <samp>File saved.</samp></p>
<p>Area = <var>width</var> × <var>height</var></p>Preview
The <code> Element
The <code> element is used to display inline code snippets, function names, and short programming constructs.
Inline Code Examples:
HTML:
<p>Use the <code>document.getElementById()</code> method.</p>Result:
Use the document.getElementById() method.
When to Use <code>:
- Function names:
console.log(),getElementById() - HTML tags:
<div>,<span> - CSS properties:
color,font-size - File names:
index.html,style.css
The <pre> Element
The <pre> element preserves both spaces and line breaks, making it perfect for displaying code blocks and formatted text.
Code Block Example:
HTML:
<pre><code>function calculateArea(radius) {
const pi = 3.14159;
return pi * radius * radius;
}
console.log(calculateArea(5));</code></pre>Result:
function calculateArea(radius) {
const pi = 3.14159;
return pi * radius * radius;
}
console.log(calculateArea(5));Best for:
- • Multi-line code blocks
- • ASCII art
- • Formatted data
- • Configuration files
Remember:
- • Preserves whitespace
- • Uses monospace font
- • Often combined with <code>
- • May need overflow handling
The <kbd> Element
The <kbd> element represents keyboard input, shortcuts, and keys that users should press.
Keyboard Shortcut Examples:
Single keys:
Press Enter to submit
Press Esc to cancel
Key combinations:
Ctrl + C to copy
Alt + Tab to switch
HTML Examples:
<p>Press <kbd>Ctrl</kbd> + <kbd>S</kbd> to save.</p>
<p>Use <kbd>F12</kbd> to open developer tools.</p>
<p>Press <kbd>Shift</kbd> + <kbd>Ctrl</kbd> + <kbd>I</kbd> for inspector.</p>Additional Elements
<samp> Element
Represents sample output from a computer program or system.
Example:
<samp>File not found</samp><var> Element
Represents variables in programming or mathematical expressions.
Example:
<var>x</var> = 5x = 5
Best Practices
Do These:
- Use <code> for inline code snippets
- Combine <pre> and <code> for code blocks
- Use <kbd> for all keyboard references
- Style elements with CSS, not inline styles
Avoid These:
- ✗Using <b> or <i> for code styling
- ✗Forgetting to escape HTML in code examples
- ✗Using quotes around keyboard shortcuts
- ✗Overusing <pre> for non-code content
Code and Keyboard Elements — step by step
Use code for inline code
The code element represents a fragment of computer code inline within text. It renders in a monospace font by default, visually distinguishing it from prose.
🎯 Quick Check
Which HTML element should you use to display multi-line code while preserving whitespace and indentation?
HTML Code and Keyboard Elements: summary
Key points
<pre>preserves whitespace;<code>marks the content as code. Use both for code blocks.- Add a class like
language-jsto<code>for syntax highlighters such as Prism or Shiki. - Long lines in
<pre>should scroll horizontally withoverflow-x: autorather than overflow the page.
Frequently asked questions
›How do I show HTML tags as text?
Replace < with < and > with > so the browser displays them instead of parsing them.