HTML Entities & Special Characters
HTML entities display reserved characters or characters difficult to type. They start with & and end with ;.
Why Entities?
Some characters have special meaning in HTML:
<and>define tags&starts entities"defines attribute values
To display these characters as text, you need entities.
Common Named Entities
| Character | Entity | Description |
|---|---|---|
< |
< |
Less than |
> |
> |
Greater than |
& |
& |
Ampersand |
" |
" |
Double quote |
' |
' |
Apostrophe (XML) |
© |
© |
Copyright |
® |
® |
Registered |
™ |
™ |
Trademark |
€ |
€ |
Euro |
£ |
£ |
Pound |
¥ |
¥ |
Yen |
Examples
Displaying Code
<!-- Show HTML code without executing it -->
<p>To create a paragraph, use <p> tags.</p>
<p>
Use & to create entities: &lt; renders as <
</p>
Result:
To create a paragraph, use <p> tags.
Use & to create entities: < renders as <
In Attributes
<a href="page.html?q=1&s=2">Link</a>
<img alt="Fish & Chips" src="food.jpg" />
Quotes in Text
<p>He said "Hello!" and left.</p>
<p>It's a beautiful day.</p>
Result: He said “Hello!” and left. It’s a beautiful day.
Numeric Entities
Use character codes (decimal or hexadecimal) for any character:
<!-- Decimal -->
<p>< > &</p> <!-- < > & -->
<!-- Hexadecimal -->
<p>< > &</p> <!-- < > & -->
<!-- Any Unicode character -->
<p>😄</p> <!-- 😊 emoji -->
<p>😀</p> <!-- 😀 emoji -->
Special Whitespace
HTML collapses whitespace. Use entities for intentional spacing:
<!-- Normal spaces collapse -->
<p>Word1 Word2</p> <!-- Renders as: Word1 Word2 -->
<!-- Non-breaking space keeps words together -->
<p>Word1 Word2</p> <!-- Keeps words on same line -->
<!-- Useful for: -->
<p>Price: $100 USD</p> <!-- Prevents line break -->
<p> Indented text</p>
Complete Example
Here’s a complete page using entities:
<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML Entities Demo</title>
</head>
<body>
<h1>Special Characters in HTML</h1>
<section>
<h2>Displaying Code</h2>
<p>
To create a paragraph: <p>Your text</p>
</p>
<p>
Links use &href attribute: <a href="url">
</p>
</section>
<section>
<h2>Mathematical Symbols</h2>
<p>Less than: <</p>
<p>Greater than: ></p>
<p>Equal to: =</p>
<p>Not equal: ≠</p>
<p>Plus/minus: ±</p>
<p>Multiplication: ×</p>
<p>Division: ÷</p>
</section>
<section>
<h2>Currency</h2>
<p>Dollar: $</p>
<p>Euro: €</p>
<p>Pound: £</p>
<p>Yen: ¥</p>
</section>
<section>
<h2>Legal Symbols</h2>
<p>Copyright: © 2026 My Company</p>
<p>Registered: ®</p>
<p>Trademark: ™</p>
</section>
<section>
<h2>Quotes</h2>
<p>
He said “Hello” and she replied “Hi!”
</p>
<p>
It’s a beautiful day, isn’t it?
</p>
</section>
<section>
<h2>Arrows</h2>
<p>Left: ←</p>
<p>Right: →</p>
<p>Up: ↑</p>
<p>Down: ↓</p>
</section>
<section>
<h2>Emojis (Numeric Entities)</h2>
<p>Happy: 😁</p>
<p>Thumbs up: 👍</p>
<p>Star: ⭐</p>
</section>
</body>
</html>
Common Use Cases
Displaying HTML in Documentation
<p>
The <img> element requires the src attribute.<br />
Example: <img src="photo.jpg" alt="Photo" />
</p>
User-Generated Content
<!-- Always escape user input to prevent XSS -->
<p>User said: <script>alert('hack')</script></p>
Preventing Line Breaks
<p>
CDN URL: https://example.com/path/to/very/long/file.js
(bookmark this)
</p>
Complete Character Reference
Mathematical:
<>≤≥≠±×÷
Arrows:
←→↑↓↔
Punctuation:
“”‘’…—–
Currency:
¢£¥€$
Legal:
©®™¶§
Best Practices
- Use named entities — More readable than numeric
- Always escape
<,>,&— Required in text content - Use
for spacing — When you need non-breaking spaces - Escape user input — Prevents XSS attacks
- Don’t overuse entities — Most characters don’t need them
Common Mistakes
- Missing semicolon —
<instead of< - Using entities in URLs — Use
&in href attributes - Forgetting to escape
<— Browser treats it as a tag - Using numeric for common characters — Named is more readable
- Not escaping user content — Security vulnerability