CHAPTER 12 · HTML

Comments and code text: explain source without confusing the browser

Sometimes you need to leave a note for another author. Sometimes the reader needs to see HTML itself as an example. Those are different tasks. This chapter separates comments hidden from the normal page view from code deliberately displayed as content.

Download example files (ZIP)

What you will learn

  • Write single-line and multiline HTML comments.
  • Avoid broken boundaries and nested comments.
  • Understand why comments are not private storage.
  • Display code without accidentally creating page elements.
  • Use code, pre, kbd, samp and var for their different roles.

Comments are notes in the source

A comment is a source note surrounded by comment markers. In ordinary body markup, the browser does not display that comment as page text.

HTML fragment — part of a document
<!-- Keep the current room number in this notice. -->
<p>The practice room is Room 6.</p>

The opening marker is <!-- and the closing marker is -->. The first marker has an exclamation mark; the closing one does not. Keep those exact boundaries. Spaces after the opener make the note easier to read but are not part of the marker itself.

A useful comment explains a reason, decision or maintenance requirement. A note that only repeats heading starts here above an obvious heading often adds little value.

A comment may cover several source lines

HTML fragment — part of a document
<!--
This notice uses fictional details.
Update the visible date before reusing it.
-->

The same two markers work across lines. You do not need an extra comment opener on every line. Do not nest one complete comment inside another; the browser's comment parsing does not treat it like a set of nested boxes.

For predictable beginner comments, use ordinary short text and avoid inserting comment-boundary sequences in the note itself. If you are temporarily commenting out markup that already contains comments, move or remove those inner notes rather than wrapping them blindly.

Comments do not protect information

A comment can remain in the HTML delivered to a visitor. People can view source or inspect the document and read it. Do not place passwords, private contact details, access tokens or confidential notes in it.

Comments are also not a complete content-management system. Leaving an outdated notice inside a comment may be convenient during local editing, but remove content that must not be delivered before publishing.

Complete example: a source note and visible instructions

commented-notice.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Practice Notice with Source Notes</title>
</head>
<body>
  <!-- These room details are fictional teaching data. -->
  <h1>Practice session notice</h1>
  <p>Bring one saved HTML example to Room 6.</p>
  <!-- Keep instructions visible rather than only in a comment. -->
  <p>Save the file before checking it in your browser.</p>
</body>
</html>
Browser previewDocument title: Practice Notice with Source Notes

This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.

The preview should show the heading and two paragraphs, not the comments. The Copy button still copies the whole source, including comments. That difference demonstrates why hidden from normal display does not mean secret.

Code describes a code sample; it does not run it

The code element marks a piece of computer code in the page's content. A browser normally uses a fixed-width font, but the important point is its code-sample meaning.

HTML fragment — part of a document
<p>A paragraph uses the <code>p</code> element.</p>

Putting JavaScript or C source inside code does not execute it. The element is for showing text. However, HTML-looking text still needs escaping so it is not interpreted as markup inside the code element.

Escape the sample's angle brackets and ampersands

To show the literal characters <p>, write &lt;p&gt; in the source. These are character references: spellings that represent characters. Chapter 13 explains them fully.

HTML fragment — part of a document
<p>Example: <code>&lt;p&gt;Read carefully.&lt;/p&gt;</code></p>

The result displays the tag text as part of the explanation. It does not create a second real p element within the example's paragraph. If the code sample itself contains an ampersand, escape it as &amp; when needed, especially when showing an entity's spelling.

Do not use an obsolete xmp element to bypass these rules. Use suitable code markup and escaped text.

Pre and code work together

Code by itself does not preserve every source line break and space. Use pre around code when the formatting is meaningful to the sample.

HTML fragment — part of a document
<pre><code>&lt;h1&gt;Daily practice&lt;/h1&gt;
&lt;p&gt;Check one result.&lt;/p&gt;</code></pre>

