10. HTML Entities
Introduction
Section titled “Introduction”HTML entities are special codes used to represent characters that have special meaning in HTML or characters that are difficult to type directly. They allow you to display reserved characters, special symbols, and characters from various languages and character sets. Understanding HTML entities is essential for creating properly formatted and accessible web content.
What are HTML Entities?
Section titled “What are HTML Entities?”HTML entities are sequences of characters that represent other characters. They start with an ampersand (&) and end with a semicolon (;).
Basic Syntax
Section titled “Basic Syntax”&entity_name;&#entity_number;- Named entities:
&(ampersand) - Numeric entities:
&(decimal) or&(hexadecimal)
Why Use HTML Entities?
Section titled “Why Use HTML Entities?”Reserved Characters
Section titled “Reserved Characters”Some characters have special meaning in HTML and must be escaped:
<!-- These would break HTML if used directly --><p>Price: $10 < $20</p> <!-- < breaks HTML --><p>He said "Hello"</p> <!-- Quotes may cause issues --><p>Tom & Jerry</p> <!-- & starts an entity -->Solution:
<p>Price: $10 < $20</p> <!-- < for < --><p>He said "Hello"</p> <!-- " for " --><p>Tom & Jerry</p> <!-- & for & -->Special Characters
Section titled “Special Characters”Display characters that aren’t on your keyboard:
<p>© 2024</p> <!-- Copyright symbol --><p>® Trademark</p> <!-- Registered trademark --><p>™ Brand</p> <!-- Trademark symbol --><p>€ 100</p> <!-- Euro symbol --><p>£ 50</p> <!-- Pound symbol -->Accessibility
Section titled “Accessibility”Entities help ensure characters display correctly across different systems and browsers.
Common HTML Entities
Section titled “Common HTML Entities”Reserved Characters
Section titled “Reserved Characters”| Character | Entity Name | Entity Number | Description |
|---|---|---|---|
< | < | < | Less than |
> | > | > | Greater than |
& | & | & | Ampersand |
" | " | " | Double quote |
' | ' | ' | Apostrophe (single quote) |
Common Symbols
Section titled “Common Symbols”| Character | Entity Name | Entity Number | Description |
|---|---|---|---|
© | © | © | Copyright |
® | ® | ® | Registered trademark |
™ | ™ | ™ | Trademark |
€ | € | € | Euro |
£ | £ | £ | Pound |
¥ | ¥ | ¥ | Yen |
§ | § | § | Section |
¶ | ¶ | ¶ | Paragraph |
• | • | • | Bullet |
… | … | … | Ellipsis |
— | — | — | Em dash |
– | – | – | En dash |
° | ° | ° | Degree |
± | ± | ± | Plus-minus |
× | × | × | Multiplication |
÷ | ÷ | ÷ | Division |
½ | ½ | ½ | One half |
¼ | ¼ | ¼ | One quarter |
¾ | ¾ | ¾ | Three quarters |
Spacing Entities
Section titled “Spacing Entities”| Character | Entity Name | Entity Number | Description |
|---|---|---|---|
| |   | Non-breaking space |
|   |   | En space |
|   |   | Em space |
|   |   | Thin space |
Mathematical Symbols
Section titled “Mathematical Symbols”| Character | Entity Name | Entity Number | Description |
|---|---|---|---|
∞ | ∞ | ∞ | Infinity |
∑ | ∑ | ∑ | Summation |
∏ | ∏ | ∏ | Product |
√ | √ | √ | Square root |
∫ | ∫ | ∫ | Integral |
≈ | ≈ | ≈ | Approximately equal |
≠ | ≠ | ≠ | Not equal |
≤ | ≤ | ≤ | Less than or equal |
≥ | ≥ | ≥ | Greater than or equal |
Arrow Symbols
Section titled “Arrow Symbols”| Character | Entity Name | Entity Number | Description |
|---|---|---|---|
← | ← | ← | Left arrow |
→ | → | → | Right arrow |
↑ | ↑ | ↑ | Up arrow |
↓ | ↓ | ↓ | Down arrow |
↔ | ↔ | ↔ | Left-right arrow |
Using HTML Entities
Section titled “Using HTML Entities”In Content
Section titled “In Content”<p>Copyright © 2024 My Company</p><p>Price: €29.99</p><p>5 × 3 = 15</p><p>Temperature: 25°C</p>In Attributes
Section titled “In Attributes”<img src="photo.jpg" alt="Tom & Jerry"><a href="/page" title="Price < $100">Link</a>In Code Examples
Section titled “In Code Examples”When displaying HTML code in content:
<p>Use <code><div></code> for containers.</p><p>Escape ampersands: <code>&amp;</code></p>Numeric Entities
Section titled “Numeric Entities”You can use numeric codes instead of named entities:
Decimal
Section titled “Decimal”<p>© Copyright</p> <!-- Same as © --><p>& Ampersand</p> <!-- Same as & -->Hexadecimal
Section titled “Hexadecimal”<p>© Copyright</p> <!-- Same as © --><p>& Ampersand</p> <!-- Same as & -->Common Use Cases
Section titled “Common Use Cases”Copyright and Legal
Section titled “Copyright and Legal”<footer> <p>© 2024 Company Name. All rights reserved.</p> <p>Product® and Brand™ are trademarks.</p></footer>Mathematical Content
Section titled “Mathematical Content”<p>Area = πr²</p><p>5 × 3 = 15</p><p>Temperature: 25°C</p><p>x ≤ 10</p>Quotations
Section titled “Quotations”<p>He said, "Hello, world!"</p><p>It's a beautiful day.</p>Currency
Section titled “Currency”<p>Price: €29.99</p><p>Cost: £50.00</p><p>Amount: ¥1000</p>Best Practices
Section titled “Best Practices”When to Use Entities
Section titled “When to Use Entities”- Reserved characters: Always escape
<,>,&in content - Special symbols: Use entities for symbols not on keyboard
- Consistency: Use entities for consistent display across browsers
- Accessibility: Ensure screen readers can interpret characters
When Not to Use Entities
Section titled “When Not to Use Entities”- Regular text: Don’t use entities for normal letters and numbers
- UTF-8 encoding: With proper UTF-8, many characters can be typed directly
- Over-encoding: Don’t encode characters unnecessarily
UTF-8 Alternative
Section titled “UTF-8 Alternative”With UTF-8 encoding, you can often type characters directly:
<!-- Both work with UTF-8 --><p>© 2024</p><p>© 2024</p>
<!-- UTF-8 is preferred for readability --><meta charset="UTF-8"><p>© 2024</p>However, entities are still useful for:
- Reserved characters (
<,>,&) - When you can’t type the character
- Ensuring compatibility
Common Mistakes
Section titled “Common Mistakes”Forgetting the Semicolon
Section titled “Forgetting the Semicolon”Problem:
<p>Copyright © 2024</p> <!-- Missing semicolon -->Solution:
<p>Copyright © 2024</p> <!-- Correct -->Double Encoding
Section titled “Double Encoding”Problem:
<p>&amp;</p> <!-- Shows & instead of & -->Solution:
<p>&</p> <!-- Correct -->Using Entities Unnecessarily
Section titled “Using Entities Unnecessarily”Problem:
<p>Hello</p> <!-- Unnecessary -->Solution:
<p>Hello</p> <!-- Just type it -->Example: Using Entities
Section titled “Example: Using Entities”<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>HTML Entities Example</title></head><body> <header> <h1>HTML Entities Demo</h1> </header> <main> <section> <h2>Reserved Characters</h2> <p>Less than: < | Greater than: > | Ampersand: &</p> <p>Quote: "Hello" | Apostrophe: It's</p> </section>
<section> <h2>Symbols</h2> <p>Copyright: © 2024</p> <p>Trademark: Brand® and Product™</p> <p>Currency: €100, £50, ¥1000</p> </section>
<section> <h2>Mathematical</h2> <p>5 × 3 = 15</p> <p>Area = πr²</p> <p>Temperature: 25°C</p> <p>x ≤ 10 and y ≥ 5</p> </section>
<section> <h2>Spacing</h2> <p>Non-breaking space example</p> </section> </main> <footer> <p>© 2024 Example Site. All rights reserved.</p> </footer></body></html>