You write the documentation, paste in a short HTML example so readers can copy it, and publish. Then you open the page and the example is gone. In its place there is a working link, a stray line break, or simply a gap where the code was supposed to be. Nothing crashed and nothing was deleted. The browser read your sample exactly the way it reads the rest of the page: as markup to act on, not as text to show.

That confusion is built into how pages work, and it catches everyone who publishes code samples, documentation or user content that a browser would otherwise swallow as markup. This guide shows how to make a sample display literally, using the free HTML Entity Encoder/Decoder on Ovatly, and it is clear about the boundary of the fix: encoding makes code safe to display as text, nothing more.

Contents

  • Quick answer
  • Why the browser eats your sample
  • The five characters that do the damage
  • How to display a code sample, step by step
  • Worked example: a two-line snippet, before and after
  • Limitations and honest limits
  • Common mistakes
  • FAQs
  • Conclusion

Quick answer

Before the sample goes anywhere near the page, run it through the HTML Entity Encoder/Decoder: paste the code in, press Encode entities, and copy the result. Encoding converts the characters a browser acts on, &, <, >, the double quote and the single quote, into their safe entity forms, so < becomes &lt; and & becomes &amp;. Paste the encoded text into the page in place of the raw sample, and the browser prints the code exactly as written instead of parsing it. The conversion runs locally in your browser; nothing is uploaded.

Why the browser eats your sample

A browser has no way to know which parts of a page are instructions and which parts are merely pictures of instructions. When it meets a < character, it starts reading a tag. When it meets a closing tag, it ends an element. Your carefully pasted sample goes through the same machinery as the page's real markup, so a sample link becomes an actual link, a sample heading becomes an actual heading, and a sample tag with no visible rendering, like a meta or link element, disappears from the reader's view entirely.

Quotes fail in a quieter way. Page markup is full of attribute values wrapped in double quotes, so a raw double quote inside text that sits in an attribute ends the value early and corrupts everything after it. The ampersand has its own trap: & begins an entity reference, so text that happens to contain an ampersand followed by the wrong letters can print as a different character than the one you typed. The sample is not broken because you wrote it badly. It is broken because, unencoded, it is not text at all. It is live markup wearing a sample's clothes.

The five characters that do the damage

The fix does not require transforming the whole sample. Encoding covers the five characters that matter for safety and correctness: &, <, >, the double quote and the single quote. Each one gets replaced by its entity form:

  • & becomes &amp;, so the browser prints an ampersand instead of starting an entity reference.
  • < becomes &lt; and > becomes &gt;, so tags print as text instead of opening and closing elements.
  • The double quote becomes &quot; and the single quote becomes &#39;, so quoted strings inside the sample cannot break out of an attribute value.

Everything else in the sample, the letters, the numbers, the indentation, passes through untouched. That is what makes the encoded version safe to paste: the browser still sees a page, but the sample inside it has been demoted from instructions to printout.

How to display a code sample, step by step

  1. Copy the code sample you want to show, exactly as readers should see it.
  2. Open the HTML Entity Encoder/Decoder and paste the sample into the input box.
  3. Press Encode entities.
  4. Copy the encoded result from the output box, and paste it into your page where the sample belongs, inside the code block element your page uses for samples.
  5. Open the published page and check the sample renders as text: tags visible, link inert, quotes intact.

Worked example: a two-line snippet, before and after

Say your documentation needs to show this snippet, a line of text and a link:

<p>Read the docs:</p>
<a href="x">Tom & Jerry's</a>

Pasted raw into a page, the p element becomes a real paragraph, the a element becomes a real, clickable link, and the reader never sees the tags you were trying to teach. Run the same snippet through the encoder and it comes back as:

&lt;p&gt;Read the docs:&lt;/p&gt;
&lt;a href=&quot;x&quot;&gt;Tom &amp; Jerry&#39;s&lt;/a&gt;

Read the second line against the five-character rule and every change is accounted for. The angle brackets of the a tag became &lt; and &gt;, so the tag is printout now, not an element. The double quotes around the href value became &quot;, so they cannot terminate an attribute early. The ampersand in the link text became &amp;, and the apostrophe became &#39;. Paste that encoded version into the page and the reader sees the original snippet, character for character, while the browser sees nothing it can act on.

Limitations and honest limits

Encoding does one job: it makes text display as text. Two limits follow from that, and both matter.

First, encoding is not a security filter on its own. It makes a sample you wrote safe to display, but it does not make arbitrary user input safe to publish. In a real application you still escape output in the right context, whether that context is an attribute, a script or a URL, because each context has its own ways of breaking out that five-character encoding was never meant to cover. Treat the encoder as a publishing step for content you control, not as a sanitiser for content you do not.

Second, encoding is not presentation. The encoded sample prints in whatever plain style the surrounding page gives it. Syntax highlighting, the colouring that makes code easier to scan, is a separate tool's job, and this encoder does not do it. If your page already has a highlighter, encoding still comes first: the highlighter needs the sample as text before it can colour it.

Common mistakes

  • Pasting the sample raw and fixing the page afterwards. Once a browser has parsed your sample as markup, the damage is in the rendered page, not the source. Encode first, then paste.
  • Encoding only the tags. A sample with encoded brackets but a raw & can still misprint, because the ampersand starts an entity reference. All five characters need encoding, which is why a single pass through the tool beats hand-editing.
  • Forgetting the quotes. If encoded text goes inside an HTML attribute value, the quotes matter as much as the brackets. This tool encodes both quote types, which keeps attribute contexts safe; a hand-rolled replacement that skips them does not.
  • Encoding an already encoded sample. Run &lt; through the encoder and its ampersand becomes &amp;, so the page prints the literal text &lt; instead of a bracket. Encode raw samples once.
  • Trusting encoding to sanitise user content. Displaying your own sample and defending against hostile input are different problems. The second one needs context-aware escaping in the application, not just an encoder.

FAQs

Why did my code sample disappear from the page? Because the browser parsed it as markup. A pasted tag becomes an element, so instead of seeing the tag as text, the reader sees whatever the element renders, or nothing at all. Encoding the sample first makes the browser print it as text.

Which characters does the encoder change? Five: &, <, >, the double quote and the single quote. Those are the characters that matter for safety and correctness when text is shown inside a page.

Why does my < show as &lt; in the page source? Because that is the point: &lt; tells the browser to display a less-than sign as text instead of starting a tag. Your readers see <, the browser sees an instruction to print it.

Does encoding make user content safe to publish? Not on its own. Encoding is not a security filter by itself: in a real application you still escape output in the right context, whether that is an attribute, a script or a URL.

Is my text uploaded anywhere? No. Encoding and decoding run locally in your browser, and you copy the result from the output box.

Conclusion

A code sample breaks for one reason: the browser cannot tell it apart from the page around it. Encode the five characters the browser acts on, paste the encoded text in place of the raw sample, and the same browser prints your code exactly as you wrote it. When you reach for an entity spelling for a symbol inside that sample, the companion guide on named vs numeric HTML entities covers which form to choose, and the rest of this series lives in the Developer Tools guides archive.