Pre preserves the meaningful spacing, while code identifies what that content represents. Preformatted text can be wider than a phone, so a production design should let readers scroll or wrap code appropriately rather than clipping it. Our tutorial's own code boxes support horizontal scrolling.

Kbd, samp and var describe different things

ElementUseExample role
codeA code sampleAn element name or a small program
prePreformatted contentA sample where line breaks matter
kbdUser inputA keyboard shortcut
sampSample output from a programA message shown by a tool
varA variable in code or mathematicsThe name of a quantity

Kbd does not install a shortcut or capture key presses. Samp does not generate the output. Var does not create a working JavaScript variable. These elements describe content that the author supplies.

Complete example: a tiny lesson about paragraphs

code-reading.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Read a Paragraph Example</title>
</head>
<body>
  <h1>Read and save the example</h1>
  <p>On Windows or Linux, save with <kbd>Ctrl</kbd> + <kbd>S</kbd>.</p>
  <p>A tool might report <samp>File saved</samp>.</p>
  <p>Let <var>n</var> mean the number of practice pages.</p>
  <pre><code>&lt;p&gt;Check the saved page.&lt;/p&gt;</code></pre>
</body>
</html>
Browser previewDocument title: Read a Paragraph Example

This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.

The shortcut description is platform-specific; macOS commonly uses Command+S. The sample message is an illustration, not a message produced by this HTML file. The final code sample should display literal paragraph tags in the preview.

Choose source notes or displayed text
  1. 1Comment: a note in source
  2. 2Code: a code fragment to read
  3. 3Kbd: input the reader enters
  4. 4Samp: example output

Escape literal angle brackets when displaying HTML. Comments are public source, not a safe place for secrets.

Problem and solution: the example becomes part of the page

Problem: an author writes <code><h1>Demo</h1></code> intending to show the tag characters. The browser instead interprets the inner markup, and the result is not the intended code text.

Solution: escape the sample's markup characters and use pre where meaningful whitespace should remain. Compare the actual displayed characters with the sample you wanted readers to copy.

Common mistakes and useful tips

  • Writing a broken opener such as <! --.
  • Forgetting the end of a comment and hiding more content than intended.
  • Nesting comments without checking their boundaries.
  • Using comments as secret storage.
  • Expecting code to escape nested HTML automatically.
  • Assuming pre means executable code or kbd creates a key handler.
  • Writing essential instructions only in comments instead of visible text.

Practice questions

  1. Write a comment saying that the example uses fictional data.
  2. Should a password be stored in an HTML comment?
  3. How do you display the literal text <h2> inside a code element?
  4. Which element describes a shortcut, and which describes sample output?
  5. Why might you combine pre with code?
  6. Create a small lesson page showing an escaped HTML paragraph and a visible save instruction. Add one useful source comment.
Check your practice answers
  1. <!-- This example uses fictional data. -->.
  2. No. Delivered comments can be inspected.
  3. Use <code>&lt;h2&gt;</code>.
  4. Kbd describes user input; samp describes sample output.
  5. Code supplies the code-sample meaning, while pre preserves meaningful source spacing and line breaks.
  6. Use a complete document, escape sample tags and keep the save instruction visible. Check that the sample appears as text rather than creating a real paragraph inside it.

Interview questions

Are HTML comments private?

No. They are omitted from the normal rendered text, but they can remain in the source delivered to visitors.

Does code automatically escape HTML or run the text inside it?

No. It identifies a code sample. Escape markup characters for display; the element does not execute a program.

How do pre and code differ?

Pre describes preformatted content and normally preserves whitespace. Code marks a computer-code sample. Combining them can express both roles.

What is a useful comment?

One that explains a decision, reason or maintenance detail not obvious from the markup. It should not replace clear source or necessary visible instructions.

Quick revision

  • Comments use exact boundaries and do not nest normally.
  • Hidden from the normal view is not secret.
  • Escape code samples that contain HTML characters.
  • Pre, code, kbd, samp and var describe different roles.
  • A displayed sample is not automatically executed behaviour.