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.
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
- & -> & (Ampersand: the character reference prefix)
- < -> < (Less than: prevents tag injection)
- > -> > (Greater than: prevents tag closure injection)
- " -> " (Double quotation mark: prevents attribute breakout)
- ' -> ' (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: © for the copyright symbol (easy for humans to remember).
- Decimal Code: © using the Unicode code point in base-10.
- Hexadecimal Code: © 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 (🚀 for 🚀) and mathematical symbols (∞ for ∞).
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 < neutralizes the script, rendering it as harmless visible text.
Context matters: escaping for an HTML body (<) 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 ( ): Prevents an automatic line break between two words (e.g. '10 USD' or 'Chapter 4').
- Em dash (—) and En dash (–): Used for grammatical parenthetical statements and number ranges (1990–2026).
- Quotation marks (“ and ”): 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.
Recommended Articles & Guides
How to Format JSON Properly: A Complete Guide
Learn how to format JSON data correctly with best practices, common patterns, and troubleshooting tips for developers.
Common JSON Errors and How to Fix Them
A comprehensive guide to the most frequent JSON errors developers encounter and step-by-step solutions to fix them.
URL Encoding and Decoding: What Every Developer Should Know
Understand how URL encoding works, why it matters for web development, and how to properly encode and decode URLs.