HTML Encoding Explained
Learn why HTML encoding matters, common entities, and how to encode special characters safely.
HTML encoding converts special characters into HTML entities so they display as text instead of being interpreted as markup. This prevents broken layouts and is essential for XSS (cross-site scripting) prevention.
Characters that must be encoded
- < becomes < (prevents opening tags)
- > becomes > (prevents closing tags)
- & becomes & (prevents entity injection)
- " becomes " (safe inside attributes)
- ' becomes ' (safe inside single-quoted attributes)
When to encode
- Displaying user-generated content on web pages
- Inserting dynamic values into HTML templates
- Embedding JSON or code snippets in HTML
- Building email templates with dynamic data