Frontend

HTML Entities Explained: When to Use <, >, & and Quotes

πŸ“… June 10, 2026⏱ 6 min read πŸ› οΈ Try the HTML Entity Encoder β†’

Ever tried to display <div> on a web page and watched it vanish β€” or worse, break your layout? That's HTML entities at work. This guide covers what they are, the 5 characters you must always escape, when encoding prevents XSS, and how to encode in JavaScript, Python and C#.

What Are HTML Entities?

An HTML entity is a piece of text that starts with an ampersand (&) and ends with a semicolon (;). Browsers replace the entity with the character it represents. Entities exist because some characters have special meaning in HTML β€” if you type them literally, the browser interprets them as markup instead of displaying them.

You write:      &lt;div&gt;
Browser shows:  <div>

There are two forms:

The 5 Characters You Must Always Escape

CharacterEntityWhy it's dangerous
<&lt;Starts an HTML tag
>&gt;Ends an HTML tag
&&amp;Starts an entity β€” unescaped, it corrupts text
"&quot;Breaks out of double-quoted attributes
'&#39;Breaks out of single-quoted attributes

Order matters when encoding manually: always escape & first. If you escape < to &lt; and then escape ampersands, you'll corrupt it into &amp;lt; β€” the classic double-encoding bug.

When Do Developers Actually Need This?

Entity Encoding vs Sanitization

These solve different problems and developers often confuse them:

Rule of thumb: encode by default; sanitize only when you deliberately want some HTML to render.

How to Encode HTML Entities in Code

JavaScript β€” the safest trick uses the DOM itself:

function encodeEntities(str) {
  const div = document.createElement('div');
  div.textContent = str;
  return div.innerHTML;
}
encodeEntities('<div>Tom & Jerry</div>');
// "&lt;div&gt;Tom &amp; Jerry&lt;/div&gt;"

In React, JSX escapes text content automatically β€” entities only matter when you use dangerouslySetInnerHTML (which is the hint to avoid it).

Python β€” built into the standard library:

import html
html.escape('<div>Tom & Jerry</div>')
# '&lt;div&gt;Tom &amp; Jerry&lt;/div&gt;'
html.unescape('&lt;div&gt;')   # '<div>'

C# β€” use System.Net.WebUtility:

using System.Net;
WebUtility.HtmlEncode("<div>Tom & Jerry</div>");
// "&lt;div&gt;Tom &amp; Jerry&lt;/div&gt;"
WebUtility.HtmlDecode("&lt;div&gt;");  // "<div>"

For quick one-off jobs β€” escaping a snippet for a blog post or fixing double-encoded text β€” skip the code entirely and use our free HTML Entity Encoder. It runs in your browser, so nothing you paste is uploaded anywhere.

Common Mistakes

Frequently Asked Questions

Do I need to escape characters like Γ© or δΈ­ as entities?
No β€” if your page is UTF-8 (it should be), you can write Unicode characters directly. Entities are only required for the 5 special characters, and optionally for characters you can't type.

Is &apos; safe to use for apostrophes?
It works in HTML5 and XML but not in old HTML4 parsers. &#39; works everywhere, which is why encoders prefer it.

Does entity encoding fully prevent XSS?
It prevents XSS in HTML body and quoted attribute contexts. Script blocks, event handlers, URLs and CSS need their own context-specific escaping β€” that's why frameworks handle it for you.

Why does my RSS feed or API show &amp; in titles?
The text was HTML-encoded before being placed in a context that doesn't render HTML. Decode it with the HTML Entity Decoder or your language's unescape function.

Encode or decode HTML entities instantly β€” free

Paste any text or code snippet and convert <, >, & and quotes to safe HTML entities β€” or decode them back. Runs 100% in your browser.

HTML Entity Encoder/Decoder β†’