JSONToonPro
Encoding tool

HTML Encode

Convert special characters like less-than, greater-than, ampersand, and quotes into safe HTML entities to prevent markup injection and display code as text. Everything runs 100% client-side in your browser.

100% client sideInstant resultNo data sent
Plain text / HTML
0 chars
HTML Encoded
Result appears here...

What HTML Entities Are and Why They Exist

An HTML parser reads a document looking for structure. When it sees a less-than sign it assumes a tag is starting, and when it sees an ampersand it assumes an entity reference is starting. If your content genuinely contains those characters as text, the parser has no way to tell the difference, and your paragraph about "a < b" silently becomes a malformed element.

Entities solve this by giving every problem character an alternative spelling that contains no structural characters. An entity begins with an ampersand, names the character either by keyword or by code point number, and ends with a semicolon. The parser resolves it back to the original character at the moment it builds the text node, so the reader sees exactly what you intended.

The Characters You Must Escape

Five characters do the damage. Two of them, the ampersand and the less-than sign, are dangerous anywhere in an HTML document. The others matter depending on where the text lands.

& -> &amp; always, and escape it FIRST
< -> &lt; always, it starts a tag
> -> &gt; for safety and symmetry
" -> &quot; required inside double quoted attributes
' -> &#39; required inside single quoted attributes

Order is not optional. If you escape the less-than sign before the ampersand, the ampersand you just introduced gets escaped again and < turns into &amp;lt; which renders as visible text rather than a symbol. Always replace the ampersand first, then everything else.

Escaping and Cross-Site Scripting

Escaping user input before it reaches a page is the primary defence against cross-site scripting. An attacker who can get raw markup into your HTML can run JavaScript in your users' sessions. Encoding removes the ability of the input to change the shape of the document.

Raw input:
<script>fetch('/api/keys').then(r=>r.text())</script>
 
Escaped:
&lt;script&gt;fetch(...)&lt;/script&gt;
 
The browser now prints the text instead of executing it.

The critical subtlety is that escaping is context dependent. There is no single correct encoding, only the correct encoding for the place the value lands:

  • HTML body. Escape the five characters above. This is what an HTML encoder does.
  • Attribute value. Escape the same set and always quote the attribute. An unquoted attribute can be escaped by a space alone.
  • Inside a script block. HTML escaping does not help. You need JavaScript string escaping, and the safest approach is to serialise the value as JSON rather than interpolating it.
  • Inside a URL attribute. Percent-encode the value and validate the scheme, since javascript: URLs survive HTML escaping untouched.
  • Inside CSS. CSS has its own escaping rules and its own injection vectors. Prefer not to interpolate user data into styles at all.
Escaping is a defence, not the only defenceUse the framework's own escaping wherever possible, because it knows the context. Avoid APIs that insert raw markup, and add a Content Security Policy so that a missed escape does not become an immediate compromise.

Named, Numeric, and Hexadecimal Entities

The same character can be written three ways. All three render identically, and the choice is about readability and compatibility.

Named: &amp; readable, HTML only, about 2200 defined names
Numeric: &#38; decimal code point, works in XML and HTML
Hexadecimal: &#x26; hex code point, matches Unicode notation U+0026

XML predefines only five names (amp, lt, gt, quot, apos), so anything generating XML, RSS, or SVG should prefer numeric references. Note also that &apos; was added in HTML5 and is unreliable in very old HTML4 parsers, which is why security libraries tend to emit &#39; for the apostrophe instead.

HTML Entity Reference Table

Named entities are easier to read, numeric entities work everywhere including XML where only five names are predefined. Both forms always end with a semicolon.

CharacterNamed entityNumeric entityDescription
&&amp;&#38;Ampersand
<&lt;&#60;Less than, opens a tag
>&gt;&#62;Greater than, closes a tag
"&quot;&#34;Double quotation mark
'&apos;&#39;Apostrophe or single quote
(space)&nbsp;&#160;Non-breaking space
©&copy;&#169;Copyright sign
®&reg;&#174;Registered trademark sign
&trade;&#8482;Trademark sign
 –&ndash;&#8211;En dash
&hellip;&#8230;Horizontal ellipsis
&euro;&#8364;Euro sign
£&pound;&#163;Pound sign
°&deg;&#176;Degree sign
×&times;&#215;Multiplication sign

Working on something related? Browse every free developer tool on the site, including a HTML decoder, URL encoder, Base64 encoder, and text utilities. Everything runs entirely in your browser, so nothing you paste is ever uploaded.

Frequently asked questions

4 answers
HTML encoding replaces characters that have special meaning in HTML with their entity equivalents, so a browser renders them as literal text instead of interpreting them as markup. For example, a less-than sign becomes its named entity, which stops the browser from treating it as the start of a tag. This tool encodes any text you paste, converting the reserved characters while leaving normal letters and numbers untouched.

More JSON Tools

About HTML Encode

Any time text you do not fully control ends up inside an HTML page, it needs to be encoded first. Without encoding, a stray angle bracket can break your layout, and a malicious script tag can hijack your users. This HTML encoder converts the reserved characters (less-than, greater-than, ampersand, and both quote styles) into their entity equivalents so the browser renders them as plain text. It is the right tool for escaping user input before display, preparing code snippets for documentation, and hardening templates against cross-site scripting. Processing happens entirely in your browser, so nothing you paste is ever uploaded.