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:WHATWG Named Character References
MDN: HTML entitiesRFC 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

📌 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

  • Named entity: &lt; → less-than sign
  • Decimal numeric: &#60; → same less-than sign
  • Hexadecimal numeric: &#x3C; → same less-than sign

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

FAQ: Common Questions

Q: What are HTML entities?

HTML entities are a way of representing HTML reserved characters with specific codes. `<` means `<`, `&` means `&`, `©` means ©.

Q: When are HTML entities needed?

Entities must be used when displaying reserved characters such as < > & in HTML content. JavaScript strings, URL parameters, and attribute values ​​all need to be encoded correctly.

Q: What is the difference between HTML entities and Unicode?

HTML entities are HTML-specific encodings (such as `©` = ©), and Unicode is a character set standard (U+00A9 = ©). HTML entities can reference Unicode characters.

🧰
Add to Home Screen
Works offline, launches instantly