A little help goes a long way. Find a tool/

Developer FIELD NOTES · 4 MIN READ

How HTML Entities Work

HTML entities explained: the three reference forms, the five characters you must escape, the ordering rule for encode and decode, and escaping as a safety tool.

HTML has a handful of characters with special jobs: < opens a tag, > closes one, & starts an escape, and quotes wrap attribute values. To display any of them as ordinary text — or to ship a character your keyboard does not have — HTML leans on entities: short escape sequences that stand in for literal characters.

Three ways to write one character

An entity starts with & and ends with ;, and comes in three forms:

&lt;      named     (a mnemonic name)
&#60;     decimal   (Unicode code point 60 = <)
&#x3C;    hex       (the same code point in hexadecimal)

All three render as <. Named entities cover the common cases — HTML5 defines around two thousand of them — but any character can be written numerically: the euro sign is &#8364;, a non-breaking space is &#160; (the famous &nbsp;), and the copyright sign is &#169; or &copy;. Numeric references always work; names are a convenience layered on top.

The five characters you must encode

Five characters may appear in raw text only as escapes: &lt; and &gt; for the angle brackets, &amp; for the ampersand itself, and &quot; and &#39; for the two quote characters. The apostrophe is usually written as the numeric &#39; because it works everywhere; the named form &apos; is valid HTML5 but was not part of HTML 4. An encoder tool produces these five and decodes the standard ones back — which sounds trivial until you meet the ordering rule:

encoding: escape &amp; FIRST  → "<" becomes &lt;, not &amp;lt;
decoding: unescape &amp; LAST  → "&amp;lt;" becomes "&lt;", not "<"

If < is escaped before &, the newly created &amp; sequences get escaped again and every &amp;lt; is double-escaped. Decoding has the mirror-image trap: unescape &amp; first and &amp;lt; collapses to < — text that was safely escaped once becomes live markup. Handling each direction in the right order is the entire correctness test for an entity encoder.

Escaping as a safety tool

The mechanism that displays < as text also defangs untrusted content. If a comment, search query, or form field is escaped before it is placed in a page, whatever tags it contains render as harmless text instead of live markup — the browser sees the entities and prints <script> rather than executing it. That is why everything that renders user-supplied text escapes it first. The same discipline protects converters: the Markdown-to-HTML pipeline described in Markdown Explained escapes <, >, and & before applying any Markdown rules, so pasted content can never smuggle raw HTML into the output. Code samples need it too: wrap them in <pre><code> and escape the brackets, or the browser will try to interpret the markup you meant to display.

Details worth knowing

Entity names are case-sensitive: &lt; is defined, &LT; is not — a few names like &AMP; have uppercase twins, but you cannot rely on it. Attribute values need the same escaping as element text, plus the quote character that delimits the attribute itself: inside title="..." a literal double quote would end the attribute early, which is the everyday job of &quot;. In plain element text, quotes are legal unescaped — encoding them anyway is harmless and keeps the habit uniform. Even spacing has an entity: &nbsp; is a space the renderer refuses to break a line at, which is why glued-together measurements like 10&nbsp;km never split across lines. Finally, note that modern pages are UTF-8, so most characters can be pasted directly into the source; entities remain essential for the five specials, for safety escaping, and for characters that are hard to type or hard to tell apart.

Try it yourself

Convert text to entities and back with the HTML Entity Encoder — try <b>, confirm you get &lt;b&gt;, then decode it again and watch the ordering rule hold. See what unescaped markup costs with the HTML to Text Converter, which strips tags and hands back clean plain text with line breaks preserved. And to audit a document's structure before or after conversion, the HTML Tag Extractor counts every tag and lists all attributes, classes, ids, and href/src values in the snippet.

Frequently asked questions

Which characters must be escaped in HTML?
Five: the less-than and greater-than signs (as &lt; and &gt;), the ampersand (as &amp;), and the two quotes (as &quot; and &#39;). Quotes only strictly need escaping inside attribute values, but encoding them everywhere is harmless and consistent.
What is the difference between named and numeric entities?
Named entities use a mnemonic (&amp;, &copy;); numeric ones give the Unicode code point in decimal (&#169;) or hexadecimal (&#xA9;). Both render the same character. Names cover the common cases; numerics work for every character in Unicode.
Why does the ampersand have to be escaped first?
Because escaping the others manufactures ampersands. If you turn < into &lt; before handling &, that fresh & gets escaped too, producing &amp;lt; — double-escaped text. Decoding mirrors the rule: unescape &amp; last, or &amp;lt; collapses into live markup.
Does escaping user input really prevent markup injection?
Yes — that is its core safety job. Escaped input renders as visible text: the browser prints <script> instead of executing it, because what arrived in the HTML was the harmless entity sequence, not real tags. Anything that displays user-supplied text should escape it first.
Are entity names case-sensitive?
Yes. &lt; is defined and &LT; is not; a few names such as &AMP; have uppercase twins, but you cannot rely on that. When in doubt, use the standard lowercase names or a numeric reference, which is always valid.