HTML Entity Encoder · Tutorial

How to show HTML code on a web page without it running

4 min readbeginnerUpdated

You're writing a blog post or documentation and want to show a piece of HTML code, but when you paste it into the page the browser renders a real button instead of showing the code. This tutorial escapes the snippet so it displays exactly as written.

What you need

  • The HTML Entity Encoder
  • The HTML snippet you want to display (the Try it button loads a button element)

Steps

  1. Paste the snippet

    Paste your HTML into the left box and keep the direction on Encode to entities.

  2. Use minimal encoding

    Choose Only & < > " '. That's all a modern UTF-8 page needs. The angle brackets become &lt; and &gt;, the quotes become &quot;, and the & in "Save & close" becomes &amp;.

    &lt;button class=&quot;btn&quot; onclick=&quot;save()&quot;&gt;Save &amp; close&lt;/button&gt;
  3. Wrap it in pre and code

    Paste the escaped text inside <pre><code> tags in your page. <pre> keeps line breaks and spacing, and <code> marks it as code for styling and screen readers.

    <pre><code>&lt;button class=&quot;btn&quot; …&gt;Save &amp; close&lt;/button&gt;</code></pre>
  4. Check the result

    View the page. The reader now sees the original snippet, <button class="btn" onclick="save()">Save & close</button>, as text, and no button is rendered.

Try it yourself

Opens the HTML Entity Encoder with this tutorial's sample loaded.

Try it now

Common mistakes

Escaping inside a Markdown code block
Most Markdown editors escape code blocks for you. If you escape the text as well, readers see &lt; instead of <. Only escape when you write raw HTML.
Escaping twice
If your page shows &amp;lt;, the text was escaped twice. Decode once and try again.
Relying on this for user input
For text submitted by users, let your framework or template engine escape it automatically, every time. Manual escaping is easy to forget.

Next steps

Go back to the HTML Entity Encoder, or keep going with one of these:

FAQ

Do I need to escape > as well as <?

Strictly, only < and & must be escaped in normal text, but escaping > too is harmless and avoids edge cases.