ContextCleaner: see every entry in the Windows 11 right-click menu, and which app added it — on the Microsoft Store.
Engineering PrismKit

Which Characters Need Escaping in HTML? HTML Entities Explained for Text and Attributes

Published October 23, 2026 Updated October 23, 2026 8 min read

In HTML text, escape < as &lt; and & as &amp;; escaping > as &gt; is not strictly required but is standard practice. Inside an attribute value, also escape the quote character you used around the value: " as &quot;, or ' as &#x27; or &#39;. When you need to do it by hand, for a snippet in documentation or a value in a template, PrismKit’s HTML entities encoder and decoder runs offline on Windows from a keyboard palette, so the text never goes into a web converter.

The rules below come from the WHATWG HTML standard and the OWASP Cross Site Scripting Prevention Cheat Sheet.

Encode and decode HTML entities offline, from a hotkey, without a browser tab.

Get it from Microsoft

Which characters must be escaped in HTML text?

The HTML standard says that the text of a normal element must not contain the character < or an “ambiguous ampersand”. In practice that gives two must-escape characters and one should-escape character:

CharacterEntityWhy
<&lt;Starts a tag; the standard forbids it in normal text
&&amp;Starts a character reference
>&gt;Not required in text, but escaped by convention and by the standard’s own serializer

Every other character, including accented letters, emoji and symbols, can be written directly if the page is served as UTF-8. You do not need &eacute; for é.

What is an ambiguous ampersand?

The WHATWG standard defines an ambiguous ampersand as an & followed by one or more letters or digits and then a ;, where that sequence does not match a named character reference. So &foo; is an error, while & followed by a space is technically allowed.

That subtlety is why the safe rule is simply to escape every & as &amp;. The standard’s list of named references also keeps some legacy names that are recognized without a semicolon, such as &copy, so a pasted URL containing &copy=1 can display with a © sign in body text. A product name like R&D; is an ambiguous ampersand and a conformance error. Writing &amp; every time removes the guesswork.

What are the three kinds of character references?

The standard allows three forms, all starting with & and ending with ;:

KindExample for <Example for "
Named&lt;&quot;
Decimal&#60;&#34;
Hexadecimal&#x3C;&#x22;

Named references are case-sensitive: &Eacute; and &eacute; are different characters. Numeric references work for any character, which is why the single quote is often written &#x27; or &#39;.

Which characters need escaping inside attribute values?

Attribute values have one more character to worry about: the quote that surrounds the value. The standard says a double-quoted value must not contain a literal ", and a single-quoted value must not contain a literal '. So:

<input value="She said &quot;yes&quot; &amp; left">
<a title='It&#x27;s fine'>Link</a>

Unquoted attribute values are far stricter. The standard forbids whitespace, ", ', =, <, > and the backtick in them. OWASP’s advice is simply to always quote attribute values, with double or single quotation marks, and use an attribute encoder for what goes inside.

How does the HTML standard itself escape text?

The WHATWG parsing chapter includes an algorithm for escaping a string when HTML is serialized. It replaces:

  • & with &amp;
  • a no-break space with &nbsp;
  • < with &lt; and > with &gt;
  • in attribute mode, " with &quot;

That is a useful reference for what a correct encoder outputs, though for security OWASP recommends also encoding ' in attribute contexts.

Format HTML, encode entities and test a regex from one offline palette.

Get it from Microsoft

Does escaping HTML prevent XSS?

It prevents it in the two contexts it was designed for: text between tags and ordinary quoted attributes such as title or value. For those, OWASP’s cheat sheet lists HTML entity encoding of &, <, >, " and ' as the control.

It does not make other contexts safe:

  • Event-handler attributes. OWASP warns that HTML attribute encoding alone does not protect JavaScript in an attribute like onclick, because the parser decodes the references before the script runs.
  • URLs, CSS and script blocks. Each needs its own encoding or validation. Our guide to URL encoding, %20 versus + covers the URL side.
  • Raw HTML sinks. Assigning untrusted text to innerHTML parses it as markup.

The cheat sheet also points the other way: when you set text from JavaScript with textContent, or set an ordinary attribute with setAttribute, the browser does not parse HTML in the value. OWASP says to pass the original text without pre-encoding it, otherwise users see &amp; on screen. The same applies to most template engines that escape automatically: encoding by hand on top of them double-encodes.

How do you show HTML code on a web page?

To display markup as text, for example in a tutorial, escape it so the browser does not parse it. Each < becomes &lt; and each & becomes &amp;, then the result goes inside a pre and code element. A snippet such as <b>Bold</b> is written in the page source as &lt;b&gt;Bold&lt;/b&gt;, and the reader sees the original tags.

The pre element keeps line breaks and spacing, but it does not stop parsing. Tags inside it are still tags, which is why the escaping step cannot be skipped.

What are the common mistakes when escaping by hand?

MistakeWhat happensFix
Escaping < but not &Existing entities in the text, such as &lt; in a code sample, display as < instead of as writtenEscape & first, then < and >
Leaving quotes unescaped in attributesThe value ends early at the first matching quoteEscape " or ' to match the quoting
Unquoted attribute valuesA space or > in the value breaks the attributeAlways quote attribute values
Using entities in a URL&amp; is correct inside an HTML href, but a URL typed into a browser needs percent encodingUse URL encoding for URL parts, entity encoding for the HTML around them

The order matters in the first row. If you replace < with &lt; first and then replace &, you turn your own &lt; into &amp;lt;.

What does double encoding look like?

Double encoding happens when already-escaped text is escaped again:

StepText
OriginalFish & Chips
Escaped onceFish &amp; Chips
Escaped twiceFish &amp;amp; Chips

The page then shows Fish &amp; Chips to the reader. The fix is to escape once, at output, in the layer that knows the context. Decoding the text to inspect it, then encoding once, is the quickest way to repair a value that has been through too many layers.

How do you encode or decode entities quickly?

For one-off work, such as preparing an HTML snippet for documentation, checking what a stored value really contains, or cleaning up a double-encoded string, an encoder is faster than doing it by eye.

PrismKit is a free, keyboard-first utility belt for Windows that sits in the background until you call it with a hotkey. Its Encoders & Decoders include Base64, URL, JWT inspection and HTML entities, and its formatters cover JSON, XML, SQL and HTML. It runs entirely offline, so the text you paste, which may include tokens or customer data, never leaves your PC. There is no tracking or telemetry.

Decode first if you are not sure what you have, then encode once. Check that the output escapes quotes when the value is going into an attribute. For the related Base64 and URL jobs, see our page on encoding and decoding Base64, URL and JSON.

Frequently asked questions

Do I need to escape the greater-than sign?

In text, the HTML standard does not require it, but escaping > as &gt; is common practice and harmless.

Should I escape non-English characters?

No, if your page is UTF-8. Write them directly. Entities for letters like é are optional.

What is the entity for a non-breaking space?

&nbsp;, or numerically &#xA0;.

Is &apos; valid HTML?

It is a named reference in HTML5, but &#x27; or &#39; is the widely used choice, and it is what OWASP lists.

Try PrismKit

PrismKit is free in the Microsoft Store, with optional one-time expansion packs. Install it, set your hotkey, and encode your next snippet without opening a browser tab. More detail is on the PrismKit product page.

Keep everyday developer tools a hotkey away, offline, with a free core set.

Get it from Microsoft

Last updated 23 October 2026. Published by Automata Labs, a software company in Canada.

// release_radar

Get notified about new releases.

We build native, local-first tools for professionals who refuse SaaS fatigue. Drop your email to hear about new apps, major updates, and zero-telemetry releases as they land on the Microsoft Store. No spam — just release news.