Menu

The five characters that break your HTML (and the one habit that fixes them)
📰

The five characters that break your HTML (and the one habit that fixes them)

DEV Community: html·Mubeen Khan·6 days ago
#4vtHLL4f
#devto2#devto#dev#entities#output
Reading 0:00
15s threshold

You have done this. You paste a code snippet into a blog post, a comment box, or a CMS field, hit save, and half of it vanishes. The <div> you typed is gone. The & in "Tom & Jerry" turned into something weird. A <script> tag someone typed into a form actually ran.

It is not a bug in your editor. It is HTML doing exactly what it was designed to do: angle brackets mean "a tag starts here", and ampersands mean "an entity starts here". When you want those characters to show up as plain text, you have to tell the browser so, by writing them as HTML entities.

The five characters that matter

Almost every encoding problem comes down to five characters:

& becomes &amp;

< becomes &lt;

> becomes &gt;

" becomes &quot;

' becomes &#39;

That is the whole list for safe output. Encode those five and your text displays exactly as typed, in every browser, with no surprises.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.