~/
hackweb.dev
HTML Entities & Special Characters
Quiz
⌘K
...
~/
/tutorials
/html/html-entities/edit
~ Contribute
Suggest a correction or improvement. The author reviews it before it goes live.
Loading...
Comment
0 / 300
Typo
Grammar
Broken link
Clarify
Code
en/tutorials/html/9html-entities
Write
Preview
Diff
# 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 ```html <!-- 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 ```html <a href="page.html?q=1&s=2">Link</a> <img alt="Fish & Chips" src="food.jpg" /> ``` ### Quotes in Text ```html <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: ```html <!-- 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: ```html <!-- 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: ```html <!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 ```html <p> The <img> element requires the src attribute.<br /> Example: <img src="photo.jpg" alt="Photo" /> </p> ``` ### User-Generated Content ```html <!-- Always escape user input to prevent XSS --> <p>User said: <script>alert('hack')</script></p> ``` ### Preventing Line Breaks ```html <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 1. **Use named entities** — More readable than numeric 2. **Always escape `<`, `>`, `&`** — Required in text content 3. **Use ` ` for spacing** — When you need non-breaking spaces 4. **Escape user input** — Prevents XSS attacks 5. **Don't overuse entities** — Most characters don't need them ## Common Mistakes 1. **Missing semicolon** — `<` instead of `<` 2. **Using entities in URLs** — Use `&` in href attributes 3. **Forgetting to escape `<`** — Browser treats it as a tag 4. **Using numeric for common characters** — Named is more readable 5. **Not escaping user content** — Security vulnerability
No changes yet
Reset to original
Submit suggestion
cancel