Sub topic 7
Character references and comments
This chapter covers two small features that appear in almost every real page. Character references let you write characters that HTML would otherwise treat as markup. Comments let you leave notes in the source that the browser does not show. I assume you can already write a basic document with a doctype, html, head and body, and that you know what an element and an attribute are.
Why character references exist
The HTML parser reads your file as a stream of characters. When it meets <, it starts looking for a tag. When it meets &, it starts looking for a character reference. If you want to show either character as plain text, you have to say so explicitly.
A character reference is a sequence that starts with & and ends with ;. The browser replaces it with one character (a few produce two) when it builds the page. There are three forms:
| Form | Pattern | Example | Result |
|---|---|---|---|
| Named | `&name;` | `©` | © |
| Decimal numeric | `&#number;` | `©` | © |
| Hexadecimal numeric | `&#xhex;` | `©` | © |
The numbers are Unicode code points. Named references are easier to read, but only a fixed list of names exists. Numeric references work for any character, so they are the fallback when no name exists.
The characters you must escape
Five characters need attention. The first two matter in nearly every document.
| Character | Reference | When you need it |
|---|---|---|
| `<` | `<` | In text, whenever it could be read as the start of a tag |
| `&` | `&` | In text and attribute values, whenever it could start a reference |
| `>` | `>` | Optional in text, but harmless and tidy |
| `"` | `"` | Inside an attribute value delimited by double quotes |
| `'` | `'` | Inside an attribute value delimited by single quotes (or use `'`) |
Here is a paragraph that displays markup as text:
<p>To make a paragraph, write <code><p>Hello</p></code>.</p>
<p>Fish & chips cost £5.50.</p>
<p>Copyright © 2026 — all rights reserved.</p>The browser shows the first line as To make a paragraph, write <p>Hello</p>. without creating a paragraph. This is the standard way to show HTML source in a tutorial.
Escaping inside attribute values
Attribute values are parsed for references too. A URL with a query string often contains &, and strictly it should be written &:
<a href="/search?q=html&page=2">Search results</a>
<img src="cat.jpg" alt="A cat called "Biscuit"">
<img src="dog.jpg" alt='A dog called "Rex"'>The second line escapes the double quotes so they do not end the value early. The third line avoids the problem by using single quotes around the value. Either approach is valid. Pick the one that needs less escaping.
Browsers are forgiving about a bare & that is followed by a space or something that is not a known name. Do not rely on this. A URL such as ?x=1©=2 contains the text ©, which some parsers read as the copyright sign. Writing & removes the ambiguity, and validators flag the unescaped form.
How the parser decides
The flow below shows what the tokenizer does when it reaches an ampersand in normal text.
Real browsers also accept a handful of legacy names without the final semicolon, such as © and &. That leniency exists for old pages. Always write the semicolon.
Spaces and invisible characters
is a non-breaking space. It looks like a space but the browser will not wrap a line at it, and it is not collapsed with neighbouring spaces. Normal whitespace collapses: any run of spaces, tabs and newlines renders as one space.
Use where a line break would be wrong, for example between a number and its unit:
<p>The file is 4 MB, so the download takes about 10 seconds.</p>Do not use repeated to indent or position things. That belongs to CSS. Screen readers may also announce stretches of non-breaking spaces oddly.
Other useful references include  ,  ,  , ­ (a soft hyphen that appears only if the word must break) and … (an ellipsis).
Do you still need references for ©, é and €?
Usually no. If your file is saved as UTF-8 and the head contains <meta charset="utf-8">, you can type ©, é or € directly. References remain useful in three cases:
- The character is one of the reserved ones (
<,&, and quotes in attributes). - The character is invisible or easy to confuse, such as a non-breaking space or a soft hyphen. A reference documents your intent.
- You cannot be sure of the file encoding, or your editor makes the character hard to type.
If the charset declaration is missing and the file is not UTF-8, directly typed characters can appear garbled. Numeric references are immune to that problem, because they are plain ASCII text.
Where references are not decoded
Not every part of a document is parsed the same way. Inside script and style elements the content is raw text. < stays as the four characters <. This is correct for code, but it means you cannot use references to escape things there. Use the escaping rules of JavaScript or CSS instead.
Inside textarea and title, references are decoded but tags are not recognised. So <title>Q&A</title> shows "Q&A" in the tab, and <title>Use <b> tags</title> shows the <b> literally.
Comments
A comment is text in the source that the browser parses but does not render. The syntax is:
<!-- This is a comment -->
<p>Visible text.</p>
<!--
Comments can span
several lines.
-->Comments are useful for three things:
- Marking regions of a long page, such as
<!-- end of navigation -->. - Leaving a note for a teammate or your future self, such as
<!-- TODO: replace with the final logo -->. - Temporarily disabling markup while you debug.
Comments can appear almost anywhere in the document, including before the doctype and inside the head. They cannot appear inside a tag or inside an attribute value. <p <!-- note --> class="intro"> is not a comment.
Rules that cause bugs
The comment syntax is stricter than it looks. The current specification says the text of a comment must not:
- start with
>or->, - contain
<!--,-->or--!>, - end with
<!-.
The practical consequence is that comments do not nest. The first --> ends the comment, whatever came before it:
<!-- disabled for now
<p>Old text</p>
<!-- a note I added earlier -->
<p>This paragraph is NOT hidden.</p>
-->Here the comment ends at the first -->, on the third line. The fourth line renders, and the stray --> on the last line appears as visible text. When you comment out a block, check that it contains no comments of its own.
Do not write double hyphens inside a comment as decoration, such as <!-- ---- section ---- -->. Modern parsers tolerate most of these, but older tools and validators complain.
Comments are public
Everyone can read your comments through "View source" or the developer tools. Never put passwords, internal URLs, personal data or unfinished plans in them. Comments also add bytes to every download, so long notes are better kept in your repository documentation than in production markup.
Two obsolete uses
Old tutorials mention conditional comments such as <!--[if IE 8]>...<![endif]-->. Modern browsers ignore them, so do not use them. Old tutorials also wrapped the contents of script and style in comments to hide them from ancient browsers. That is no longer needed.
Putting it together
This page uses both features and is valid as written:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Q&A: Tags & Entities</title>
</head>
<body>
<!-- Main heading -->
<h1>Writing <tags> as text</h1>
<p>Use <code>&lt;</code> to show a less-than sign.</p>
<p>Price: €4.50 per 100 g.</p>
<!-- TODO: add a table of common references -->
</body>
</html>Notice that to show the text < on screen, you escape the ampersand itself: &lt;.
Exercise
A page contains <p>Use the tag <img> to add a picture.</p> and the browser shows "Use the tag to add a picture." with a broken image icon. Fix the markup so the text <img> appears. Then explain what happens to <!-- hide <!-- inner --> still here --> in the browser.
Show answer
Write <p>Use the tag <img> to add a picture.</p>. Only the < strictly needs escaping, but escaping > as well is tidy. In the comment example, comments do not nest, so the comment ends at the first -->, after "inner". The text " still here -->" is then rendered as visible text.
Exercise
Write the correct markup for a link to /shop?item=5&size=large whose visible text is "Large widget (5 for £10)". Use a numeric reference for the pound sign and a reference that stops "5 for" from wrapping away from "£10".
Show answer
<a href="/shop?item=5&size=large">Large widget (5 for £10)</a>. The & in the URL avoids ambiguity with a possible reference. £ is the pound sign, and keeps "for" and the price on one line.