An entity is a way of writing a character using only plain letters. It exists because a few characters are part of the syntax of HTML, so writing them literally would confuse the parser. Modern encoding has made most entities unnecessary, but a small set remains genuinely required.
The four that actually matter
These characters mean something to the parser, so they must be escaped when they appear as content.
- The less than sign becomes < because a literal one starts a tag.
- The greater than sign becomes > for symmetry, and it is required inside attribute values.
- The ampersand becomes & because a literal one starts an entity.
- The double quote becomes " when it appears inside an attribute value delimited by double quotes.
Every entity begins with an ampersand and ends with a semicolon. Forgetting the semicolon is the most common error and usually results in the raw text appearing on the page.
Showing code on a page
This is where escaping becomes unavoidable. To display markup as text rather than have it render, every angle bracket must be escaped.
<p>Write a link like this: <a href="/about/">About</a></p> Miss one and the browser will interpret it, so your example becomes a real link and the surrounding structure quietly breaks. Any code editor or documentation tool has a function to escape a block automatically, and using it is far more reliable than working through by hand.
The ampersand is the one people forget
An unescaped ampersand is technically an error everywhere it appears, and it matters most in URLs containing query parameters.
<a href="/search?q=html&page=2">Next page</a> Written unescaped, the parser may attempt to interpret what follows as an entity. If the parameter name happens to match a known entity, the character silently changes and your link stops working in a way that is difficult to spot by reading the source.
Most other entities are no longer needed
Entities for accented letters, currency symbols, arrows and dashes date from an era when documents were sent in limited character sets. With a UTF-8 declaration in your head and your editor saving as UTF-8, you can simply type these characters directly. A euro sign, an accented e, an em dash and an arrow all work as themselves.
Typing the character is more readable in source and less error prone than remembering an entity name. The important condition is the charset declaration, which must be present and correct, or the literal characters will be misinterpreted and appear as nonsense.
The non breaking space is misused constantly
The non breaking space renders as a space that does not collapse and does not allow a line break. Because ordinary whitespace collapses, it gets pressed into service as a spacing tool.
This is the wrong job for it. Using several to indent text or push elements apart produces markup that is fragile, invisible in editors, impossible to adjust responsively and confusing for screen readers, which may announce each one. Spacing is a presentational concern and belongs in CSS, where margin and padding do the job properly.
The legitimate uses are narrow and worth knowing. It prevents an awkward break between a number and its unit, keeps a name and title together, and stops a single word dangling at the end of a heading.
<p>The file is 20 MB in size.</p> Numeric references cover everything else
Every character has a numeric code that can be written directly, either in decimal or in hexadecimal.
© decimal © hexadecimal © named All three produce a copyright symbol. Named references are more readable where one exists, and numeric references cover the many characters that have no name. Both are safe fallbacks when you are unsure whether an encoding will survive a particular publishing pipeline.
Entities inside attribute values
Attribute values follow the same rules as text content, and the character to watch is whichever quote mark delimits the value. A double quoted value containing a double quote ends early, leaving the remainder of your text parsed as further attributes. Escaping it, or switching the delimiter to single quotes, resolves it. Angle brackets inside attribute values should be escaped as well, since some parsers and many validators treat them as errors even where a browser copes.
Escaping is a security measure too
The reason escaping matters beyond appearance is that any user supplied text placed onto a page unescaped can contain markup, including a script tag. Escaping the angle brackets is what turns that from executable code into visible text. Every server side language has a function for it, and applying it to anything that came from a user is not optional. Entities are not merely a typography concern.