Sub topic 7
Character References and Comments
This chapter assumes you can create and save an HTML file, open it in a browser, and recognize an element with its start tag, content, end tag and attributes. It covers two small features that appear in nearly every real page: character references and comments.
Why some characters need special handling
The browser reads your markup and treats < as the start of a tag and & as the start of a character reference. If you want these characters to appear as visible text, you must write them in a form the parser will not mistake for markup.
A character reference is a piece of text that stands for one character. It starts with & and ends with ;. There are two kinds:
- A named reference uses a name, such as
&for the ampersand. - A numeric reference uses the character's Unicode code point, either in decimal (
©) or in hexadecimal (©).
Both © and © produce the copyright sign ©.
The characters you must escape
Two characters are the ones you will escape most often in text content.
| Character | Reference | Why |
|---|---|---|
| `<` | `<` | Otherwise it may begin a tag |
| `&` | `&` | Otherwise it may begin a reference |
| `>` | `>` | Optional in text, but common for symmetry |
| `"` | `"` | Needed inside a double-quoted attribute value |
| non-breaking space | ` ` | Keeps two words on one line |
<p>To write a paragraph, type <p> before your text.</p>
<p>Fish & chips cost £8.</p>
<p>Price: 10 USD</p>
<p>© 2026 Garden Notes</p>
<a href="/search?q=salt&pepper" title="Say "hello"">Search</a>The browser displays <p> as plain text in the first line. In the last line, the & inside the URL is the correct way to write an ampersand in an attribute value.
Typing characters directly
If your file is saved as UTF-8 and the head declares <meta charset="utf-8">, you can type characters such as é, €, and 日本語 directly. You do not need é or €. References remain useful for characters that are hard to type, for invisible characters such as the non-breaking space, and for the reserved characters in the table above.
Common errors
- A missing semicolon, as in
© 2026. Browsers often recover from this, but the behavior is inconsistent and validators report it. - A raw
&followed by letters, as inAT&T. WriteAT&T. - An unknown name, such as
©right;. The browser shows it as literal text. - Putting references inside comments, where they are not interpreted.
Comments
A comment is text in your source that the browser does not display. It begins with <!-- and ends with -->.
<!-- Navigation starts here -->
<nav>
<a href="/">Home</a>
</nav>
<!-- Navigation ends here -->
<!--
TODO: replace this image
<img src="old.jpg" alt="Old photo">
-->Comments let you leave notes for yourself or other authors, and they let you disable a block of markup temporarily while testing.
Comments are not private. Anyone can read them with "View source" or the developer tools. Never put passwords, internal addresses, or unfinished plans in one.
A comment cannot contain --> inside it, because that ends the comment. Do not nest comments: the first --> closes the whole thing, and the rest of your text appears on the page.
Exercise
You want a page to show the literal text <em> and the phrase "Q&A". What do you write in the HTML source?
Show answer
Write <em> and Q&A. The first stops the parser from reading a tag, and the second stops it from reading &A as the start of a reference. The page then shows <em> and Q&A as plain text.
Exercise
A colleague wraps a block of markup in a comment to hide it, but the block already contains a comment. What goes wrong?
Show answer
Comments do not nest. The first --> ends the outer comment, so the remaining markup after it is shown on the page, and the final --> appears as text.