Skip to content
uencoder

HTML Entities

HTML entities escape characters with special meaning in HTML (<, >, &, ") into &name; or &#NNN; form so they display literally instead of being parsed as markup.

Purpose

They keep user input or dynamic content from being executed by the browser as HTML tags, and are a foundational defense against XSS.

Mechanism

Named entities such as &lt; &gt; &amp; &quot; &apos; cover the 5 XML-predefined characters. Numeric entities &#NNN; use a decimal code point, and &#xNNN; a hexadecimal one. Decoding expands them according to the DTD entity table.

Use cases

  • Displaying < > & as text in HTML
  • Output escaping for XSS defense
  • Special symbols in HTML email
  • XML content escaping

Examples

  • Input
    <script>
    Output
    &lt;script&gt;

    After escaping, the browser shows text instead of executing a script

Common errors

Escaping only < and > while missing quotes, enabling XSS in attribute contexts
Escape by context: text needs < > &; attributes additionally need " and '

Related terms

Related tools

References