How to Encode and Decode HTML Entities
HTML character references (usually called entities) let you write a character that would otherwise be read as markup. The ampersand & is written &, the less-than sign < as <, and a double quote inside an attribute as ". Getting this right is what lets a page show code samples, and it is the first line of defense against cross-site scripting (XSS).
Which Characters Must Be Encoded, and Where
It depends on context. In ordinary text content, escape < as < and & as &. Inside an attribute value, also escape the quote character you used to delimit it (" or '). > only strictly needs escaping in a few places, but encoding it as > is harmless and common.
Worked Example: What the Browser Does With Unescaped Text
We fed each string below to Chromium 141's HTML parser and read back the text it produced (script and results in our test folder, October 13, 2026):
| Written in the HTML | What the page shows | Why |
|---|---|---|
a < b and c > d | a < b and c > d | Kept: a < followed by a space is not a tag |
x<y and more | x | Lost: <y starts an element and swallows the rest |
AT&T | AT&T | Kept: &T is not a known reference |
© 2026 | Β© 2026 | Changed: legacy reference recognized without ; |
I¬it; is | IΒ¬it; is | Changed: ¬ matched inside ¬it; |
title="say "hi"" | say | Cut: the quote ended the attribute |
<img src=x onerror="β¦">hello | hello | Script ran (onerror fired) |
The parser is forgiving, which is the problem: most mistakes do not fail loudly, they quietly drop or change text β and the last row is an XSS hole. The legacy rows come from the HTML Standard itself: it defines 2,231 named character references, 106 of which are still recognized without a trailing semicolon for compatibility with old pages.
Named vs Numeric References
Named references use a mnemonic: &, <, >, ©, ™. Numeric references give the Unicode code point in decimal (©) or hex (©) and work for every character, including emoji (🚀 for π β one reference, not two surrogate halves). In a UTF-8 page you rarely need either for non-ASCII letters: write Γ© or π directly and only escape the markup characters.
Encoding for Security (XSS Prevention)
Never insert untrusted text into HTML without encoding it for the exact context: element content, attribute value, URL, JavaScript or CSS each need different handling, as the OWASP Cross-Site Scripting Prevention Cheat Sheet sets out. Most template engines encode element content and attributes automatically; the danger is in their "raw" or "unescaped" outputs and in client-side code that assigns to innerHTML. In the browser, prefer textContent for text; when you genuinely need to insert user-supplied HTML, sanitize it with a maintained library such as DOMPurify.
Using the HTML Entity Encoder
Our HTML Entity Encoder encodes text in named, decimal or hex form, iterating by code point so emoji become a single reference, and keeps ordinary spaces as spaces. Its decoder uses the browser's own HTML parser, so it understands all 2,231 named references and the legacy no-semicolon forms; in our tests it matched Python's html.unescape on 12 of 12 tricky strings.
Frequently Asked Questions
What are HTML entities?
& for an ampersand or < for a less-than sign. They let a page display characters the parser would otherwise treat as markup, and they can write any Unicode character by number, for example © for Β©.Which characters need HTML encoding?
< and &. In attribute values: & and the quote character that delimits the attribute. Encoding > as > is optional but common.Why is HTML encoding important for security?
<script> tags or HTML attributes could execute malicious code in other users' browsers.