HTML Entities: When Escaping Is Required and When It Is Wrong

Open Tool HTML Entity Encoder & Decoder

How the browser parses

The moment a browser sees < it starts treating the text as markup. If you want the four characters

on screen, writing them raw produces an empty div element instead. Replacing < with <, > with > and & with & tells the parser this is text. Quotes inside attribute values need escaping too, as " and '. These rules come from the HTML specification; they are not optional niceties.

Steps

  1. Paste the content; the sample is a paragraph tag with a class attribute.
  2. Click Convert, and ⇅ flips between encoding and decoding.
  3. Enable non-ASCII to numeric entities to turn Chinese into forms like 中, useful for legacy systems and mail clients with mismatched charsets.
  4. Load example shows a standard case; copy the output when done.

What the non-ASCII option changes

By default only the five named entities are escaped and Chinese stays readable — the shortest, most legible output for a UTF-8 page. With the option on, Chinese becomes decimal entities that render anywhere, at the cost of roughly eight characters per glyph and much harder reading. The right choice depends on the downstream system, not on maximal caution.

Three common misuses

Double encoding is the first: escaping content that already contains & produces &lt; on screen, so users see source code. Decoding into the DOM is the second: turning <script> back into tags and injecting it is self-inflicted cross-site scripting; show code inside textarea or pre instead. Missing escapes are the third: pasting a nickname straight into an attribute lets a quote break out of it, a classic XSS entry. The rule of thumb is escape five characters for text nodes, escape quotes for attributes, and use JSON or CSS escaping inside script and style blocks rather than HTML entities.

⏫︎