Guide

HTML Entities: What to Escape and Why

The five characters to escape in HTML, why the ampersand comes first, when UTF-8 makes entities unnecessary, and how escaping stops XSS in text and attributes.

6 min read Updated Oct 2026

Quick Answer

HTML entities let you display characters the browser would otherwise treat as markup, like <, >, and &. The entity &lt; renders as <; &amp; renders as &.

To html encode text, replace reserved characters with their entity equivalents. To html decode or unescape html, do the reverse. You can also escape html before inserting user content into a page to prevent XSS. Entities come in two forms: named entity references like &lt;, and numeric references like &#60;.

If you need to encode or decode HTML entities right now, use the tool directly.

Try the HTML Entity Encoder and Decoder →

What Are HTML Entities?

HTML has structure. The < character opens a tag. The > closes it. The & starts a character reference. If you want to display any of those characters as content on the page (not as syntax) you need a way to say "I mean the literal character, not the markup."

HTML entities solve this. They're escape sequences: &lt; for <, &gt; for >, &amp; for &. The browser reads the entity, recognizes it as a character reference, and renders the actual character on screen.

Entities come in two forms. Named references use a human-readable name: &copy; for ©, &mdash; for the em dash (—), &nbsp; for a non-breaking space. Numeric references use the Unicode code point: either decimal (&#169;) or hexadecimal (&#xA9;), and work for any character, including ones that have no named form.

Why They Exist

HTML parsers treat angle brackets and ampersands as syntax. Write <b>Hello</b> and you get Hello. If you actually want the text <b>Hello</b> displayed on screen, the parser needs a different signal.

That's the core problem entities solve, and it predates the modern web. In the early days, pages often ran in ASCII or Latin-1 encoding. Characters outside those sets: copyright signs, accent marks, mathematical symbols, currency signs, couldn't always be typed or reliably stored in source files. Named entities like &copy;, &eacute;, and &pound; gave authors a portable way to include them.

Today UTF-8 is universal, so most of those named entities are unnecessary: you can paste the character directly into your HTML. But the four reserved characters: <, >, &, and " inside attributes: still require escaping regardless of encoding, because they carry structural meaning in the parser.

Which Characters Must You Escape in HTML?

Five characters carry meaning in HTML markup. This tool escapes exactly these five:

  • & becomes &amp;, because it starts every entity.
  • < becomes &lt;, because it opens a tag.
  • > becomes &gt;, which is not strictly required in text but keeps the output symmetrical.
  • " becomes &quot;, which matters inside double-quoted attributes.
  • ' becomes &#39;, which matters inside single-quoted attributes.

The ampersand has to go first. Escape < first and you get &lt;; then the ampersand pass turns that into &amp;lt;, and the page shows the literal text &lt; instead of a bracket. Everything else, from é to €, can stay as it is in a UTF-8 page.

Does Escaping Depend on Where the Text Goes?

Yes. Entity escaping is the right defence against XSS in two places and the wrong one in two others:

  • Text between tags: escape & and < at minimum. Entities are exactly right here.
  • Attribute values: always quote the attribute, then escape the quote character as well. An unquoted attribute can be broken out of with a single space.
  • URLs in href or src: percent-encode the parts with the URL Encoder, and refuse javascript: links. Entities alone do not make a URL safe.
  • Inside a <script> element: the browser does not decode entities there, so &lt; stays as four literal characters. Build the value with JSON encoding instead, using the JSON Escape tool, and make sure the text cannot contain </script>.

Most template engines escape text and attributes for you. The bugs come from the places they cannot see: hand-built strings, innerHTML, and values dropped into scripts.

When to Use HTML Entities

HTML entities show up in a handful of specific situations. For example:

  • Showing code examples: To display <div class="container"> on a web page, write it as &lt;div class=&quot;container&quot;&gt;. Without escaping, the browser renders the div, not its source.
  • User-generated content: Comments, usernames, forum posts, and search terms must be HTML-escaped before being placed in the page. A username like <script>alert(1)</script> should render as text, not execute.
  • Typography: &mdash; (—) and &ndash; (–) for proper dashes instead of double hyphens. &ldquo; and &rdquo; for curly quotes.
  • Non-breaking spaces: &nbsp; between a number and its unit keeps them on the same line: 10&nbsp;kg, §&nbsp;4.2.
  • Symbols: &copy;, &reg;, &trade; for © ® ™, &euro; for €, &deg; for °.
  • Math notation: &ne; for ≠, &asymp; for ≈, &le; and &ge; for ≤ and ≥.

Common Mistakes

You May Also Need

You may also need

Next steps

Alternatives

Continue Learning