HTML Computercode
HTML has dedicated elements for code, output, variables, and keyboard input. They give meaning and styling hooks beyond plain text.
Code-related HTML elements
EXAMPLE
<!-- Inline code -->
<p>Run <code>npm install</code> to fetch dependencies.</p>
<!-- Block of code - <pre> preserves whitespace, <code> marks it as code -->
<pre><code>function add(a, b) {
return a + b;
}
</code></pre>
<!-- Keyboard input -->
<p>Press <kbd>Ctrl</kbd> + <kbd>C</kbd> to stop the server.</p>
<!-- Sample output (what the program prints) -->
<p>The script outputs: <samp>hello, world</samp></p>
<!-- A variable name in mathematical or technical text -->
<p>Solve for <var>x</var> in <var>x</var><sup>2</sup> = 16.</p>
<!-- Long code block with syntax highlighting (via a JS library like Prism) -->
<pre><code class='language-javascript'>
const greet = (name) => \`hello, ${name}\`;
console.log(greet('world'));
</code></pre>
<style>
code, kbd, samp, var, pre {
font-family: 'JetBrains Mono', ui-monospace, monospace;
font-size: 0.95em;
}
code { background: #f1f5f9; padding: 0.1em 0.3em; border-radius: 3px; }
kbd { background: #1f2937; color: white; padding: 0.1em 0.4em; border-radius: 4px; }
samp { color: #047857; }
var { font-style: italic; color: #b91c1c; }
pre { padding: 1rem; background: #0f172a; color: #e2e8f0; border-radius: 6px; overflow-x: auto; }
pre code { background: transparent; padding: 0; color: inherit; }
</style>
Why it matters
Use the right element for the right thing. is for code, is for keys the user presses, is for program output, is for variables in prose. Screen readers read them differently, and CSS targets them by element so styling is clean.
Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.
Example
Example
<!DOCTYPE html>
<html>
<head>
<title>HTML Computercode</title>
</head>
<body>
<h1>HTML Computercode</h1>
<p>This is a demo page for the "HTML Computercode" lesson.</p>
</body>
</html>
Try it Yourself »
Exercise
Wrap a snippet of code in the element designed for it.
<p>Run <
>npm install</
> first.</p>
Four letters. Inline code element.
Discussion
Loading…