Quick access

Saved in this browser. Sign in to keep it on all your devices.

HTML Encoder

Encode and decode HTML special characters for safe embedding.

Input
Output
Encode: chars Decode: chars

Quick Examples

Character Reference

& → &
< → &lt;
> → &gt;
" → &quot;
' → &#39;
→ &nbsp;
© → &copy;
® → &reg;
™ → &trade;
¢ → &cent;
Version 1.1 Updated What's new

Release notes

  1. v1.1 Latest
    • Added an in-depth guide below the tool, written from what the tool actually does.
    • Text is decoded once instead of twice, and emoji are handled correctly.

About the HTML Encoder

The HTML encoder replaces characters that have a special meaning in HTML, such as < and &, with safe codes like &lt; and &amp;, so text shows up as written instead of being read as markup.

How to use it

  1. 1 Paste text or HTML into the input.
  2. 2 Choose Encode or Decode.
  3. 3 Copy the safe output.

Frequently asked questions

Which characters need escaping in HTML?

At minimum: & (&amp;), < (&lt;), > (&gt;), double quotes (&quot;), and single quotes (&#39;) inside attribute values.

Does HTML encoding prevent XSS?

Escaping user input before putting it into HTML is a key defence against cross-site scripting, but you also need the right escaping for JavaScript, URLs, and CSS contexts.

What is the difference between this and the HTML Entity tool?

This tool escapes only the characters that must be escaped. The HTML Entity tool can also turn every non-ASCII character into a named or numeric entity.

Five characters do the damage

Browsers treat < as the start of a tag and & as the start of an entity, and quotes end an attribute value. Encoding replaces those with references, so they display and are never interpreted:

&  →  &amp;
<  →  &lt;
>  →  &gt;
"  →  &quot;
'  →  &#39;

Single quotes use the numeric form because &apos; was missing from HTML 4, and some older software still does not recognize it. The & must always be handled first or in one pass, or the ampersands in the newly created references would be encoded a second time.

Numeric and hexadecimal modes

Both modes write the Unicode code point of the five special characters and of everything outside basic ASCII. The letter é becomes &#233; or &#xe9;. An emoji is a single code point even though JavaScript stores it as two units, so 😀 becomes &#128512; and not a pair of broken halves.

These forms are handy when a file’s encoding is uncertain, as with an old email template, since the output is pure ASCII and survives any character set.

Double encoding

If you see &amp;lt; on a page, or a literal &lt; where a < belongs, the text has been encoded twice. Decode removes exactly one layer, so &amp;lt; becomes &lt;. Press it again on the result for the next layer. Unknown names such as &foo; stay as written so nothing is silently lost.

Context decides the right escaping

HTML encoding is correct for text between tags and for quoted attribute values. Other positions follow other rules:

  • Inside a URL parameter, use percent-encoding. The URL Encoder on this site does that.
  • Inside a script block, use JSON encoding and keep the data out of the markup where you can.
  • An href still needs its scheme checked, because javascript:alert(1) contains nothing that HTML encoding would change.

In application code, rely on your template engine’s automatic escaping, such as Blade’s double braces, React’s JSX or Django templates. Manual escaping is easy to forget in one place, and one place is enough for a cross-site scripting bug.

', style: '', quotes: 'He said "Hello" and \'Hi\'', ampersand: 'Tom & Jerry are friends' }; this.input = examples[type] || ''; this.encode(); }, clearAll() { this.input = ''; this.output = ''; }, copyOutput() { navigator.clipboard.writeText(this.output); } }; }