Why HTML Encoding Matters
HTML encoding, also called HTML escaping, converts characters that have special meaning in HTML into their corresponding named or numeric entities. This prevents the browser from misinterpreting user input as markup, which both preserves the intended text and protects against Cross-Site Scripting (XSS) attacks. Any text that originates from user input, databases, third-party APIs, or content management systems should be encoded before it is rendered. Applying encoding consistently is one of the simplest and most effective security controls a web application can adopt.
Common HTML Entities
The most common entities are & for the ampersand, < for the less-than sign, > for the greater-than sign, " for the double quote, and ' for the apostrophe. Any Unicode character can be represented with numeric references: &#NNN; in decimal or &#xNNN; in hexadecimal, for example 😀 for the smiley emoji. Named entities exist for many symbols, such as for a non-breaking space and © for the copyright sign. Using the correct entity ensures the character renders as intended in every browser.