HTML character entities are used to display special characters that cannot be typed directly on a keyboard or may not display properly due to encoding issues.
These characters include symbols, accented letters, mathematical operators, and characters that have special meaning in HTML markup.
Every HTML entity begins with an ampersand (&) and ends with a semicolon (;).
This format tells the browser to interpret the code as a special character rather than regular text.
HTML provides three different methods to specify these special characters:
-
Named entities: Use descriptive names like (©) for the copyright symbol (©).
These are easy to remember and make code more readable.
-
Decimal entities: Use the character's decimal Unicode value like (©) for the copyright symbol (©).
The decimal number represents the character's position in the Unicode table.
-
Hexadecimal entities: Use the character's hexadecimal Unicode value like (©) for the copyright symbol (©).
Hexadecimal values are often used by developers familiar with Unicode references.
When using decimal or hexadecimal methods, you must prefix the number with a hash symbol (#) immediately after the ampersand.
For hexadecimal values, add an 'x' before the hex digits.
Essential HTML entities that every developer should know:
-
[ ]: Creates a non-breaking space that prevents line breaks between words or elements.
-
[<]: Displays the less-than symbol (<) without being interpreted as an HTML tag opening.
-
[>]: Displays the greater-than symbol (>) without being interpreted as an HTML tag closing.
-
[&]: Displays an ampersand (&) without starting a new entity code.
-
["]: Displays quotation marks (") inside attribute values without breaking the HTML structure.
These entities are particularly important when displaying code examples, user-generated content, or any text that might contain HTML-sensitive characters.
Using entities ensures your content displays correctly across all browsers and prevents potential security issues with malformed HTML.