Named vs numeric HTML entities
An HTML entity is a way to write a character that would otherwise be interpreted as markup, or that is hard to type. The ampersand alone can be written three ways that all render as &: the named form &, the decimal form &, and the hexadecimal form &. They are interchangeable to the browser, but they are not equally safe or equally portable, so it is worth knowing when to reach for each.
The three forms
Every character reference starts with & and ends with ;.
- Named:
&,<,©. Readable, but only a fixed list of names exists. - Decimal numeric:
&,<,©. The code point written in base 10. - Hex numeric:
&,<,©. The same code point in base 16, marked byx.
& = & = & all render as &
© = © = © all render as ©
Numeric references can express any Unicode code point, so they never run out. Named references are limited to the characters that HTML defines a name for, which is a large set but a closed one.
The five you must escape
Most text needs no escaping at all. Only a handful of characters carry special meaning in HTML, and exactly which ones matter depends on context.
| Character | Entity | Why |
|---|---|---|
& | & | Starts every entity; escape first |
< | < | Opens a tag |
> | > | Closes a tag |
" | " | Ends a double quoted attribute |
' | ' | Ends a single quoted attribute |
In ordinary text content, only &, <, and > strictly need escaping. Inside an attribute value you must also escape whichever quote character delimits it. Always escape & before the others, or you risk double escaping the entities you just produced.
Note that ' has no universally safe named form. ' is defined in HTML5 and XML but was not in older HTML, so the numeric ' is the portable choice.
When numeric is safer
Reach for numeric references when:
- The character has no name, or the name is not reliable across parsers.
'for the apostrophe is the classic example. - You are producing XML or XHTML, which only predefines five names (
amp,lt,gt,quot,apos). Any other named entity, including and©, is undefined in plain XML and will cause a parse error. Numeric references always work. - A machine generates the output. A single escape function that always emits numeric references is simpler and less error prone than one that maintains a name table.
Named entities earn their place in hand written markup where — reads better than —.
Charset considerations
If your document is served and declared as UTF-8, which it should be, you often do not need entities for non ASCII characters at all. You can write © or € directly and the browser reads them correctly. Entities become necessary when you cannot trust the byte encoding end to end, such as data passing through a system that might mangle raw UTF-8, where a plain ASCII numeric reference survives untouched. Always declare the charset with <meta charset="utf-8"> so the browser interprets both your raw characters and your entities consistently.
To convert text in either direction, the HTML entities encoder and decoder runs entirely in your browser, so the content you paste, which may be private, never leaves your device. Use named entities for readability in markup you write by hand, and numeric entities when you need portability or are escaping programmatically, especially for XML. For the related safety topic, see when to escape HTML and XSS basics.