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
<!-- Keep the current room number in this notice. -->
<p>The practice room is Room 6.</p><!-- 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 comment may cover several source lines
<!--
This notice uses fictional details.
Update the visible date before reusing it.
-->Comments do not protect information
Complete example: a source note and visible instructions
<!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>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.
Code describes a code sample; it does not run it
<p>A paragraph uses the <code>p</code> element.</p>Escape the sample's angle brackets and ampersands
<p>, write <p> in the source. These are character references: spellings that represent characters. Chapter 13 explains them fully.
<p>Example: <code><p>Read carefully.</p></code></p>& when needed, especially when showing an entity's spelling.
Pre and code work together
<pre><code><h1>Daily practice</h1>
<p>Check one result.</p></code></pre>Kbd, samp and var describe different things
Complete example: a tiny lesson about paragraphs
<!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><p>Check the saved page.</p></code></pre>
</body>
</html>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.
- 1Comment: a note in source
- 2Code: a code fragment to read
- 3Kbd: input the reader enters
- 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
<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.
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
Write a comment saying that the example uses fictional data. Should a password be stored in an HTML comment? How do you display the literal text <h2>inside a code element?Which element describes a shortcut, and which describes sample output? Why might you combine pre with code? Create a small lesson page showing an escaped HTML paragraph and a visible save instruction. Add one useful source comment.
Check your practice answers
<!-- This example uses fictional data. -->.No. Delivered comments can be inspected. Use <code><h2></code>.Kbd describes user input; samp describes sample output. Code supplies the code-sample meaning, while pre preserves meaningful source spacing and line breaks. 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?
Does code automatically escape HTML or run the text inside it?
How do pre and code differ?
What is a useful comment?
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.