Glossary Development

HTML entity

An HTML entity is a code such as & or © that stands for a character that is reserved in HTML or hard to type.

Last reviewed:

AdSense Placeholder
Slot: header_reference_page

In more detail

An entity starts with & and ends with ;. It comes in three forms: a named entity such as &amp; or &copy;, a decimal numeric reference such as &#169;, and a hexadecimal one such as &#xA9;. All three are the same character here, the copyright sign, code point 169 (hex A9) in Unicode. &, < and > must be written as &amp;, &lt; and &gt; in text, and &quot; is needed for a double quote inside a double-quoted attribute. Escaping user input this way is the main defence against cross-site scripting (Cross-site scripting (XSS)). See the HTML Entities Cheat Sheet.

Why it matters

If a page prints user-supplied text without escaping it, the browser treats <script> as markup instead of text, which is exactly how injection happens. The opposite error is also common: escaping twice turns & into &amp;amp;, and visitors then see the literal text &amp;. Escape once, at output time, using the rule for the context (element text and attribute values differ from JavaScript or URLs).

Example

The text <a href="x">Tom & Jerry</a> is shown literally on a page when written as:

&lt;a href=&quot;x&quot;&gt;Tom &amp; Jerry&lt;/a&gt;

A visitor sees <a href="x">Tom & Jerry</a> as plain text and nothing is executed. Convert text either way with the HTML Entity Encode and HTML Entity Decode.

Common confusion

HTML entities are not URL percent-encoding: in a query string & is written %26, while in HTML text it is &amp;.

AdSense Placeholder
Slot: footer_leaderboard