DeveloperMar 22, 2026 Β· Updated Oct 13, 2026 Β· 3 min read

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 &amp;, the less-than sign < as &lt;, and a double quote inside an attribute as &quot;. Getting this right is what lets a page show code samples, and it is the first line of defense against cross-site scripting (XSS).

πŸ”£
Try the HTML Entity Encoder
Free, no signup
β†’
DG
Derek Giordano
Designer & Developer
In this guide
01Which Characters Must Be Encoded, and Where02Worked Example: What the Browser Does With Unescaped Text03Named vs Numeric References04Encoding for Security (XSS Prevention)05Using the HTML Entity Encoder

Which Characters Must Be Encoded, and Where

It depends on context. In ordinary text content, escape < as &lt; and & as &amp;. Inside an attribute value, also escape the quote character you used to delimit it (&quot; or &#39;). > only strictly needs escaping in a few places, but encoding it as &gt; 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 HTMLWhat the page showsWhy
a < b and c > da < b and c > dKept: a < followed by a space is not a tag
x<y and morexLost: <y starts an element and swallows the rest
AT&TAT&TKept: &T is not a known reference
&copy 2026Β© 2026Changed: legacy reference recognized without ;
I&notit; isI¬it; isChanged: &not matched inside &notit;
title="say "hi""say Cut: the quote ended the attribute
<img src=x onerror="…">hellohelloScript 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: &amp;, &lt;, &gt;, &copy;, &trade;. Numeric references give the Unicode code point in decimal (&#169;) or hex (&#xA9;) and work for every character, including emoji (&#x1F680; 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?+
HTML character references, often called entities, are codes such as &amp; for an ampersand or &lt; 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 &#xA9; for Β©.
Which characters need HTML encoding?+
In element content: < and &. In attribute values: & and the quote character that delimits the attribute. Encoding > as &gt; is optional but common.
Why is HTML encoding important for security?+
Encoding prevents cross-site scripting (XSS) attacks. Without encoding, user-supplied content containing <script> tags or HTML attributes could execute malicious code in other users' browsers.
Try it yourself

Use the HTML Entity Encoder β€” free, no signup required.

⚑ Open HTML Entity Encoder
DG
Derek Giordano
Written by the creator of Ultimate Design Tools. BA in Business Marketing.
πŸ“š References & Further Reading
Browse all utility tools β†’
⚑Code Beautifier - CSS, HTML & JS Formatterβ†’βš‘CSS / JS / HTML Minifier | Ultimate Design Toolsβ†’βš‘HTML Entity Encoder / Decoder | Ultimate Design Toolsβ†’βš‘Free HTML Formatter & Beautifierβ†’
DeveloperHow to Encode and Decode Base64 (With Examples)→DeveloperHow to URL Encode and Decode Strings→DeveloperHow to Convert HTML to JSX: The Complete Developer's Guide→DeveloperHow to Convert HTML to Markdown→DeveloperHow to Convert an Image to Base64 for CSS and HTML→DeveloperHow to Convert Markdown to HTML (and Back)→DeveloperHow to Decode a JWT Token (Without the Secret)→GuideBase64, HTML Entities & Encoding for Developers (2026)→
πŸ”£
Try HTML Entity Encoder
Free, no signup →