The characters you must escape in HTML are < and & in normal text, plus the quote character that wraps an attribute value. In practice, most encoders escape five: & as &, < as <, > as >, " as " and ' as '. That covers text and quoted attributes, but not scripts, styles or URLs, which follow their own rules.
The five escape characters in HTML at a glance
The five escape characters in HTML are &, <, >, " and ', and the OWASP XSS prevention rules list exactly these for HTML entity encoding. Not all five are required everywhere, though.
| Character | Entity | Required in text? | Required in a quoted attribute? |
|---|---|---|---|
& |
& |
Only before something that looks like a reference | Same as text |
< |
< |
Yes | No |
> |
> |
No | No |
" |
" |
No | Yes, inside "..." |
' |
' |
No | Yes, inside '...' |
Everything else, including spaces, accented letters and emoji, can stay as-is in a UTF-8 page. You don't need é for é.
Escaping in normal text between tags
Text between tags needs only two characters escaped: the less-than sign and some ampersands. The HTML syntax rules say normal element text must not contain < or an ambiguous ampersand.
An ambiguous ampersand is an &, then letters or digits, then a ;, where the name isn't a real entity. So AT&T is fine as written. © is not text at all: the browser turns it into ©.
Escape every & as &, and you never have to think about which ones could start a reference. A bare & followed by a space is valid, but escaping it costs nothing.
The > sign has no special meaning in text, so 5 > 3 renders correctly unescaped. Most encoders escape it anyway, which is harmless.
Escaping inside attribute values
Attribute values need the quote that wraps them escaped, and the spec forbids the ambiguous ampersand there too. A double-quoted value can't contain a literal ", and a single-quoted value can't contain a literal '.
Unquoted values are where the list grows. The spec says an unquoted value must not contain whitespace, ", ', =, <, > or the backtick. One 2021 analysis of the parser rules reaches the same result: > only has to be escaped in an unquoted attribute.
A title attribute holding both quote types:
<p title="She said "it's fine"">Hover me</p>
The double quotes are escaped because they wrap the value. The apostrophe in it's stays as it is. OWASP's advice is to always quote attribute values, which removes the whole unquoted list from your worries.
Which characters to escape in each context
Context decides the rule, so this table maps the main places text can land in a page. It's built from the spec rules above and OWASP's per-context guidance.
| Where the text goes | What to escape | Notes |
|---|---|---|
| Text between tags | <, & |
Escaping > too is harmless |
| Double-quoted attribute | &, " |
Add < and > for safety |
| Single-quoted attribute | &, ' |
Same as above |
| Unquoted attribute | Whitespace, ", ', =, <, >, backtick |
Just quote the value instead |
<textarea>, <title> |
&, and any closing tag text |
Escaped like text |
<script>, <style> |
HTML entities don't work here | Use JavaScript or CSS encoding |
URL in href or src |
Percent-encode the value | Then HTML-encode the attribute |
The <script> and <style> row catches people out. The spec calls these raw text elements: entities inside them aren't decoded, so < in a script stays as four literal characters. The only rule is that the text must not contain </script or </style closing the element early.
<textarea> and <title> are escapable raw text elements. Tags inside them show as text, but entities still decode, so an ambiguous ampersand is still not allowed.
What browsers escape when they serialize HTML
The browser's own serializer is a good second opinion, because it's what you see when you read innerHTML. The serialization algorithm replaces &, <, > and the no-break space (U+00A0) as in text. In attributes it also replaces ". It never escapes the apostrophe, because it always writes attributes in double quotes.
The spec was updated on May 20, 2025 to also escape < and > inside attribute values. The Chrome team's post says the change shipped in Chrome 138, and names Firefox 140 and Safari 26 Beta as well.
Chrome's example:
<!-- your markup -->
<div data-content="<u>hello</u>"></div>
<!-- outerHTML in Chrome 138+ -->
<div data-content="<u>hello</u>"></div>
Only serialization changed. Parsing and getAttribute return the same value as before. The goal, per MDN's innerHTML page, is to block mutation XSS, where a value gets re-parsed as markup after a round trip.
If a snapshot test compares innerHTML strings, this is a likely reason it started failing in mid-2025.
Where HTML escaping is not enough
HTML entity escaping only protects text between tags and quoted attribute values. OWASP is explicit that attribute encoding alone doesn't protect JavaScript in an event handler like onclick.
Where it falls short:
- Inside
<script>: put data only inside a quoted JavaScript string, encoded for that context. Safer still, pass it through adata-attribute or JSON and read it from script. - CSS, in
<style>or astyleattribute: OWASP says not to put untrusted data directly into CSS at all. - In a URL: percent-encode the value with
%HH, then HTML-encode the attribute. My post on HTML encoding vs URL encoding walks through that order. - Inside an HTML comment: no encoding makes this safe.
OWASP's own summary is blunt: output encoding won't always prevent XSS.
My take on how much to escape
My take: escape all five characters everywhere text meets HTML, and always quote attributes. The strict minimum is smaller. But an encoder that handles five characters in every context is easy to check. One that tracks which quote style each template uses is not.
The limitation is that escaping is about context, not characters. A perfect five-character encoder still fails inside a <script> block or a javascript: URL. Most template engines escape text by default, so the real bugs usually come from the places they don't cover.
To check a string by hand, paste it into the HTML entity encoder, which runs in your browser and sends nothing to a server. Related rules live elsewhere: escaping double quotes in JSON has its own backslash syntax, and Markdown escape characters use a backslash too. If you minify pages, minifying HTML safely covers what whitespace can go.
FAQ
Do I need to escape the greater-than sign in HTML?
Not in normal text or quoted attributes, where > has no special meaning. It must be escaped only in unquoted attribute values. Escaping it everywhere is harmless and keeps your encoder simple.
Do spaces or accented letters need escaping in HTML?
No. In a UTF-8 page, spaces, accented letters and emoji can stay as they are. Spaces only cause trouble in unquoted attribute values, which you should avoid anyway.
Why does innerHTML show < inside my attributes?
Browsers changed how they serialize attributes in 2025. Chrome 138 and later turn < and > inside attribute values into < and > when you read innerHTML or outerHTML. The attribute's real value, from getAttribute, is unchanged.
Is escaping HTML enough to stop XSS?
Only for text between tags and quoted attribute values. Data inside a script block, an event handler, CSS or a URL needs the encoding for that context, and some places should never take untrusted data at all.