HTML Entities are special codes used to represent characters that are either reserved in HTML (and would be misinterpreted as code) or characters that are not easily generated on a standard keyboard (like foreign currency symbols or technical symbols).
1. The Structure of an Entity
All HTML entities follow a specific format:
- They begin with an ampersand (
&). - They end with a semicolon (
;). - The middle section is either a name (mnemonic) or a number (decimal or hexadecimal code).
| Format | Example | Description |
| Name Entity | &entity_name; | Easier to remember, but not all browsers support every name. |
| Number Entity | &#decimal_number; | Guaranteed to work in all browsers, but harder to memorize. |
| Hex Entity | &#xhex_number; | Less common, uses the hexadecimal code. |
2. Reserved Characters
The most common reason to use an entity is to display characters that the browser would otherwise interpret as the start or end of an HTML tag.
| Character | Description | Name Entity | Output |
< | Less Than (Used to open tags) | < | < |
> | Greater Than (Used to close tags) | > | > |
& | Ampersand (Used to start entities) | & | & |
" | Double Quote (Used for attributes) | " | “ |
' | Single Quote (Used for attributes) | ' | ‘ |
Example: Displaying Code
If you want to display the HTML code for a paragraph element, you must use entities:
<p>
To create a paragraph, use the tags: <p> and </p>.
</p>
3. Non-Breaking Space ( )
The non-breaking space entity is crucial for controlling text flow.
- Problem: Normal spaces are collapsed by the browser (multiple spaces are treated as one).
- Solution:
inserts a required space that the browser will not collapse and prevents a line break from occurring at that spot.
<p>
The price is $500 USD.
</p>
4. Common Symbols and Punctuation
Entities are used for common symbols and typographic elements not found on the keyboard.
| Symbol | Name Entity | Decimal Entity | Description |
© | © | © | Copyright Symbol |
€ | € | € | Euro Currency |
™ | ™ | ™ | Trademark Symbol |
— | — | — | Em Dash (long dash) |
" | ” | ” | Right Double Quote |
Example: Copyright Notice
<footer>
© 2024 My Website. All Rights Reserved.
</footer>
