HTML Entity

A character reference for representing special characters in HTML. It is a notation that starts with & and ends with ;.

An HTML entity is a character reference for safely representing special characters in an HTML document. If you write characters that have a special meaning in HTML syntax (<, >, &, and so on) as they are, the browser interprets them as tags or syntax, so you use entities to make explicit that they should be "displayed as characters." Typical examples include &amp; (&), &lt; (<), &gt; (>), &quot; ("), and &apos; (').

There are two kinds of HTML entities: named references and numeric references. Named references are a human-readable form such as &amp;, and as of August 2026 there are 2,231 named entities defined in the HTML specification. Of these, 106 are also defined in a form that omits the trailing semicolon, such as &AMP and &lt, and browsers interpret these for legacy compatibility. Note that if your own sanitizer assumes only "the form that starts with & and ends with ;," it will let these abbreviated forms slip through. Numeric references specify a Unicode code point directly in decimal (&#38;) or hexadecimal (&#x26;), and they can represent even characters for which no name is defined.

From a web security perspective, conversion to HTML entities (escaping) is the basis of protection against XSS (cross-site scripting). When you output user input into HTML, you must always convert the 5 characters <, >, &, ", and ' into entities. If you neglect this processing, there is a risk that malicious scripts will be injected. Many web frameworks provide automatic escaping in their template engines, but in situations where you use innerHTML or dangerouslySetInnerHTML, the developer needs to escape explicitly.

However, it is dangerous to think that "converting the 5 characters makes it safe." Escaping is effective only when the output destination is the body of the HTML or an attribute value enclosed in quotes. If you output an attribute value without enclosing it in quotes, an attribute boundary can be created with a space or a backquote, and attributes can be added even if the 5 characters are converted. Furthermore, inside <script>, in the URL part of href or src, and inside inline CSS, conversion to HTML entities is not a defense. Each of these requires separate processing, such as escaping as a JavaScript string or validating as a URL. Choosing the escaping method according to "which context you output into" is the practical criterion for judgment.

Entities commonly used in practice include the no-break space (&nbsp;), the copyright sign (&copy;), the trademark sign (&trade;), arrows (&rarr;), and mathematical symbols (&times;, &divide;). In particular, &nbsp; is frequently used when you want to display consecutive spaces or give content to an empty table cell. However, U+00A0, which &nbsp; expands to, is a different character from the ordinary half-width space (U+0020). In string comparison, "a b" and "a b" do not match, so if it slips into copied text it causes problems such as the text not being found by a search, or parsing failing when it gets mixed in as a digit separator in numbers. As its name says, it also prohibits line breaks, so if you use it heavily in long phrases, the text cannot wrap and overflows horizontally. If the purpose is to keep the height of empty cells, it is now more straightforward to handle it with CSS empty-cells or min-height.

A common misconception is that "if you use UTF-8, you don't need entities." It is true that with UTF-8 you can write Japanese and emoji as they are, but escaping the HTML syntax characters (<, >, &) is essential regardless of the character encoding. Double quotes (") inside HTML attribute values also need to be converted to &quot;.

From a character counting perspective, HTML entities create a gap between the character count in the source code and the character count displayed in the browser. &amp; is 5 characters in the source code, but in the browser it is displayed as the single character "&". &nbsp; turns 6 characters into a single space character. Because the results differ greatly between counting the characters of the HTML source and counting the characters of the text after rendering, it is important to be aware of which one a character counting tool targets.

Share this article