Vaultools
Menu

HTML entity

An HTML entity, or character reference, is text like &amp;, &#8212; or &#x2014; that stands for a single character in HTML. It lets you write characters that would otherwise be read as markup, such as < and &, and characters that are hard to type or see.

Three ways to write a character

Every character reference starts with & and should end with ;. There are three forms, and all of them produce the same character:

FormExampleNotes
Named&mdash;Readable; 2,125 names exist in HTML5
Decimal&#8212;The Unicode code point in base 10
Hexadecimal&#x2014;The code point in base 16, matching U+2014

Numeric references work for any Unicode character and in XML as well. Named ones are specific to HTML: XML, and therefore SVG and XHTML served as XML, only defines &amp;, &lt;, &gt;, &quot; and &apos;.

When you need them

On a UTF-8 page you can type almost any character directly, so entities are mostly needed for the characters HTML would misread. In text between tags that is & and <. Inside a quoted attribute it is & and the quote character used around the value. Escaping all five of & < > " ' is the safe default for both. Entities are also handy for characters that are invisible or easy to lose in an editor, such as the non-breaking space &nbsp; and the soft hyphen &shy;.

Escaping for HTML doesn’t make a value safe everywhere on the page. A URL placed in href can still be javascript:…, and text inside <script> or <style> isn’t decoded as HTML at all. Those places need validation or their own escaping, such as percent-encoding for URL parts.

How browsers decode them

Browsers follow the HTML specification’s error-tolerant rules rather than rejecting bad references:

Common mistakes

References

Ads on this page

Non-personalized ads help keep Vaultools free — Google decides where they appear on the page.

Go Pro to remove them →