Core answer: HTML entities escape characters that are structural or invisible: < → <, > → >, & → &, " → ", ' → '. Named ( ), decimal (©), and hex (©) forms exist — © is © = © = ©. The five structural escapes are mandatory in content; anything beyond UTF-8 text is optional in modern documents. Unescaped user input rendered raw = XSS.
The mandatory five
| Char | Entity | Why mandatory |
|---|---|---|
| < | < | starts tags |
| > | > | ends tags |
| & | & | starts entities itself |
| " | " | breaks attribute values |
| ' | ' | breaks single-quoted attributes |
Write
if (a < b && c > d)
or the parser eats your comparison as a tag.The useful named entities
| Entity | Renders | Use |
|---|---|---|
| non-breaking space | keep "100 km" on one line | |
| © | © | copyright |
| ® / ™ | ® ™ | marks |
| — – | — – | real dashes |
| … | … | ellipsis |
| × ÷ | × ÷ | math |
| ± | ± | tolerances |
| ° | ° | degrees |
| ¥ € £ | ¥ € £ | currency |
| ← → | ← → | arrows |
Why escaping is THE security boundary
User comment: . Rendered raw, it executes. Rendered escaped (<script>...), it displays as harmless text. Every framework's auto-escaping (React's JSX, Vue's mustaches) exists for this; dangerouslySetInnerHTML / v-html bypass it — hence the names.
Worked examples
Example 1 — Displaying code. To show
Example 2 — Attribute injection. breaks on the apostrophe; use ' or switch attribute quotes.
Example 3 — The & URL bug. Links with & in query strings (?a=1&b=2) inside HTML attributes should be & — validators complain, and some contexts misparse.
Example 4 — Non-breaking space abuse. Ten for indentation is 1998; use CSS margin/padding. Legit use: keeping "Figure 3" or "¥ 100" unbreakable.
Common mistakes and myths
- Double-encoding — < displays "<" instead of "<"; sanitize once, at the render boundary.
- Escaping in the database — store raw text, escape on output; escaping on input corrupts data for non-HTML consumers (emails, APIs).
- Assuming entities cover security — entities fix HTML contexts; URLs need percent-encoding, JS strings need JS escaping, CSS needs CSS escaping. Context matters.
- Numeric entity confusion — © decimal vs © hex; both fine, don't mix digits across forms by accident.
- UTF-8 paranoia — with you can type © directly; entities remain mandatory only for the structural five.
Frequently Asked Questions
Why does HTML need entities?
What are the must-know entities?
Named vs numeric entities — what is the difference?
What is really for?
How do entities relate to XSS?
Input
Output
Why encode?
- 1< and > are tag delimiters — a literal < in text may be parsed as a tag start and break the page.
- 2& starts an entity reference — literal & must be written as &.
- 3In attribute values, quotes must be escaped to avoid premature attribute termination (XSS vector).
Common entities
| Char | Named | Decimal | Description |
|---|---|---|---|
| < | < | < | Less than |
| > | > | > | Greater than |
| & | & | & | Ampersand |
| " | " | " | Double quote |
| ' | ' | ' | Single quote |
| ␣ | |   | Non-breaking space |
| © | © | © | Copyright |
| ® | ® | ® | Registered |
| ™ | ™ | ™ | Trademark |
| ¥ | ¥ | ¥ | Yen |
| € | € | € | Euro |
| ° | ° | ° | Degree |
| ± | ± | ± | Plus-minus |
| × | × | × | Multiplication |
| ÷ | ÷ | ÷ | Division |
| ← | ← | ← | Left arrow |
| → | → | → | Right arrow |
| ♥ | ♥ | ♥ | Heart |