Skip to content
T
Tools.Town
Free Online Tools for Everyone
Developer Tools

HTML Entities Explained: When and How to Escape Characters

A practical guide to HTML entities — which characters to escape, named vs numeric entities, how decoding works, and why escaping prevents broken markup and XSS.

24 June 2026 4 min read By Tools.Town Team Fact Checked

Key Takeaways

  • Only when the character should appear literally as text rather than start a tag
  • For rendering, yes — © and © produce the same character
  • It is a critical part of the defence but not the whole story

Why a single character can break a page

HTML is built from a handful of characters that mean something special to the parser. The moment the browser sees a <, it starts looking for a tag name. The moment it sees an &, it starts looking for an entity. When those characters appear in your content rather than your markup, the parser misreads them — and your page renders wrong, or worse, runs code you never intended. Escaping is how we tell the browser “treat this as literal text, not as syntax.” The HTML Entity Encoder does this conversion instantly, in both directions, so you can see exactly what safe output looks like.

The five characters that matter most

Almost every escaping bug traces back to the same small set:

  • & becomes &amp; — the ampersand starts every entity, so it must be escaped first.
  • < becomes &lt; — otherwise the browser thinks a tag is beginning.
  • > becomes &gt; — escaped for symmetry and to avoid edge cases in some parsers.
  • " becomes &quot; — critical inside double-quoted attribute values.
  • ' becomes &#39; (or &apos;) — critical inside single-quoted attribute values.

If you escape only these five, you have covered the vast majority of real-world cases. Paste a snippet containing all of them into the HTML Entity Encoder and watch each one transform — the tool always handles the ampersand first so nothing double-encodes.

Why ampersand-first ordering matters

Imagine you escaped the angle brackets before the ampersands. Your < turns into &lt;. Then a second escaping pass sees the new ampersand in &lt; and turns it into &amp;lt;. Now the browser renders the literal text &lt; instead of a <. This double-encoding bug is subtle and common in hand-rolled escaping. The fix is simple but easy to forget: always replace & first, then the other characters. A correct encoder bakes this ordering in.

Named versus numeric entities

There are two ways to write an entity:

A named entity uses a keyword, like &copy; for the copyright sign or &mdash; for an em dash. Names are readable and self-documenting, but only a fixed list of characters has one.

A numeric entity references a character by its Unicode code point. You can write it in decimal — &#169; — or hexadecimal — &#xA9;. Numeric entities work for any character, including symbols, accented letters, and emoji that have no name. They are the universal fallback, which is why the encoder’s “all non-ASCII” mode emits numeric entities for everything outside plain ASCII.

Both forms render identically. Choosing between them is a question of readability versus universality.

Decoding: going the other way

Decoding is the reverse operation, and you need it more often than you would expect — debugging a log line full of &amp;amp;, cleaning data scraped from a feed, or turning an entity-encoded email body back into plain text. A decoder matches every &…; sequence and resolves three kinds at once: named entities from a lookup table, decimal references, and hexadecimal references. Unknown sequences are left untouched so you never lose data. Drop a tangled string into the decode panel of the HTML Entity Encoder and it untangles named, decimal, and hex entities in a single pass.

Encoding is a security control, not just a formatting one

The most important reason to escape is safety. Cross-site scripting (XSS) happens when untrusted text is inserted into a page and the browser executes part of it as markup or script. If a comment field contains <script>…</script> and you render it raw, that script runs for every visitor. Escaping the angle brackets turns the payload into inert text that displays harmlessly. This is why frameworks escape by default and why you should never concatenate user input into HTML without encoding it.

That said, HTML entity encoding solves the HTML context. Data that lands inside a <script> block, a URL, or a style attribute needs its own escaping rules — JavaScript string escaping, URL percent-encoding, and so on. For URLs specifically, reach for the URL Encoder rather than HTML entities. Think of entity encoding as the baseline you apply everywhere text meets markup, then layer context-specific escaping on top.

A practical workflow

When you are handling text that will end up in a page, a useful habit is: decide the context, escape for that context, and verify. For HTML content and attributes, run the text through an entity encoder. For a quick sanity check on something you have received, decode it and confirm it says what you expect. If you only need to remove markup rather than escape it — for example to extract plain text from a snippet of HTML — the HTML Stripper is the better fit. And when you want to understand a specific entity, the reference table in the HTML Entity Encoder lists the most common ones side by side.

Common mistakes worth avoiding

A few recurring errors cause most entity-related bugs, and knowing them saves hours of debugging. The first is encoding twice. If text passes through two layers that each escape it — a template engine and then a manual call, say — you end up with &amp;amp; rendered literally on the page. The fix is to escape exactly once, at the boundary where untrusted text enters HTML, and never again. The HTML Entity Encoder makes double-encoding visible instantly: encode a string twice and you can watch the ampersands multiply.

The second mistake is escaping in the wrong context. HTML entity encoding is correct for HTML text and attributes, but it does nothing useful inside a <script> block, a URL, or a CSS value. Putting &amp; inside a JavaScript string literal just produces the literal characters &amp;, not an ampersand. Each context has its own escaping rules, and reaching for HTML entities everywhere is a classic source of subtle breakage.

The third is forgetting attributes. Developers often escape the angle brackets in text content but forget that attribute values need the quote characters escaped too. An unescaped double quote inside a double-quoted attribute ends the attribute early and can let an attacker inject new attributes. Whenever you build an attribute from untrusted data, escape the quotes as well as the ampersand.

Finally, watch for whitespace entities. A &nbsp; looks like a space but is a non-breaking space (U+00A0), which behaves differently in layout and breaks naive word-splitting. When you decode entities, be aware that some “spaces” in your output are not ordinary spaces — a detail that matters when the text feeds a parser or a search index downstream.

Key takeaways

Escape the five special characters whenever text meets markup, always handle the ampersand first to avoid double-encoding, prefer named entities for readability and numeric entities for anything without a name, and remember that decoding is just as useful as encoding when you are debugging. Most of all, treat encoding as a security measure: it is the simplest, most reliable way to keep untrusted input from becoming executable markup. For more on related conversions, see our guide to CSS gradients and visual tooling, part of the same developer-tools series.

Advertisement

Try HTML Entity Encoder — Free

Apply what you just learned with our free tool. No sign-up required.

Try HTML Entity Encoder

Frequently Asked Questions

Do I need to escape every angle bracket I write?
Only when the character should appear literally as text rather than start a tag. Inside ordinary HTML content, a stray < can begin an unintended tag, so escaping it to &lt; is the safe choice. In code you control you may not need to, but any text coming from users or external data should always be escaped.
Are named and numeric entities interchangeable?
For rendering, yes — &copy; and &#169; produce the same character. Named entities are more readable, but only a limited set of characters have names. Numeric entities can represent any Unicode code point, which is why they are the fallback for symbols and emoji.
Is HTML entity encoding enough to stop XSS on its own?
It is a critical part of the defence but not the whole story. Encoding for HTML text and attributes stops markup injection, yet JavaScript, URL, and CSS contexts each need their own escaping rules. Treat entity encoding as the baseline, then apply context-appropriate escaping everywhere untrusted data lands.

Was this guide helpful?

Your feedback helps us improve our content.

Get the best Developer Tools tips & guides in your inbox

Join 25,000+ users who get our weekly developer tools insights.