HTML Entities
HTML Entities
Level 9 — DOM, Rendering & Accessibility Special text code strings starting with an ampersand (
&) and ending with a semicolon (;) used to display reserved characters, special symbols, and invisible spacing in HTML.
1. Prerequisites
- Element vs. Tag — Understanding that browsers parse
<and>as tags. - Character Encoding (
charset) — The system mapping character lookups.
2. Term Category
Syntax / Concept (Universal Browser Support .): HTML Entities is a fundamental concept in this technology stack. Level 9 — DOM, Rendering & Accessibility
3. Explanation
(1) Design Motivation — "Why did we design this?"
There are three main scenarios where you cannot simply type a character into your HTML text:
1. Reserved Characters (Parser Conflicts)
The characters < (less-than) and > (greater-than) are reserved because the browser uses them to identify HTML tags.
If you type:
<p>In math, 5 < 10 is true.</p>
The browser gets confused. It sees < 10 and thinks you are starting a tag named 10. It will search for a closing tag, hide the text from the user, and break the rendering.
2. Invisible Formatting
By default, HTML collapses multiple spaces down to a single space (Whitespace Collapse). If you want to force words to stay on the same line, or add a specific space that never breaks, you cannot do it with standard typing.
3. Keyboard Restrictions
Symbols like copyright (©), trademark (™), or mathematical signs (e.g. ±) are not found on standard keyboards.
The W3C created HTML Entities to solve all three issues. They act as "escape codes" that tell the browser's HTML parser: "Do not process this code as a tag or space; just print this specific symbol on the screen."
(2) Common HTML Entities
All HTML entities follow a strict syntax: &[EntityName]; (must start with an ampersand and end with a semicolon).
| Visual Symbol | Entity Code | Symbol Name | Primary Use Case |
|---|---|---|---|
< | < | Less Than | Displaying math inequalities or printing HTML tag examples. |
> | > | Greater Than | Displaying math inequalities or printing HTML tag examples. |
& | & | Ampersand | Displaying ampersands (since & alone starts an entity code). |
" | " | Double Quote | Escaping quotes inside attribute values. |
© | © | Copyright | Placing standard copyright markers in footers. |
| | Non-Breaking Space | A space that prevents words from wrapping onto a new line (e.g. $100 million). |
(3) Code Examples
Short Snippet
Displaying code examples safely:
<p>To create a paragraph, use the <p> tag.</p>
<!-- Displays: To create a paragraph, use the <p> tag. -->
Fuller Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>HTML Entities Showcase</title>
</head>
<body>
<h1>Cooking & Baking Guide</h1> <!-- Renders: Cooking & Baking Guide -->
<p>
If you mix flour < water, you get paste.
If you mix flour & water & yeast, you get bread!
</p>
<p>
Standard copy rules apply. For licensing details,
contact our legal department.
</p>
<footer>
<p>© 2026 BreadHub Inc. All rights reserved.</p>
</footer>
</body>
</html>
4. Common Mistakes & Pitfalls
Mistake 1: Forgetting the closing semicolon
The mistake: Leaving the semicolon off the end of the entity code:
<!-- BAD: Might render as literal text "© 2026" or break parsing! -->
<p>© 2026 Company Name</p>
Why it's wrong: The browser parses ampersands as entity starters. It searches forward until it finds a semicolon to close the lookup code. If you omit the semicolon, the browser might fail to translate the symbol, or get confused if the following word starts with letters matching other entities.
Mistake 2: Abusing for layout spacing
The mistake: Using multiple non-breaking spaces to push text across the screen:
<!-- BAD: DO NOT DO THIS! (Messy unmaintainable structure) -->
<p>Logo Navigation</p>
Why it's wrong: Non-breaking spaces are strictly semantic markers to prevent line wraps between two related words. They are not layout tools. Using them for spacing causes layouts to break on different screen sizes and ruins responsiveness. Spacing layout is CSS's job (using padding, margin, or CSS flexbox/grid).
Mistake 3: Writing Raw Special Characters (<, >, &) in HTML Body Text
The mistake: Writing <p>5 < 10 & 20 > 15</p>.
Why it's wrong: Browsers interpret raw < and & as HTML tag openings and entity references. Unescaped characters cause parsing errors. Use <, >, &.
Incorrect:
<p>Compare 5 < 10 & 20 > 15</p> <!-- ❌ Unescaped special characters! -->
Fix:
<p>Compare 5 < 10 & 20 > 15</p>
Mistake 4: Forgetting Semicolons at the End of HTML Entity Names (© vs ©)
The mistake: Writing © 2026 without a trailing semicolon.
Why it's wrong: HTML entities MUST end with a trailing semicolon ;. Omitting semicolons leads to inconsistent rendering across browser parsers.
Incorrect:
<p>© 2026 Company</p> <!-- ❌ Missing trailing semicolon! -->
Fix:
<p>© 2026 Company</p>
5. Practice Exercises
Exercise 1: Safely Encoding Reserved HTML Characters in Code Snippets
Scenario: An author uses HTML entity codes (<, >, &) to display HTML markup examples inside a tutorial without triggering browser parsing.
Requirements:
- Use
<for<and>for>. - Use
&for&and"for". - Wrap in
<pre><code>block.
Answer
Implementation
<pre><code><div class="container">
<p>This text contains an ampersand (&) symbol.</p>
</div></code></pre>
Technical Explanation
- HTML Entity Purpose: Special character sequences starting with
&and ending with;used to display reserved characters or special symbols. - Reserved Character Escaping:
<(<),>(>),&(&), and"(") MUST be escaped in text to prevent HTML parsing errors. - Preventing Script Injection: Escaping user-generated text into entities prevents Cross-Site Scripting (XSS) attacks.
Exercise 2: Rendering Special Copyright and Currency Entities
Scenario: Displays special typographical symbols using standard named HTML entities.
Requirements:
- Use
©for copyright. - Use
™and®for trademarks. - Use
€and£for currency.
Answer
Implementation
<footer>
<p>© 2026 Acme™ Corp. All Rights Reserved ®.</p>
<p>Pricing: €19.99 / £15.00 / ¥2,500</p>
</footer>
Technical Explanation
- Typographical Entities: Named entities represent symbols like
©(©),™(™),®(®), and€(€). - Browser Encoding Safety: Ensures correct symbol rendering across older non-UTF-8 servers.
- UTF-8 Equivalent: In modern UTF-8 documents, literal symbols (©, €) can be used directly, but entities remain standard fallbacks.
Exercise 3: Non-Breaking Space ( ) Usage vs CSS Margin Spacing
Scenario: Uses to prevent unwanted word wrapping in brand titles while using CSS for layout spacing.
Requirements:
- Use
between brand words.
Answer
Implementation
<p>
Welcome to Acme Enterprises.
</p>
Technical Explanation
- Non-Breaking Space (
): Prevents automatic line breaks between adjacent words. - Layout Misuse Warning: Do NOT use multiple
strings for visual layout margins; use CSSmargin/paddinginstead. - Numeric Entities: Can also be written using Unicode numbers (
 ).
6. Related Terms
- Element vs. Tag — The tags that require less-than/greater-than signs.
- Character Encoding (
charset) — The underlying byte mapping. - Whitespace Collapse — The default browser behavior that
bypasses.
7. Key Takeaways
- HTML Entities display reserved tags, special keyboard characters, and custom spacings.
- All entities follow the pattern:
&[EntityName];. - The less-than (
<) and greater-than (>) entities prevent browsers from confusing text for HTML elements. - The non-breaking space (
) prevents line wrapping between words. - Never use
to force visual page layouts; use CSS spacing attributes instead.