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.
Quick Answer
HTML entities let you display characters the browser would otherwise treat as markup, like
<, >, and &. The entity <
renders as <; & 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
<, and numeric references like <.
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: < for <,
> for >, & 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:
© for ©, — for the em dash (—), for a
non-breaking space. Numeric references use the Unicode code point: either decimal
(©) or hexadecimal (©), 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 ©, é, and
£ 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&, because it starts every entity.<becomes<, because it opens a tag.>becomes>, which is not strictly required in text but keeps the output symmetrical."becomes", which matters inside double-quoted attributes.'becomes', which matters inside single-quoted attributes.
The ampersand has to go first. Escape < first and you get <;
then the ampersand pass turns that into &lt;, and the page shows the
literal text < 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
<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<div class="container">. 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:
—(—) and–(–) for proper dashes instead of double hyphens.“and”for curly quotes. - Non-breaking spaces:
between a number and its unit keeps them on the same line:10 kg,§ 4.2. - Symbols:
©,®,™for © ® ™,€for €,°for °. - Math notation:
≠for ≠,≈for ≈,≤and≥for ≤ and ≥.
Common Mistakes
Related Tools
You May Also Need
You may also need
- URL Encoder and DecoderEscape the same string for a URL rather than markup
Next steps
- JSON Escape / UnescapeEscape it again for a JSON string literal
Alternatives
- Base64 Encoder and DecoderEncode binary data for transport instead