The ampersand is the hardest-working punctuation mark in HTML, and the easiest to write wrong. Type one into a page as ordinary text and the browser does not simply print it: it first checks whether the characters after it form an entity reference, a coded instruction such as & or ©. Most of the time the browser guesses kindly and your text survives. Sometimes it does not, and a company name, a URL or a line of documentation comes out missing pieces.
The rule that prevents all of this is short: when an ampersand is meant as a character in HTML text or in an attribute value, spell it &. This guide explains why, using the free HTML Entity Encoder/Decoder on Ovatly to do the conversion, and it is honest about where the bare character gets away with it and where it does not.
Contents
- Quick answer
- Why the ampersand is special in HTML
- Why bare ampersands so often survive
- Where the bare ampersand actually breaks
- Ampersands inside URLs and attributes
- How to encode text, step by step
- Worked example: encoding a tag as text
- Limitations and honest notes
- Common mistakes
- FAQs
- Conclusion
Quick answer
Write & wherever an ampersand should appear as a visible character inside HTML text or an attribute value. The bare & is the opening character of every entity reference, so leaving it bare asks the parser to guess whether you meant a character or a code. Paste your text into the HTML Entity Encoder/Decoder, press Encode entities, and the ampersands come out escaped, along with the other four characters the tool covers: less-than, greater-than, double quote and single quote.
Why the ampersand is special in HTML
HTML reserves a small set of characters for its own grammar. The less-than sign opens a tag, and the ampersand opens an entity reference: a named or numbered code that stands for a character the author cannot or should not type directly. When the parser meets &, it reads forward looking for the pattern of an entity, letters or digits ending in a semicolon.
That design is what makes entities useful. It is also what makes a bare ampersand ambiguous. In a sentence like Fish & Chips, a human reader never doubts the meaning, but the parser has to decide whether & starts a reference that happens to be malformed, or is simply a character that wandered into the markup unescaped. Spelling it & removes the decision entirely: the parser reads a complete, valid entity and prints an ampersand.
Why bare ampersands so often survive
If bare ampersands were reliably fatal, nobody would write them. Browsers are forgiving parsers, built to render decades of imperfect pages, and their recovery rules rescue most stray ampersands. An ampersand followed by a space is the classic survivor: no entity name contains a space, so the parser quickly concludes the character was meant literally and prints it. Whole websites run for years on this generosity.
Recovery is a safety net, not a specification. It varies with what follows the ampersand, and it is exactly why the mistake is so persistent: the page looks fine in the browser on the author's desk, so the invalid spelling ships, and it keeps shipping until the surrounding text changes into a shape the parser reads differently.
Where the bare ampersand actually breaks
The dangerous case is an ampersand followed by letters. Text such as a brand name, a product code or a query value can accidentally resemble the start of an entity, and the parser may consume characters trying to complete the reference, swallowing part of your text or printing something you never wrote. The failure is silent: no error banner, just subtly wrong output that nobody notices until a customer does.
Validators are the other place bare ampersands get caught. Run a page with unescaped ampersands through an HTML validator and it will flag them, because strictly speaking the markup is in error even when the browser recovers. That matters beyond tidiness: invalid markup behaves less predictably across browsers, assistive technology and the tools that scrape, index and re-publish your pages.
Ampersands inside URLs and attributes
The rule applies inside attribute values too, and URLs are where it bites most often. A link with two query parameters uses an ampersand as the separator, and in strict HTML source that separator is written as an entity:
<a href="search.php?q=fish&page=2">Results</a>
The browser decodes & back to a plain ampersand when it follows the link, so the address the server receives is unchanged. Writing the separator bare usually works in practice, for the recovery reasons above, but the escaped form is the correct source, and it is the form validators accept. The same discipline covers any attribute value containing an ampersand, and quotes matter there too: text inside an attribute should have its quote characters encoded, which the encoder handles in the same pass.
How to encode text, step by step
- Open the HTML Entity Encoder/Decoder and paste the text you want to convert.
- Press Encode entities to make it safe to display in HTML.
- Or press Decode entities to turn
<,&, numeric entities and common named entities back into characters. - Copy the result from the output box.
Worked example: encoding a tag as text
Suppose a documentation page needs to show a line of markup as readable text, ampersand included:
<a href="x">Tom & Jerry's</a>
Encoding it produces:
<a href="x">Tom & Jerry's</a>
Every reserved character has been converted: the angle brackets, both quote types, the apostrophe, and the ampersand in the middle, now spelled & so the parser prints it instead of hunting for an entity after it. The decoding direction is just as mechanical: decoding <p> & © gives back <p> & ©, the characters the entities stood for.
Limitations and honest notes
The encoder covers five characters: ampersand, less-than, greater-than, double quote and single quote. That set is the one that matters for correctness in text and attribute contexts, but it is deliberately narrow. The tool does not convert every special character in a document into a named entity, and on the decoding side, obscure named entities are left as they are, while common named entities, decimal entities such as © and hex entities such as © decode properly.
The most important limit is about expectations. Encoding is not a security filter on its own. It makes text parse correctly and display as intended; it does not make untrusted content safe to drop into a page. In a real application you still escape output in the right context, whether that context is an attribute, a script or a URL, using the mechanisms your platform provides.
Common mistakes
- Trusting browser recovery. A bare ampersand followed by a space survives today; the same habit breaks when letters follow, and validators flag it regardless.
- Forgetting attribute values. The rule is not only for visible text. Ampersands in
hrefvalues and other attributes need the entity spelling too. - Encoding twice. Encoding text that is already encoded turns
&into&amp;, and the reader sees the entity spelling printed on the page. Encode once, at the point where text becomes HTML. - Treating encoding as sanitizing. Escaping five characters does not neutralize untrusted input. Correct parsing and security are related but separate jobs.
- Decoding by hand. Turning entities back into characters manually invites missed cases. Use the Decode entities direction of the HTML Entity Encoder/Decoder and let it handle named, decimal and hex forms together.
FAQs
Why does my less-than sign show as an entity in the page source? Because that is the point: the entity form tells the browser to display a less-than sign as text instead of starting a tag. Your readers see the character; the browser sees an instruction to print it.
What is the difference between the numeric and named forms of the copyright entity? None in effect: both produce the copyright sign. The numeric form works everywhere; the named form is easier to read.
Should I encode quotes too? If the text goes inside an HTML attribute value, yes. The encoder covers both quote types, which keeps attribute contexts safe.
Which entities can the decoder turn back into characters?
Common named entities such as © and , decimal entities such as ©, and hex entities such as ©. Obscure named entities are left as they are.
Does encoding my text make it secure? No. Encoding is not a security filter on its own. In a real application you still escape output in the right context, whether that is an attribute, a script or a URL.
Conclusion
The ampersand rule is one line long: when it is a character, spell it &. Keep the HTML Entity Encoder/Decoder nearby for the conversion itself, in both directions, and your text will parse the same way in every browser and validator. The companion guide on dummy text mistakes that ship lorem ipsum to production covers the other classic almost-finished page problem, and the rest of this series lives in the Developer Tools guides archive.