HTML Entities: All Special Characters + XSS Defense

Want to display < on a web page? Typing it directly makes the browser think you're opening a tag — you need to write &lt; instead. That's an HTML entity. HTML entities are fundamental front-end knowledge and your first line of defense against XSS attacks. This guide covers named vs numeric entities, a 15-character cheat sheet, and 3 real XSS scenarios. Check the DevToolbox HTML Entities Reference for the full table with search and copy.

✍️ Author:DevToolbox Team📅 Updated:2026-06-24📎 References:RFC Standards

📌 Key Takeaways

  • HTML Entities: All Special Characters + XSS Defens is widely used by developers
  • Based on RFC standards and real-world experience
  • Free online tools, runs locally, no data upload
  • FAQ section at the bottom answers common questions
✍️ Author:DevToolbox Team📅 Updated:2026-06-24📎 References:RFC Standards

📌 Key Takeaways

  • HTML Entities: All Special Characters + XSS Defens is widely used by developers
  • Based on RFC standards and real-world experience
  • Free online tools, runs locally, no data upload
  • FAQ section at the bottom answers common questions

1. Three Ways to Write an HTML Entity

All three are equivalent. Named entities are easier to remember; numeric entities cover every Unicode character.

2. 15 Must-Know Entities

CharNamedDecimalWhen to Use
<&lt;&#60;Display <
>&gt;&#62;Display >
&&amp;&#38;Display &
"&quot;&#34;In attributes
non-breaking space&nbsp;&#160;Prevent collapse
©&copy;&#169;Copyright
®&reg;&#174;Registered
&trade;&#8482;Trademark
&mdash;&#8212;Em dash

3. Three XSS Scenarios

User Input Injected Into HTML

// ❌ Dangerous: raw user input concatenation
element.innerHTML = userInput;

// ✅ Safe: textContent auto-escapes
element.textContent = userInput;

Unfiltered URL Parameters

// Always sanitize external input before injecting into HTML
const safe = DOMPurify.sanitize(new URLSearchParams(location.search).get('name'));
document.write(`<h1>Welcome ${safe}</h1>`);

Template Engine Auto-Escape Override

Vue/React auto-escape by default, but v-html / dangerouslySetInnerHTML bypass this. Only use with 100% trusted content. External input must always go through DOMPurify first.

4. Why Use &nbsp; Instead of a Space?

HTML collapses consecutive whitespace by default — 3 spaces display as 1. Use &nbsp; (non-breaking space) to preserve spacing. Common in paragraph indentation, price alignment (¥99&nbsp;元), and preventing line breaks between related words.

5. Summary

Memorize the first four reserved characters (&lt; &gt; &amp; &quot;) and you'll handle 90% of HTML entity use cases. For XSS, remember the golden rule: escape or sanitize every piece of user input. Browse the full table at the DevToolbox HTML Entities Reference — 200+ characters with search and one-click copy.

Related: HTML Entities · URL Codec · Regex Tester

🔗 Share: 𝕏 📘 ✈️ 💬

FAQ: Common Questions

Q: HTML 实体是什么?

HTML 实体(HTML entities)是用特定代码表示 HTML 保留字符的方法。`<` 表示 `<`,`&` 表示 `&`,`©` 表示 ©。

Q: 什么时候需要 HTML 实体?

在 HTML 内容中显示 < > & 等保留字符时必须用实体。JavaScript 字符串、URL 参数、属性值都需要正确编码。

Q: HTML 实体和 Unicode 有什么区别?

HTML 实体是 HTML 专用编码方式(如 `©` = ©),Unicode 是字符集标准(U+00A9 = ©)。HTML 实体可以引用 Unicode 字符。

🧰
Add to Home Screen
Works offline, launches instantly