Encoders

HTML Entities Guide: Named Entities, Decimal, Hex, and XSS Prevention

Complete guide to HTML entities: character encoding, named vs numeric entities, preventing Cross-Site Scripting (XSS), and typography symbols.

WebUtil Team

1. Why HTML Entities Exist: Escaping Reserved Markup Delimiters

HTML uses angle brackets (< and >) to denote element tags and ampersands (&) to denote character references. When web developers want to display a literal less-than or greater-than sign on a web page, typing < directly causes the browser's HTML parser to interpret the character as the beginning of a tag, corrupting the document layout.

HTML entities are standardized character references used to display reserved characters, invisible formatting spaces, and symbols outside the document's character encoding. Using entities guarantees that your content renders predictably across all operating systems, screen readers, and web browsers.

2. The Big Five Essential Entities and Their Encoding

  • &amp; -> & (Ampersand: the character reference prefix)
  • &lt; -> < (Less than: prevents tag injection)
  • &gt; -> > (Greater than: prevents tag closure injection)
  • &quot; -> " (Double quotation mark: prevents attribute breakout)
  • &apos; -> ' (Single quote / apostrophe)

In standard XML and strict XHTML documents, only these five entities are predefined. All other characters must be specified using numeric character references or declared in an explicit DTD.

3. Named Entities vs Decimal and Hexadecimal References

Characters can be represented in three distinct ways in HTML:

  • Named Entity: &copy; for the copyright symbol (easy for humans to remember).
  • Decimal Code: &#169; using the Unicode code point in base-10.
  • Hexadecimal Code: &#xA9; using the Unicode code point in base-16.

While named entities are popular for common symbols, numeric character references (decimal and hex) support every single character across the entire Unicode standard, including emojis (&#x1F680; for 🚀) and mathematical symbols (&#8734; for ∞).

Sponsored
Advertisement

4. Cross-Site Scripting (XSS) Prevention: Context-Aware Escaping

Failing to escape user-generated input before rendering it in HTML is the root cause of Cross-Site Scripting (XSS) vulnerabilities. If an attacker inputs <script>stealCookies()</script>, converting < to &lt; neutralizes the script, rendering it as harmless visible text.

Context matters: escaping for an HTML body (&lt;) is different from escaping inside an HTML attribute (where quotes must be escaped), which is different from escaping inside a <script> block where HTML entities are not parsed and can lead to code execution.

5. Encoding vs Decoding in Vanilla JavaScript and Frameworks

Modern frameworks like React and Vue automatically escape strings rendered inside JSX templates {userText}. When manipulating raw DOM elements in vanilla JavaScript, follow these rules:

```javascript // VULNERABLE TO XSS ATTACKS element.innerHTML = userComment;

// SECURE: Browser automatically encodes text to safe entities element.textContent = userComment;

// Helper to encode arbitrary HTML entities in modern browser function escapeHtml(str) { const div = document.createElement('div'); div.textContent = str; return div.innerHTML; } ```

6. Handling Typography, Whitespace, and Symbols

HTML entities also control micro-typography:

  • Non-breaking space (&nbsp;): Prevents an automatic line break between two words (e.g. '10&nbsp;USD' or 'Chapter&nbsp;4').
  • Em dash (&mdash;) and En dash (&ndash;): Used for grammatical parenthetical statements and number ranges (1990&ndash;2026).
  • Quotation marks (&ldquo; and &rdquo;): Curly smart quotes for professional typography.

WebUtil's HTML Entities Encoder allows you to encode and decode entire documents and text snippets with 1-click presets for minimal escaping, named entities, or full hexadecimal references.