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>

html
<p>Every page starts with <code>&lt;!DOCTYPE html&gt;</code>.</p>

Notice &lt; and &gt;: inside <code>, a < still starts a tag, so you still need character references.

Keyboard keys: <kbd>

html
<p>Save your file with <kbd>Ctrl</kbd> + <kbd>S</kbd>.</p>

Program output: <samp>

html
<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>:

html
<!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>&lt;p&gt;
  This is &lt;strong&gt;important&lt;/strong&gt;.
&lt;/p&gt;</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.

The page "My HTML notes". A code block shows three lines — an opening p tag, an indented line with strong tags, and a closing p tag — in a monospace font with the indentation kept. Below it: "To save, press Ctrl + S", with Ctrl and S in monospace.

<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

  1. Select the notes example and choose Try it.
  2. Change <pre> and </pre> to <p> and </p>. The line breaks disappear. Change them back.
  3. 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 &lt;.
  • 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

  1. Which element keeps spaces and line breaks exactly as you type them?
  2. How do you show the key Enter?
  3. Why do you still need &lt; inside <code>?

Answers

  1. <pre>.
  2. <kbd>Enter</kbd>.
  3. 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.

Practice project

a recipe page

Goal: write a complete recipe page using the text elements from this module.

Your website after this module

Outcome: your site has a second page with real content: headings in the right order, emphasis, a quote and a code comment.

Your my-website folder: index.html (about you) and recipe.html.

Own topic? Write an article about it instead of a recipe. Use your own two <h2> sections in place of Ingredients and Steps. Items 1 to 9 still apply.

Warm up first (optional): Fix the Heading Order in Work Space, if it is in your menu. Work Space checks your code at once, so you know you are ready before you build.

What to do

In your my-website folder, create a new file called recipe.html. Write a page for a dish you know — from your country, your family, or your favourite café. It must have:

  1. The full page skeleton, with a <title> that names the dish.
  2. One <h1> with the name of the dish.
  3. A short introduction paragraph with one important phrase in <strong> (for example a warning or a key tip).
  4. An <h2> called Ingredients, with a paragraph that lists them. Use <br> so that each ingredient is on its own line.
  5. An <h2> called Steps, and under it two <h3> headings for two parts of the method, each with a paragraph.
  6. One word in <em> that you would stress when speaking.
  7. One quotation from someone who likes this dish, in <blockquote>, with their name under it.
  8. An ampersand written correctly as &amp;, for example "salt & pepper".
  9. One comment that labels the steps section.

Save, open the page in your browser from the my-website folder, and check it. Then paste all of your code into the box below and submit.

Checklist before you submit

  • The heading outline is h1 → h2 → h3, with no level skipped.
  • Each ingredient is on its own line.
  • The comment does not show on the page.
  • You see & on the page, not &amp;.
  • Every element you opened is closed, in the right order.

Your mentor reviews every submission. The exercise counts as done when it is marked Passed.

Starter code

html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>NAME OF THE DISH</title>
</head>
<body>
  <h1>NAME OF THE DISH</h1>
  <p>A SHORT INTRODUCTION.</p>

  <h2>Ingredients</h2>
  <p>
    FIRST INGREDIENT<br>
    SECOND INGREDIENT
  </p>

  <h2>Steps</h2>
  <h3>FIRST PART</h3>
  <p>WHAT TO DO.</p>
</body>
</html>

Build it in your own folder, on your computer. ZAM students hand this project in on their dashboard, and their mentor reviews it.

Chat with us on WhatsApp