Lesson 15 of 51 · Module 2, lesson 9 of 9 · 3 min
Showing code on a page
Display code, keyboard keys and program output, and keep spaces exactly as typed.
What you'll learn
- How to show code inside a sentence with
<code>- How to show a block of code with its spaces kept, using
<pre>- How to show keyboard keys with
<kbd>and program output with<samp>
Why this matters
As a developer, you will write about code: in notes, in your portfolio, in help pages. HTML has elements that tell the browser "this is code", so that it is shown in a monospace font, where every letter has the same width.
Code inside a sentence: <code>
<p>Every page starts with <code><!DOCTYPE html></code>.</p>
Notice < and >: inside <code>, a < still starts a tag, so you still need character references.
Keyboard keys: <kbd>
<p>Save your file with <kbd>Ctrl</kbd> + <kbd>S</kbd>.</p>
Program output: <samp>
<p>If the file is missing, the browser shows <samp>This page isn't available</samp>.</p>
Blocks of code: <pre>
Remember the white-space rule: the browser collapses spaces and line breaks. <pre> (preformatted text) switches that rule off. Every space and every line break inside it is kept.
For a block of code, put <code> inside <pre>:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My notes</title>
</head>
<body>
<h1>My HTML notes</h1>
<p>A paragraph with important words:</p>
<pre><code><p>
This is <strong>important</strong>.
</p></code></pre>
<p>To save, press <kbd>Ctrl</kbd> + <kbd>S</kbd>.</p>
</body>
</html>
What you'll see: the code block in a monospace font, with its line breaks and indentation kept, and the keys in monospace too.
<pre> also works for other text where spacing matters, such as a small text drawing. Long lines inside <pre> do not wrap, so on a phone the reader may need to scroll sideways. Keep lines short.
Try it yourself
- Select the notes example and choose Try it.
- Change
<pre>and</pre>to<p>and</p>. The line breaks disappear. Change them back. - Add a line that shows the shortcut for View Source, with each key in
<kbd>.
Common mistakes
- Writing
<inside<code>. The browser still reads it as a tag, and your code example disappears. Fix: write<. - Indenting the first line of
<pre>to match your code. Every space inside<pre>shows on the page. Fix: start the content right after<pre><code>. - Using
<pre>for normal text to keep line breaks. It changes the font and does not wrap on small screens. Fix: use paragraphs, and<br>where the line break is part of the content.
Check yourself
- Which element keeps spaces and line breaks exactly as you type them?
- How do you show the key Enter?
- Why do you still need
<inside<code>?
Answers
<pre>.<kbd>Enter</kbd>.- Because the browser still reads
<as the start of a tag, even inside<code>.
Next: that completes the text elements. Put them all together in the practice exercise: a complete recipe page.
a recipe page
Starter code
Build it in your own folder, on your computer. ZAM students hand this project in on their dashboard, and their mentor reviews it.

