How to Escape HTML

Escaping HTML means turning the characters a browser would read as code — angle brackets, quotes, ampersands — into entities the browser displays as text instead. It is the step that lets you show a code example on a page without the browser swallowing it, and it takes one click.

Why HTML needs escaping

Angle brackets are tags to a browser

A browser reads angle brackets as the start of a tag, so if you want to show the text <div> on a web page, you cannot write it as-is — the browser will treat it as a real div and show nothing. Escaping replaces the five characters that have special meaning in HTML — the ampersand, less-than, greater-than, double quote and single quote — with entities the browser displays as text instead of interpreting as code.

Code examples vanish without escaping

The classic case is a tutorial page that shows code examples. Every tag in the example must be escaped, or the example vanishes into the page and may even break the layout around it. The same applies to user comments, forum posts and any place where text typed by a person is shown on a page: escaping is what keeps a typed <script> tag harmless.

The five entities

The five entities to know

The ampersand becomes &amp;, the less-than sign becomes &lt;, the greater-than sign becomes &gt;, the double quote becomes &quot;, and the single quote becomes &#39;. The ampersand is always escaped first, because the other entities contain ampersands themselves — escape it last and you would double-escape everything into &amp;lt; and similar nonsense.

Unescaping: the browser decodes them all

Unescaping works the other way: the browser itself decodes the entities, so every named entity and numeric character reference is handled, not just the five this tool writes. That means &copy; becomes the copyright sign and &#169; does too. If your text contains an entity the browser does not recognise, it is left as-is rather than guessed.

Escape HTML in four steps

  1. Copy your code or text. Select the HTML or text you want to escape — a code example, a snippet, a comment — and copy it.
  2. Paste it into the tool. Open the HTML Escape and Unescape tool and paste your text into the Input box. Nothing is uploaded; the conversion happens in your browser.
  3. Click Escape HTML. Angle brackets, quotes and ampersands become entities. The result appears on the right, ready to paste into your page.
  4. Copy the result. Click Copy output or Download, and paste the escaped text wherever the code needs to be shown as text.

Named entities and numeric references

Named entities and numeric references do the same job in different ways. A named entity like &copy; is easy to remember, while a numeric reference like &#169; works for any character, including ones with no name at all. Older HTML documents used numeric references heavily, and you will still meet them in content copied from old sites or exported from databases.

Both forms display identically, so when you unescape text you do not need to care which form it used — the result is the same character. When you escape text yourself, the named forms are easier to read in the source, which is why this tool writes them.

Hexadecimal numeric references exist too: &#x3C; is the same less-than sign written in hex. You will meet them in content exported from some systems, and the browser decodes them exactly like the decimal form.

Escaping is not URL encoding

HTML escaping and URL encoding solve different problems and are not interchangeable. Escaping makes text safe to show inside a page; URL encoding makes text safe to put inside a web address, turning spaces into %20 and slashes into %2F. Using the wrong one produces broken links or visible percent codes in your text.

For links, use the URL and HTML Encoder, which handles both. For displaying code or user text on a page, this tool is the right one. And remember that escaping is a display step, not a complete security product: a website that takes user input still needs proper handling on the server side, but escaping is the first and most important layer.

Related guides

Questions people ask

Which characters does escaping change?

Five: the ampersand becomes the amp entity, less-than becomes lt, greater-than becomes gt, the double quote becomes quot, and the single quote becomes the numeric entity 39.

Why is the ampersand escaped first?

Because every entity starts with an ampersand. Escape it last and the other entities get escaped again, turning lt into amp lt, which displays as text instead of a less-than sign.

Can I unescape entities this tool did not write?

Yes. Unescaping uses the browser decoder, so named entities like copy and numeric references like 169 are all decoded correctly.

Is escaping enough to make user input safe on my website?

It is the essential first layer, but a real website also needs server-side handling. Escaping stops markup from breaking your page; it is not a complete security solution on its own.