Command Palette

Search for a command to run...

Guides / 5 min read

HTML Entity Encoding: When You Actually Need It

Entity encoding is useful for displaying literal markup, but it should not be used as a vague substitute for real escaping or sanitization rules.

What entity encoding is for

HTML entity encoding turns special characters like angle brackets and ampersands into their display-safe forms. That is useful when you want to show literal markup in docs, examples, or previews.

It helps the browser display the characters themselves instead of interpreting them as HTML.

Where developers misuse it

Problems start when entity encoding gets used as a generic fix for every HTML-related bug. It can solve display issues, but it does not replace understanding your rendering context.

If the underlying problem is unsafe HTML handling, encoding only one layer of the output may not be enough.

  • Use it to display literal markup.
  • Do not confuse it with full sanitization.
  • Always think about the actual rendering context.

A practical rule

Encode when the goal is to show characters literally. Sanitize when the goal is to allow safe rendering of user-controlled content. Those are related but different tasks.

Keeping that distinction clear prevents a lot of messy debugging.

FAQ