Encode special characters to HTML entities
Encode special characters to HTML entities instantly with our free online HTML encoder tool. This encoder helps web developers and content creators convert characters like &, <, >, and quotes into safe HTML entities to prevent XSS attacks and display special characters correctly. Simply paste your text and get HTML-encoded output in real-time.
& → &
< → <
> → >
" → "
' → '
HTML encoding converts special characters to their HTML entity equivalents, preventing XSS attacks and ensuring characters display correctly in HTML. Characters like <, >, &, and quotes have special meaning in HTML and must be encoded when displaying user content.
The encoder replaces characters that have special HTML meaning with named or numeric entities. < becomes <, > becomes >, & becomes &, and quotes become " or '. This prevents browsers from interpreting user input as HTML markup.
HTML entities: < (<), > (>), & (&), " ("), ' or ' ('). Numeric entities (<) work for any Unicode character. Named entities are more readable but not all characters have names. Always encode user input before inserting into HTML.
Encode user input to prevent script injection.
<script>alert("xss")</script> → safe displayShow HTML code as text without browser rendering.
Safely include dynamic content in HTML emails.
Encode user-submitted content for safe display.
// Simple encoder
function htmlEncode(str) {
return str
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
// Using DOM (browser)
function htmlEncode(str) {
const div = document.createElement('div');
div.textContent = str;
return div.innerHTML;
}
// React does this automatically
<div>{userInput}</div> // Safe - React encodesReplace order matters: & first, or you'll double-encode. React/Vue auto-encode in JSX/templates.
import html
# Encode special characters
safe = html.escape('<script>alert("xss")</script>')
# '<script>alert("xss")</script>'
# With quote encoding (for attributes)
safe = html.escape('value="test"', quote=True)
# 'value="test"'
# Django templates auto-escape
{{ user_input }} # Automatically encodedhtml.escape is the standard library solution. Django/Jinja2 templates auto-escape by default.
Use HTML encoding whenever inserting dynamic content into HTML pages. Essential for preventing XSS vulnerabilities and safely displaying user-generated content.
HTML encoding is for displaying in HTML (<, &). URL encoding is for including in URLs (%20, %26). Different character sets, different purposes. A URL in an HTML href needs both: href="page?q=a%26b" with & in the HTML properly handled.