If you've ever pasted text into a CMS, built an HTML email, or hand-written a bit of markup and ended up with the literal text & showing on the page instead of a plain &, you've run into one of HTML's oldest quirks: a small set of characters aren't allowed to appear as themselves in HTML source, because the parser uses them for its own syntax.
Why HTML can't just let you type &
HTML uses < and > to mark the start and end of tags, and & to start something called a character reference (more commonly called an entity). If you type a raw & or < into your page's source, the browser's HTML parser tries to interpret it as the start of one of those constructs. Sometimes that fails safely and the character just disappears or gets mangled; sometimes it breaks the layout of everything after it, because the parser is now confused about where a tag starts or ends.
So HTML reserves a short list of characters: <, >, &, ", and '. Anywhere you want one of these to appear as a literal character in rendered text, you write it as an entity instead -- a short code the parser recognizes and converts back to the real character when it renders the page, instead of treating it as markup.
Named entities vs. numeric entities
There are two ways to write an entity, and both do the same job:
- Named entities are short mnemonic codes:
&for&,<for<,>for>,"for",'for'. These are the ones you'll see most often in hand-written HTML because they're readable. - Numeric entities reference a character by its Unicode code point:
&is also&,<is also<. Numeric entities matter because they cover characters that don't have a common named entity -- curly quotes, em dashes, accented letters, emoji -- anything outside basic ASCII can be written this way if you need to guarantee it survives whatever encoding a system uses along the way.
Both forms render identically in the browser. The only difference is whether a human reading the raw source can recognize what the character is at a glance.
Where this actually bites people
A few places this shows up constantly in practice:
- CMS and blog editors that store raw HTML. Write "Terms & Conditions" directly into a rich-text field that gets saved as HTML, and depending on the editor you can end up with either a correctly encoded
&(invisible to you, correct on the page) or a raw&that happens to render fine today but breaks the moment something after it looks like the start of an entity. - Email templates, where different email clients parse HTML with varying strictness, and a stray unescaped
&is far more likely to visibly break rendering than it is in a modern browser. - Generated HTML from templates or scripts, where a value pulled from a database or API -- a product name, a user's bio, a search query -- gets inserted into a page without being escaped first. This is also a security issue, not just a cosmetic one: unescaped
<and>in user-supplied text is the basic mechanism behind cross-site scripting (XSS), which is why every templating system worth using escapes these characters by default.
It's not the same thing as URL encoding
It's easy to conflate this with percent-encoding in URLs (where a space becomes %20), but they solve different problems in different places. HTML entities exist because of how the HTML parser reads markup; URL encoding exists because of which characters are legal inside a URL. A & inside a URL's query string is doing a completely different job (separating parameters) and has its own encoding rules. You can end up needing both at once -- for example, a URL containing an & that's embedded inside an href attribute in HTML -- which is exactly the kind of double-encoding mix-up that causes broken links.
How to actually fix it
The practical fix is almost never to memorize entity codes. If you're writing HTML by hand and need to include a literal &, <, >, ", or ' in text content, run it through an encoder rather than typing the entity yourself or guessing whether your editor handled it. If a page is showing you literal entity text like & instead of the symbol, that's the opposite problem -- the text got encoded once, then encoded again (or escaped and never decoded), and the fix is to decode it once properly rather than hand-editing the source.
The HTML Encoder / Decoder handles both directions -- paste text in and it'll either convert the reserved characters to safe entities, or take entity-laden text and turn it back into plain readable characters, so you don't have to guess which code maps to which symbol.