HTML entities: which characters to escape and how
An HTML entity is a short code that stands for a character, such as < for a less-than sign. You need them when a character would otherwise be read as markup, and they are handy for symbols that are hard to type.
Encode or decode entities
The five characters that matter
Only a handful of characters have a special meaning in HTML. Escape them whenever they should appear as text:
| Character | Entity | Must escape when |
|---|---|---|
& | & | Anywhere in text or attribute values, since it starts every entity |
< | < | In text, because it starts a tag |
> | > | Optional in most places, but escaping it keeps things symmetrical |
" | " | Inside an attribute value wrapped in double quotes |
' | ' | Inside an attribute value wrapped in single quotes |
The encoder above escapes exactly these five. Everything else, including accented letters and emoji, can stay as it is in a page saved as UTF-8 with <meta charset="utf-8">.
Three ways to write the same character
The copyright sign can be written in three equivalent forms:
- Named:
© - Decimal:
© - Hexadecimal:
©
The numbers are the character’s Unicode code point. Hex A9 is 10 × 16 + 9 = 169 in decimal, which is why both numeric forms give the same symbol. Named entities only exist for a fixed list of characters, but numeric references work for any code point, including emoji: the grinning face is U+1F600, so 😀 displays it.
Escaping by hand, step by step
- Replace every
&with&first. If you do it last, you also break the ampersands of the entities you just wrote. - Replace
<with<and>with>. - Inside attribute values, replace the quote character you used around the value.
- Leave the result alone. Running the steps twice gives
&lt;, which shows up on the page as the literal text<.
Example. To show the code if (a < b && c) in a paragraph, write if (a < b && c) in the source.
Why escaping is a security issue
If a site prints a visitor’s comment into a page without escaping it, a comment containing a <script> tag becomes real code that runs for every reader. This is called cross-site scripting (XSS). Templating systems in most web frameworks escape output by default for this reason. When you write HTML from JavaScript, set textContent instead of innerHTML for user text, and the browser treats it as plain text.
Entity encoding is the right defence for HTML text and quoted attributes. It is not enough inside a <script> block, a CSS block, or a URL, which each need their own kind of escaping. For URLs, see the URL encoding guide.
Common mistakes
- Double encoding, which shows readers raw codes such as
&. - Forgetting the closing semicolon. Browsers forgive it for some names, but not reliably.
- Using
repeatedly to make space. Use CSS margins or padding instead.
Questions
Do I need entities for accented letters?
Not if the page is saved and served as UTF-8. Typing é directly is fine and easier to read.
What does the decoder do with names it does not know?
It decodes all numeric references and a list of common named ones, and leaves any other name unchanged so you can see it. Your browser itself understands the full list of named entities.