Toolsky

Free Online Tools & Utilities for Every Task.

HTML Entity Encode

Encode HTML into HTML Entities.

There are moments when you need a browser to display HTML instead of obeying it: showing a code sample in a tutorial, printing a user's comment verbatim, or storing markup as plain data. If you paste raw tags into a page, the browser renders them and your example vanishes into the layout. The HTML entity encoder solves this by rewriting the sensitive characters as entities — < becomes &lt;, & becomes &amp; — so the browser shows the code as readable text while the page itself stays intact.

When Encoding Is the Right Move

Encoding is the standard way to move markup across a boundary where it must be treated as data. Tutorial and documentation writers encode every snippet they publish, because the whole point is that readers see the tags. Forum and comment systems encode user submissions before display so that a pasted tag cannot reshape the page around it. Developers encode fragments when embedding them in attribute values, generating pages from templates, preparing sample data for documentation, or writing test fixtures where the literal characters matter. In each case the goal is identical: the text should survive the journey unchanged and arrive as visible characters, not as instructions to the browser. Encoding is also reversible — anyone who receives the encoded version can decode it and recover the original exactly.

What the Encoder Converts

  • Angle brackets: < and > become &lt; and &gt;, which is what stops tags from being parsed as tags.
  • Ampersands: & becomes &amp;, handled first so that entities already present in your text are not corrupted.
  • Quotation marks: double quotes become &quot;, which keeps encoded text safe inside HTML attribute values delimited by quotes.
  • Other markup-significant characters: apostrophes and similar characters can be represented numerically so nothing in the output can break the surrounding document.
  • Everything else passes through: ordinary letters, numbers and punctuation that carry no structural meaning are left exactly as they are, keeping the output readable.

The operation is the exact inverse of the HTML entity decoder, and it is frequently paired with the HTML code editor when preparing examples: write the code, check it renders, then encode the same fragment for publication as text.

How to Encode HTML into Entities

  1. Write or copy the HTML fragment you want to display as text.
  2. Paste it into the encoder's input area in full, including any attributes and quotes.
  3. Run the encode action and inspect the converted output.
  4. Check a couple of spots: tags should now begin with &lt;, and any original ampersands should appear as &amp;.
  5. Copy the encoded text into your article, comment, template or documentation where the code should appear as visible text.
  6. Preview the final page once to confirm the snippet displays literally and the surrounding layout is undisturbed.

A Worked Example: Displaying Code in a Guide

Imagine you are writing a short guide and want readers to see this line exactly as written:

<p class="intro">Tom & Jerry</p>

Pasted raw into your guide, the browser would render a paragraph with the words "Tom & Jerry" and the tag itself would disappear. After encoding, the line becomes:

&lt;p class=&quot;intro&quot;&gt;Tom &amp; Jerry&lt;/p&gt;

That looks forbidding in source form, but when a browser displays it, readers see precisely the original line — angle brackets, quotes and ampersand all present as text. Note the ampersand inside "Tom & Jerry": it had to become &amp;, because leaving it bare would risk the browser reading what follows as the start of an entity.

Good Practice and Common Mistakes

Encode at display time, not at storage time, when you can. Storing text pre-encoded leads to the classic double-encoding bug, where readers see "&lt;" printed on the page because the content was encoded twice — once in the database and once by the template. If your output literally shows entity codes, something encoded it twice. Encoding is not sanitising. Escaping characters for one context does not make arbitrary user content safe in every context: text destined for a JavaScript string, a URL or an unquoted attribute needs its own appropriate escaping, and genuinely untrusted markup needs a proper sanitiser. Mind the character set. Encoding handles the structural characters; characters outside your document's encoding can also be written as numeric entities, but in a modern UTF-8 workflow accented letters and symbols are usually fine to leave as themselves. For percent-style escaping inside web addresses, use the dedicated URL encoder instead — the two systems are not interchangeable.

Frequently Asked Questions About Encoding HTML Entities

Why does my code disappear when I paste it into a page?

Because the browser treats pasted tags as instructions, not text. It renders the element and shows only its contents. Encoding the fragment first turns the tags into visible characters, so readers see the code itself.

Which characters actually need to be encoded?

The essential ones are <, > and &. Quotation marks matter when the text will sit inside a quoted attribute value. Everything else in ordinary prose can normally be left alone in a UTF-8 document.

Does encoding change how the text looks?

No — that is the point. Properly encoded text displays exactly like the original when a browser renders it. Only the underlying source representation changes.

What is double encoding, and how do I spot it?

It happens when text is encoded twice, so the page ends up showing the entity codes themselves, such as visible "&lt;" strings in the article. The fix is to find the second encoding step — usually a template or import routine — and remove one of them.

Is encoding enough to make user-submitted HTML safe?

Encoding everything a user submits is a sound way to display it as inert text. It is not a complete security strategy for cases where some markup should survive; that requires a dedicated HTML sanitiser with an allowlist, which is a different tool for a different job.

What is the difference between named and numeric entities?

Named entities use a memorable word, such as &lt; for <. Numeric entities use the character's number, such as &#60;. Browsers render both identically; named forms are simply easier for humans to read in source code.

Should I encode a whole page or just fragments?

Almost always fragments: the snippet you want to display, the value you want to embed, the sample in the tutorial. Encoding an entire working document would stop it from functioning as a page at all.

Show the Code Without Running It

Encoding is a small, precise step that separates code-as-instructions from code-as-text. Whenever a fragment needs to be seen rather than executed — in documentation, comments, examples or data — running it through the encoder first keeps both the snippet and the page around it intact.