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
Paste the snippet
Paste your HTML into the left box and keep the direction on Encode to entities.
Use minimal encoding
Choose Only & < > " '. That's all a modern UTF-8 page needs. The angle brackets become
<and>, the quotes become", and the&in "Save & close" becomes&.<button class="btn" onclick="save()">Save & close</button>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><button class="btn" …>Save & close</button></code></pre>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.
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
<instead of<. Only escape when you write raw HTML. - Escaping twice
- If your page shows
&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.