Convert special characters to safe HTML entities and back — for code samples, CMS pasting, and keeping user content from breaking your markup.
HTML Entity Encoder / Decoder
Convert special characters to safe HTML entities and back — for displaying code snippets, avoiding broken markup, and preventing injected HTML.
Result
Encoded HTML
How to use this tool
HTML reserves a handful of characters for its own syntax, and writing them literally causes the browser to misinterpret your content. The critical five are & (starts an entity), < and > (delimit tags), and " and ' (delimit attribute values).
Encoding replaces each with a reference — < for <, & for & — which tells the browser to display the character rather than interpret it. Order matters when encoding by hand: escape the ampersand first, or you'll double-encode the entities you just created.
Why this is a security control, not a formatting one
Escaping output is the primary defence against cross-site scripting. If user-submitted text goes into your page unescaped, a comment containing a script tag becomes executable code running with your site's privileges — able to read cookies, hijack sessions and act as the logged-in user.
Escaping neutralises it entirely: the same input renders as harmless visible text. Modern frameworks escape by default, which is why XSS is rarer than it was — but every framework also has an escape hatch (dangerouslySetInnerHTML in React, v-html in Vue, innerHTML in vanilla JS). Those are where XSS lives today, so treat any use of them as needing justification.
Decoding runs the other way — turning <div> back into readable text. It comes up when an API returns pre-escaped content, when a database stores escaped strings, or when content has been double-encoded somewhere in a pipeline and displays as literal &lt; on the page.
This tool decodes using the browser's own parser, so it handles named and numeric references correctly rather than relying on a lookup table that inevitably misses cases. One caution: never decode untrusted content and then insert it as HTML — that undoes the protection escaping provided in the first place.
Escaping for attributes, not just text
A popular shortcut escapes a string by putting it in a text node and reading back innerHTML. That escapes &, < and >, but the HTML serialiser leaves quotes alone in text, so the output is unsafe as soon as it lands inside an attribute value: an input such as " autofocus onfocus="… closes the attribute and adds an event handler. If you build HTML as strings, in a server template, an email body or a template literal, use an escape function that covers all five characters:
const ESCAPES = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
};
// One pass, so '&' can never be double-encoded
function escapeHTML(value) {
return String(value).replace(/[&<>"']/g, ch => ESCAPES[ch]);
}
const name = `" autofocus onfocus="alert(document.cookie)`;
console.log(`<input value="${escapeHTML(name)}">`);
// <input value="" autofocus onfocus="alert(document.cookie)">
console.log(escapeHTML("Tom & Jerry's <b>"));
// Tom & Jerry's <b>
In the browser, the better answer is usually not to escape at all. Assign to element.textContent or call setAttribute(), and the value is stored as data and never parsed as markup, so there is no escaping step to forget. Keep a string escaper for the cases where the output really has to be an HTML string.
Escaping also has limits. It protects text and quoted attribute values only; an unquoted attribute can be broken with a plain space. It does nothing about a javascript: URL in an href, which needs an allow-list of http: and https:, or about values placed inside <script>, inline event handlers or CSS. The OWASP Cross Site Scripting Prevention Cheat Sheet covers each context, and MDN's page on textContent explains how it differs from innerHTML.
Frequently asked questions
Which characters must be escaped in HTML?
Five: the ampersand, less-than, greater-than, double quote and apostrophe. Escape the ampersand first when doing it manually, or you will double-encode the entities you just created.
Does escaping HTML prevent XSS attacks?
Escaping output is the primary defence. Unescaped user content can inject executable scripts that read cookies and hijack sessions. Escaping renders the same input as harmless visible text — but it must be applied wherever untrusted content enters the page.
What is the difference between named and numeric entities?
For a space that must not break across lines — between a number and its unit, or in a name that should stay together. Avoid using it for layout spacing; that is what CSS margin and padding are for.