HTML uses a few characters as markup: < starts a tag and & starts a character reference. To show those characters as text, they must be written as character references, commonly called entities.
The characters that must be escaped
| Character | Reference | Needed |
|---|---|---|
| & | & | Everywhere text appears |
| < | < | In text content |
| > | > | Recommended for symmetry and safety |
| " | " | Inside double-quoted attributes |
| ' | ' | Inside single-quoted attributes |
Escaping these five characters is enough to display any text safely inside HTML content and quoted attributes. It is also the core defence against injecting markup: if user-provided text is escaped before insertion, <script> becomes visible text instead of a script.
Named and numeric references
A reference can take three forms that all mean the same thing:
- Named:
é→ é - Decimal:
é→ é - Hexadecimal:
é→ é
HTML defines more than 2,000 names, from to ∳. Numeric references can express any Unicode character, including emoji: 😀 is 😀.
Do you still need entities for accents?
Rarely. Pages served as UTF-8, which today is nearly all of the web, can contain é, €, — and emoji directly. Entities for these characters are only needed when the text passes through a system that can’t handle UTF-8, or when you want an invisible character to be visible in the source, such as (non-breaking space) or ­ (soft hyphen).
Common problems
Double escaping. Text escaped twice shows & or " on the page instead of & or ". It happens when content is escaped when saved and again when displayed. The HTML Entity Decoder reverses one level at a time.
Missing semicolons. Browsers accept some legacy references without a semicolon, like ©, but the rules are complicated. Always include the semicolon.
Escaping in the wrong context. HTML escaping does not make text safe inside JavaScript, CSS or URLs. Each context needs its own encoding, such as URL encoding for URLs.
The tools
- HTML Entity Encoder escapes text for HTML.
- HTML Entity Decoder converts references back to characters.
- HTML to Text removes tags and decodes entities together.