HTML Encoder & Decoder — Complete Guide
What is an HTML Encoder & Decoder?
An HTML Encoder & Decoder is a tool that converts special characters (<, >, &, quotes) into safe HTML entities — and back. Encoding is a core defence against cross-site scripting (XSS). When you display user-generated content without encoding, a malicious user could inject <script> tags that run in other users' browsers. Encoding converts those characters into harmless text, so the browser renders them as data — not code.
Decoding is the reverse: it turns entities like < back into <. It is useful for reading stored content, but must be used carefully — always sanitize decoded output before rendering it to the DOM.
How HTML Encoding Works
Every reserved HTML character has a corresponding entity. Encoding replaces the character with the entity so the browser treats it as text:
| Character | Entity | Why |
|---|---|---|
| & | & | Starts an entity; must be escaped first |
| < | < | Starts a tag; escape to prevent injection |
| > | > | Ends a tag; escape for symmetry |
| " | " | Used in attributes |
| ' | ' | Used in attributes (' is HTML4-safe) |
| / | / | Prevents premature </script> termination |
Example
<h1>Hello & welcome</h1>
becomes
<h1>Hello & welcome</h1>
Encode vs Decode
Encode (HTML → Entities)
Use before displaying user input, or before embedding HTML inside JSON, XML, or an attribute. Prevents XSS and tag injection.
Decode (Entities → HTML)
Use when reading stored or received content. Always sanitize the decoded output before rendering it to the page.
Features of This Tool
Encode & Decode
Convert HTML to entities and entities back to HTML.
Quote Encoding
Optionally encode " and ' for attribute safety.
Slash Encoding
Encode / as / to prevent premature tag closing.
Non-ASCII Support
Encode emojis and multilingual text as &#NNN; entities.
Safe Preview
Sanitized preview removes scripts and inline handlers.
Swap Input/Output
One-click swap to reverse the operation.
Copy & Download
Copy the output or download it as a .txt file.
100% Private
Everything runs in your browser — no server, no storage.
Common Use Cases
- User-generated content: Encode comments, profiles, and forum posts before displaying them.
- API responses: Safely embed HTML strings inside JSON or XML payloads.
- Email templates: Avoid tag-breaking when HTML is embedded in email clients.
- Code snippets: Display raw HTML on a blog or documentation page as text.
- Legacy systems: Convert non-ASCII characters into numeric entities for old browsers.
- Debugging: Decode entity-encoded strings back to readable HTML.
Security Best Practices
- Encode on output, not on input: Store raw data, encode at the moment of display.
- Use context-aware encoding: HTML body, attributes, JavaScript, and CSS each require different escaping.
- Never trust the preview: The sanitizer here is for preview only — use DOMPurify or a similar library in production.
- Decode only to sanitize: Decode untrusted data only when you will re-sanitize before rendering.
- Encode quotes in attributes: Unencoded quotes inside an attribute can break out of it.
Privacy & Data Handling
This tool runs entirely in your browser. Your input is never uploaded, logged, or stored anywhere. Encoding, decoding, and preview generation all happen locally using native browser APIs (textarea for decoding, replace for encoding). Safe for API tokens, config strings, and internal test data.
Technical Details
Common Mistakes & Fixes
- Double-encoding: Encoding an already-encoded string produces
&lt;instead of<. Decode first if the input is already encoded. - Decoding then rendering without sanitizing: This re-introduces the XSS risk. Always sanitize decoded output.
- Skipping quote encoding in attributes: Unencoded quotes can break out of
href="..."ortitle="...". - Not encoding slashes inside <script>: A
</script>inside a JS string can terminate the block early — encode the slash.
Frequently Asked Questions
What is an HTML Encoder and Decoder?
An HTML Encoder converts special characters like <, >, &, quotes, and slashes into HTML entities so the browser displays them as text instead of executing them as code. An HTML Decoder reverses the process, turning entities like < back into readable characters.
Why should I encode HTML?
Encoding HTML prevents cross-site scripting (XSS) attacks. If you display user-generated content without encoding, a malicious user could inject <script> tags or event handlers that run in other users' browsers. Encoding converts these dangerous characters into harmless text.
Is it safe to decode user input?
Decoding is only safe when the output is sanitized and rendered in a safe context. Never decode untrusted input and insert it directly into the DOM. Always sanitize with a library like DOMPurify before rendering.
What does "encode quotes" do?
It converts double quotes (") and single quotes (') into " and '. This is important when HTML is placed inside attributes, where unencoded quotes can break the attribute and allow injection.
Why would I encode slashes?
Encoding the forward slash (/) as / is useful in some legacy systems, JSON embedding, and cases where a closing tag like </script> appears inside a JavaScript string. It prevents the browser from prematurely ending a script block.
What is the Non-ASCII → &#NNN; option?
It converts non-ASCII characters (emojis, accented letters, Chinese, Hindi, Arabic, etc.) into numeric HTML entities. This ensures compatibility with older systems or email clients that do not fully support UTF-8.
Is the preview safe?
Yes. The preview sanitizes the HTML before rendering by removing <script> tags and inline event handlers (onclick, onerror, etc.). It is a lightweight sanitizer for preview only — not a replacement for a production-grade sanitizer like DOMPurify.
What is the difference between HTML entities and numeric entities?
Named entities use names like &, <, ". Numeric entities use code points like &, <, ". Both work in browsers. Named entities are more readable; numeric entities are safer for edge cases and older systems.
Can I use this tool for JSON or XML?
Yes. HTML-encoded output is also safe inside JSON and XML strings because the reserved characters (<, >, &, quotes) are converted into entities that will not break the surrounding format.
Pro Tip
Encode at the moment of output, not at the moment of input. Store raw data in the database, then encode when you render it into HTML. This keeps your data clean and lets the same record be safely rendered in HTML, JSON, or a plain-text email without double-encoding.