What are HTML Entities?
HTML entities are reserved strings of characters used to display reserved symbols, invisible whitespace, and non-ASCII characters on web pages. In HTML, characters like < and > are reserved for opening and closing tags. To display these literal characters on your website, you must escape them using entities such as < and >.
Named Entities vs. Numeric Decimal & Hex Entities
HTML entities exist in three primary formats:
- Named Entities: Easy-to-remember mnemonics preceded by an ampersand and followed by a semicolon (e.g.,
©for ©,™for ™). - Decimal Entities: Based on the Unicode character code point in base-10 format (e.g.,
©). - Hexadecimal Entities: Based on the Unicode hexadecimal code preceded by
#x(e.g.,©).
Why Use HTML Entities in Modern Web Development?
Even though modern UTF-8 encoding allows direct pasting of most symbols, HTML entities remain indispensable for:
- XSS Prevention & Escaping: Preventing browser misinterpretation of user-submitted text by escaping
<,>,&, and". - Non-Breaking Spaces: Using
to prevent automatic word wraps between numbers and units (e.g., "10 kg"). - Zero-Width Joining: Managing complex Unicode sequences with
‍and‌.