Entities, Whitespace and Comments
Write reserved characters with entities, understand how browsers collapse whitespace, and use comments, with the parsing quirks found in a real browser.
Entities, Whitespace and Comments
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- `<`, `>` and `&` start markup, so to show them as text write `<`, `>` and `&`. Other characters can be written as named (`©`) or numeric (`©`, `©`) references.
- Always end an entity with a semicolon. In a test, `©` without one still became ©, and `¬it;` became `¬it;` because `¬` was matched as a legacy entity.
- Inside attribute values the rules are stricter: `href="?a=1©=2"` kept `©=2` literally. In a test `&` in an `href` became a plain `&`.
- Browsers collapse runs of spaces, tabs and newlines into one space. A non-breaking space (` `) is not collapsed, and `<pre>` preserves whitespace.
- A comment, `<!-- ... -->`, is not shown but is still sent to every visitor and visible in view-source. Never put secrets in one.
Characters the markup reserves
HTML mixes content and instructions in one stream of text, and the parser needs a way to tell them apart. The character < begins a tag. The character & begins a character reference. So if you want the text < or & on screen, you have to write it in a form the parser won’t act on:
| Want | Write |
|---|---|
< |
< |
> |
> |
& |
& |
" (inside a quoted attribute) |
" |
| © | ©, © or © |
| non-breaking space | |
References come in named form (©) and numeric form: © in decimal, © in hex. If your file is saved as UTF-8 and has <meta charset="utf-8">, you can type © directly, and you only need entities for the reserved characters and for invisible ones.
What the parser really does
The rules are older than the web’s current habits, and a few results surprised me. In headless Chromium 159, the text © 2025 © ¬it; & <b> © © x &bogus; came out as © 2025 © ¬it; & <b> © © x &bogus;:
©with no semicolon still became ©, and&became&. A set of legacy names is recognised without one.¬it;became¬it;. The parser saw¬, one of those legacy names, and leftit;as text. A typo in a semicolon-less entity can do this.&bogus;stayed as written: unknown names are left alone.
Attribute values follow stricter rules. In href="/p?a=1©=2&b=3¬it=4¶=5" the parsed value was /p?a=1©=2&b=3¬it=4¶=5: the explicit & became &, and the semicolon-less look-alikes ©=, ¬it= and ¶= stayed as typed. That protects URLs with query strings, but don’t rely on it. Write & between query parameters in HTML source and the question never comes up. title="©" did decode to ©, because nothing followed it.
The takeaway: always use the semicolon, and only use names you looked up.
Whitespace collapses
The browser collapses any run of spaces, tabs and newlines in normal text into one space. In a test, a b, two blank lines, c and a tab before d displayed as a b c d, while the DOM’s textContent still held every original character. So spacing in your source file never creates spacing on the page. That’s why you can indent as deeply as you like.
Three ways to keep spacing when you need it:
is a non-breaking space. It isn’t collapsed and doesn’t allow a line break. In a testa bmeasured five characters. Use it for inseparable pairs such as10 kg, not for layout.<pre>preserves whitespace and uses a monospace font.a band an indented second line came out exactly as typed.- CSS
white-spaceand margins, which are the right tools for layout.
Collapsing has one more effect people meet when they build rows of inline-block elements: the whitespace between them becomes a space character. In a test, two 50px inline-blocks written back to back sat at x=8 and x=58, and with a newline between them the second moved to x=62. That 4px gap is the collapsed newline, and it’s why layouts with display: inline-block look off until you switch to flexbox.
Comments
<!-- like this --> is ignored for display. In the DOM it’s still a node (node type 8). In a test, a<!-- hidden <b>x</b> -->b gave an element whose children were the text a, a comment hidden <b>x</b>, and the text b, and the visible text was just ab. The tags inside the comment were not parsed as elements.
Comments are for the people who maintain the file. They are not private. Anyone can see them with view-source or DevTools. A comment also ends at the first -->, so you can’t comment out a block that already contains a comment. In a test, <!--> after produced an empty comment followed by the text after. That is the abrupt-closing case from the spec, and the reason to write comments in the plain <!-- text --> form.
What the video covers
The embed is a clip, 5:54 to 20:11, from Dave Gray’s “HTML Tag Text Basics | HTML5 Element Text Tutorial” (December 2021, 21 minutes). The chapters in the clip are whitespace collapsing (5:54), line breaks (6:37), adding more text content (7:52), block versus inline elements (10:05), HTML entities (12:27), the abbreviation element (15:12), the address element (17:16) and HTML comments (18:28). The opening part about headings is used in the earlier lesson on headings, paragraphs and text semantics. I could not read a transcript, so I describe it only from the chapters. The parsing results, the attribute-value rules and the inline-block gap measurement are this lesson’s own. The deeper links are MDN’s character reference page and the HTML Standard’s entity table.
Where this fits
This closes the foundations group, after links and URLs. It’s also where the repair behaviour from the first lesson shows up at the character level. The next group, semantic structure, starts with the elements that mark the large areas of a page.
Check yourself
You want a page to display the literal text `<div>`. What do you write?
`<div>`. A raw `<div>` would be parsed as an element. Only `<` strictly needs escaping in text, but escaping `>` as well is the habit, and `&` should always be written `&`.
Why does `¬it;` render as `¬it;`?
Some legacy named references are recognised without a semicolon, and `¬` is one of them. The parser matches `¬`, outputs ¬, and the rest `it;` stays as text. Always include the semicolon and use entity names you have looked up.
A link has `href="/search?q=cats©=1"`. Does the `©` turn into a ©?
No. Inside attribute values, a reference not ending in a semicolon and followed by `=` or a letter or digit is left alone. In a test `©=2` stayed as written. Still, write `&` between query parameters in HTML so the intent is clear.
Three ` ` in a row sit between two words. How is that different from three normal spaces?
Normal whitespace collapses to one space, so three spaces display as one. Non-breaking spaces don't collapse and don't allow a line break. In a test `a b` had a five-character `innerText`. Use CSS for layout spacing, and ` ` for cases like keeping '10 kg' together.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.