An HTML entity, or in the standard's own words a character reference, is a short code that a browser replaces with a character. Each one starts with & and ends with ;. There are three ways to write the same character: WHATWG
| Form | Example | Shows |
|---|---|---|
| Named | © |
© |
| Decimal number | © |
© |
| Hexadecimal number | © |
© |
The number is the character's Unicode code point, so any character has a numeric form. The standard's list of names has 2,231 entries, 106 of them old spellings without the semicolon. Names are case-sensitive: É is É and é is é.
When you need one
Mostly for characters that HTML would otherwise read as code. The W3C's advice is to always escape these three in content: W3C
<for<, which would otherwise start a tag. To show<p>as text on a page, write<p>.&for&, which would otherwise start an entity.>for>, for symmetry with<.
Inside an attribute value, you also need to escape the quote character that surrounds the value: " in a double-quoted value, ' or ' in a single-quoted one.
The second reason is characters you can't see or can't tell apart. A no-break space looks like any other space, so makes it visible in your source. The same goes for direction marks in Arabic or Hebrew text, like ‏.
When you don't
Nearly everything else. If your page is saved and served as UTF-8, and it should be, you can type ©, é, €, — and → straight into the HTML. The W3C recommends real characters over escapes for ordinary text, because escaped text is harder to read, harder to edit and larger. Imagine a paragraph of French or Czech with every accented letter written as a code. See character encoding if your page shows garbled characters instead.
Browsers turn every entity back into its character when they read the page, so café and café produce the same text. For search and readers alike, there's no benefit in using the entity.
The ones worth knowing
| Write | For | Why you'd use it |
|---|---|---|
< |
< |
Show markup or a "less than" as text |
& |
& |
Show an ampersand in text, like Tom & Jerry |
" |
" |
A double quote inside a double-quoted attribute |
|
no-break space | Keep "10 km" or "Fig. 3" on one line |
­ |
soft hyphen | Allow a long word to break at that point, with a hyphen |
©, —, € |
©, —, € | Only if you can't type the character itself |
Mistakes to avoid
- Encoding twice. If a system escapes text that's already escaped,
&becomes&amp;, and readers see&on the page instead of&. It tends to happen where text passes through more than one system, such as a CMS and a plugin that both escape it. Escape once, at the moment you write the text into HTML. - Leaving off the semicolon. Browsers accept the 106 old names without it, no others, and even those not in every position. Always end with
;. - Getting the case wrong.
©happens to work, but most names have one exact spelling;&Mdash;isn't an entity. - Using
for layout. A row of no-break spaces to indent text breaks on other screen sizes. Use CSS for spacing. - Printing user input unescaped. A comment or search term containing
<script>must be shown as text, not run. Escaping<,>,&and quotes is how that works, and good templating engines do it for you; don't switch it off. MDN
Not the same as URL encoding
Entities are for HTML text. Inside a web address, special characters use URL encoding instead, such as %20 for a space. In an href, you may need both: a URL-encoded address, and an & wherever an ampersand in its query string could be mistaken for an entity.