HTML entities are essential for web developers. Whether you're displaying special characters, preventing XSS attacks, or working with multilingual content, understanding HTML entities is crucial.
This guide covers everything you need to know about HTML entities โ what they are, when to use them, and a complete reference of common entities.
โก Encode HTML Entities Instantly
Convert special characters to HTML entities and vice versa.
Open HTML Encoder โ๐ค What Are HTML Entities?
HTML entities are character sequences that represent special characters in HTML. They start with & and end with ;.
Why Do We Need Them?
- Reserved characters:
<and>are used for tags - Special characters:
&starts an entity - Unprintable characters: Non-breaking space, etc.
- Security: Prevent XSS attacks by escaping user input
- International characters: Display any Unicode character
๐ Types of HTML Entities
1. Named Entities
< โ < (less than)
> โ > (greater than)
& โ & (ampersand)
" โ " (double quote)
' โ ' (apostrophe)
โ (non-breaking space)
© โ ยฉ (copyright)
® โ ยฎ (registered trademark)
™ โ โข (trademark)
2. Numeric Entities (Decimal)
< โ < (less than)
> โ > (greater than)
& โ & (ampersand)
" โ " (double quote)
© โ ยฉ (copyright)
€ โ โฌ (euro sign)
3. Numeric Entities (Hexadecimal)
< โ < (less than)
> โ > (greater than)
© โ ยฉ (copyright)
€ โ โฌ (euro sign)
๐ Essential HTML Entities Reference
Must-Escape Characters (Security)
| Character | Entity | Code | Purpose |
|---|---|---|---|
| < | < | < | Less than (tag start) |
| > | > | > | Greater than (tag end) |
| & | & | & | Ampersand |
| " | " | " | Double quote |
| ' | ' | ' | Apostrophe |
Common Symbols
| Character | Named | Numeric |
|---|---|---|
| ยฉ | © | © |
| ยฎ | ® | ® |
| โข | ™ | ™ |
| โฌ | € | € |
| ยฃ | £ | £ |
| ยฅ | ¥ | ¥ |
| ยฐ | ° | ° |
| ยฑ | ± | ± |
| ร | × | × |
| รท | ÷ | ÷ |
Whitespace & Special
| Description | Entity | Result |
|---|---|---|
| Non-breaking space | | Space that won't break |
| En space |   | Width of 'n' |
| Em space |   | Width of 'm' |
| Thin space |   | Narrow space |
| Soft hyphen | ­ | Optional hyphen |
Arrows
| Character | Entity | Character | Entity |
|---|---|---|---|
| โ | ← | โ | → |
| โ | ↑ | โ | ↓ |
| โ | ↔ | โต | ↵ |
๐ป Encoding in Code
JavaScript
// Encode HTML entities
function encodeHTML(str) {
return str
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
// Decode HTML entities
function decodeHTML(str) {
const textarea = document.createElement('textarea');
textarea.innerHTML = str;
return textarea.value;
}
PHP
// Encode
$encoded = htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
// Decode
$decoded = html_entity_decode($string, ENT_QUOTES, 'UTF-8');
Python
import html
# Encode
encoded = html.escape('<script>alert("XSS")</script>')
# Decode
decoded = html.unescape('<script>')
โ ๏ธ Common Mistakes
- Forgetting to escape in attributes:
<img alt=""test""> - Double encoding:
&lt;instead of< - Missing semicolon:
<(invalid) - Encoding in URLs: Use URL encoding, not HTML entities
โ Best Practices
- Always escape user input before displaying in HTML
- Use a library for encoding/decoding (don't reinvent)
- Specify charset as UTF-8 in your HTML
- Use named entities when possible (more readable)
- Test with special characters in your language
๐ Conclusion
HTML entities are a fundamental part of web development. They ensure your content displays correctly, prevents security vulnerabilities, and allows you to use any character in your HTML documents.
Bookmark this reference for quick lookup, and remember: when in doubt, encode it!