HTML Entities, Explained
After reading this you will know exactly which characters you must escape in HTML, how named, decimal and hex entities relate to each other, and how to pick the right form without breaking your markup.
An HTML entity is a way to write a character using only plain ASCII text. Instead of typing the character itself, you type a short code that the browser expands. The classic case is the ampersand. If you write Tom & Jerry in your source, the browser shows Tom & Jerry on screen, but the source contains Tom & Jerry. The five characters & stand in for one visible &.
You reach for entities for two reasons: to escape characters that would otherwise be parsed as markup, and to insert characters you cannot easily type, such as an em dash, a non-breaking space, or the multiplication sign. The reference table on the tool page lists each character with its named form, its decimal form and its hex form, so you can copy whichever one fits.
The three forms of an entity
Every entity you can write falls into one of three syntaxes. They all start with & and end with ;.
- Named entity
- A memorable keyword between the ampersand and semicolon, for example
&, ,©. Names come from a fixed list maintained in the HTML specification. There are roughly 2,231 named references defined. - Decimal numeric reference
- An ampersand, a hash, the Unicode code point in base 10, then a semicolon:
&for the ampersand (code point 38),—for the em dash (code point 8212). - Hexadecimal numeric reference
- The same as decimal but with an
xafter the hash and the code point in base 16:&for the ampersand,—for the em dash.
The two numeric forms describe the same code point in different bases, so they always produce the same character. The named form is a lookup: & is defined to mean code point 38, which is exactly what & and & also produce.
The naming rule is set by the WHATWG HTML Living Standard, section 13.5, "Named character references." Numeric references are defined in the tokenizer rules of the same document. The full list of names is machine-generated and shipped as a JSON file the browsers agree on.
Which characters you actually have to escape
This is the part most people get wrong by escaping too much. When your document is served as UTF-8, you can put almost any character directly in the text. You do not need é to write é; the literal é works. Entities are only mandatory for characters that would confuse the parser.
In ordinary text content, two characters are unsafe: & and <. The ampersand starts an entity, and the less-than sign starts a tag. Inside a double-quoted attribute value you must also escape ", and inside a single-quoted attribute value you must escape '. The greater-than sign is safe in practice but is conventionally escaped as > for symmetry.
| Character | Named | Decimal | Hex | Escape where |
|---|---|---|---|---|
| & | & | & | & | Everywhere |
| < | < | < | < | Text content |
| > | > | > | > | By convention |
| " | " | " | " | Double-quoted attributes |
| ' | ' | ' | ' | Single-quoted attributes |
Note one asymmetry: ' is defined in HTML5 and in XML, but it was not defined in HTML 4. If you must support very old parsers, use the numeric ', which has always worked.
How a code point becomes hex
Converting a Unicode code point to its hex reference is a base conversion. Take the em dash, code point 8212 in decimal. To write it in base 16, divide repeatedly by 16 and read the remainders from bottom to top.
Here each coefficient is a hex digit. Reading them in order gives 2, 0, 1, 4, so the hex string is 2014 and the reference is —. The digits 10 through 15 are written A through F. The term 16^3 equals 4096, so 2 \times 4096 = 8192, and adding 16 + 4 returns 8212. That check confirms the conversion.
Worked example: encoding a price string
Suppose you want to render the exact text Sale: "£10 & up", today only safely inside a paragraph. Walk through each special character.
- The ampersand at code point 38 becomes
&. Leaving it raw would start a bogus entity. - The double quotes are inside text content, not an attribute, so they are safe as literal
". Escaping them to"is optional and harmless. - The pound sign £ is code point 163. In UTF-8 you can leave it literal, or write
£,£or£. - If the text also used an em dash (code point 8212), the literal is fine in UTF-8, or use
—,—or—.
The minimal safe result is Sale: "£10 & up", today only. Only the ampersand had to change. The maximal, fully entitized version is Sale: "£10 & up", today only. Both display identically.
Reading the reference table
Each row in the tool gives you four things to copy: the literal character, the named form, the decimal form and the hex form. Pick by context.
- Writing hand-authored HTML in a UTF-8 file: copy the literal character when it is safe, or the named form when you want readable source.
- Generating markup in a language without a full entity table: use the numeric form, since any code point works without a lookup list.
- Targeting XML or strict XHTML: only five named entities exist there (
&,<,>,",'), so use numeric for everything else.
When in doubt, the decimal numeric form is the most portable.   for a non-breaking space works in HTML, XHTML and XML, while works in HTML but throws an "undefined entity" error in raw XML.
Common mistakes
Forgetting the semicolon. Named references normally require a trailing ;. A small set of legacy names such as & and © are parsed without one for backward compatibility, but relying on that is fragile. ©something may or may not be read as the copyright sign followed by text, depending on the next character. Always write the semicolon.
Double encoding. If you run text through an escaper twice, & becomes & and then &amp;. On the page the user sees the literal text &. This is the single most common entity bug. Encode once, at the boundary where data enters HTML.
Escaping for the wrong context. HTML escaping does not make a string safe inside a JavaScript block, a URL, or a CSS value. Each context has its own rules. Using & inside a <script> body will not decode; it appears verbatim to the JavaScript engine.
Assuming a name exists. There is no named entity for most Unicode characters. The named list is curated and stops well short of the 149,000+ assigned code points. When no name exists, the numeric form is your only entity option.
Related tools
Once your text is safe, other references on this site cover the neighboring problems. For picking and checking the colors around that text, use the Color Converter & Contrast Checker. For visual CSS effects, see the CSS Gradient, Shadow & Radius Generator. When you escape characters into a query string, the URL Parser & Query Editor handles the different percent-encoding rules that apply there. To confirm which key produced a character in the first place, the Keycode Inspector shows the raw event values, and the MIME Type Lookup helps you set the charset-aware content type that makes literal UTF-8 characters safe.
Frequently asked questions
Do I need HTML entities if my page is UTF-8?
Only for & and < in text, plus the matching quote inside attributes. Every other character, including accented letters, emoji and symbols, can appear literally as long as the file is saved as UTF-8 and served with a matching charset.
What is the difference between and a regular space?
A regular space (code point 32) may collapse with adjacent spaces and may break onto a new line. A non-breaking space (code point 160, or  ) never collapses and keeps the words on either side on the same line.
Why does my ampersand show up as & on the page?
Your text was escaped twice. The single & became &, then that ampersand was escaped again into &amp;. Escape user data exactly once, at the point it is placed into HTML.
Is ' safe to use?
In HTML5 and XML, yes. In HTML 4 it was undefined and some old browsers show the literal text '. Use ' if you need to support those.
Which form is fastest for the browser?
The difference is not measurable in practice. All three forms are resolved during tokenizing before layout. Choose based on readability and portability, not speed.