Orlixio

7 min read

HTML entities explained

HTML entities let special characters display as text instead of being interpreted as markup.

Table of contents

  1. Entity basics
  2. Escaping text for display
  3. When decoding helps
  4. Security boundaries

Entity basics

HTML entities give special characters a safe text representation. The less-than character can start a tag, so writing < lets the browser display the character instead of treating it as markup. Ampersands, quotes, and apostrophes are also common escaping targets.

Entities are especially useful in documentation and code examples. If a guide wants to show a button tag, the tag must be escaped or the browser may render it as an actual element.

Escaping text for display

When you want text to appear exactly as written, escape the characters that HTML treats as syntax. This is common in static pages, examples, comments, and templates that render user-provided text.

Modern frameworks usually escape text nodes by default, which is one reason string interpolation in React is safer than setting raw HTML. The risky point is when code intentionally injects HTML.

When decoding helps

Decoded entities are easier to read and edit. Copied content from a page source, CMS, or email template may include entities that obscure the actual text. Decoding helps you inspect the original characters.

After decoding, be careful where you paste the result. If the decoded text contains markup-like characters, it may need to be escaped again before display in HTML.

Security boundaries

Encoding text is not the same as safely allowing arbitrary HTML. If your application accepts rich HTML from users, use a trusted sanitizer that understands tags, attributes, protocols, and browser parsing behavior.

For simple text display, entity encoding is a useful part of output escaping. For complex HTML input, treat sanitization as a separate requirement.

Related guides

FAQ

What is an HTML entity?

An HTML entity is a text representation of a character, such as &lt; for <.

Why encode angle brackets?

Encoding angle brackets prevents text from being parsed as HTML tags.

Is entity encoding the same as sanitization?

No. Sanitization is a broader security process for untrusted HTML.

Can entities be decoded?

Yes. Browsers can decode named and numeric HTML entities back into characters.