← Back to Chapters

HTML Special Characters

? HTML Special Characters

⚡ Quick Overview

In HTML, some characters have special meanings and cannot be written directly without confusing the browser. For example, < and > are used for tags, and & starts an entity. To display these as normal text, we use HTML character entities such as &lt; or &amp;. These tell the browser to show the character instead of treating it as HTML code.

Entities are also helpful for symbols that are not on the keyboard (©, ™, €) or for controlling spacing (like non-breaking spaces). Knowing a few common entities makes your HTML examples safe and readable.

? Key Concepts

  • Special characters: Characters that HTML uses for its own syntax (like <, >, &).
  • Entities: Text codes that represent characters, starting with & and ending with ;.
  • Named entities: Human-friendly names such as &lt; or &copy;.
  • Numeric entities: Use Unicode code points like &#169; (©).
  • Attributes safety: Entities prevent HTML from breaking when you need quotes or symbols inside attributes.
  • Non-breaking space: &nbsp; keeps text together on one line.

? Common Special Characters and Their Codes

  • Less than: &lt;<
  • Greater than: &gt;>
  • Ampersand: &amp;&
  • Quotation marks: &quot;"
  • Apostrophe: &apos;'
  • Non-breaking space: &nbsp;
  • Copyright: &copy; → ©
  • Registered: &reg; → ®
  • Trademark: &trade; → ™
  • Euro symbol: &euro; → €
  • Em dash: &mdash; → —

? Code Example: Entities in Text & Attributes

? View Code Example
<!-- Display less-than / greater-than symbols inside text -->
<p>If x &lt; 10 and x &gt; 5, then...</p>

<!-- Show common symbols -->
<p>&copy; 2025 MySite &mdash; All rights reserved.</p>
<p>Trademark: &trade; &nbsp; Registered: &reg;</p>

<!-- Use entities safely inside attribute values -->
<img alt="2 &lt; 3 is true" src="icon.png">

<!-- Quotes inside attributes -->
<button title="She said &quot;Hello!&quot;">Hover me</button>

? Live Output / Explanation

?️ Rendered Result

If x < 10 and x > 5, then...

© 2025 MySite — All rights reserved.

Trademark: ™   Registered: ®

She said, "Hello!"

Notice how the code safely shows symbols and quotes without breaking the HTML structure. The browser receives entities in the source, but you only see the final characters.

? Tips & Best Practices

  • When writing text that includes <, >, or &, use &lt;, &gt;, and &amp; in the source.
  • Inside attributes, escape quotes with &quot; or wrap the attribute with the opposite quote type.
  • Use &nbsp; to prevent line breaks in small fragments (e.g., “© 2025”), but prefer CSS for layout spacing.
  • Numeric references like &#169; (©) also work if you can’t remember the named entity.
  • Always include <meta charset="UTF-8"> so Unicode characters (€, ✓) render correctly.
  • Escape & in query strings when writing raw HTML (for example, ?q=cats&amp;lang=en in the source).
  • Avoid “double escaping” like &amp;lt; unless you really want to show the entity code itself.
  • Use straight quotes in code examples and avoid “smart quotes” that may not be parsed correctly.

? Try It Yourself

  • Write a paragraph that literally shows <div class="box"> using the correct entities so it doesn’t become a real element.
  • Create a link with a query string (e.g., ?q=cats&lang=en) and ensure the & is written as &amp; in the HTML source.
  • Add a button whose title attribute contains quotes using &quot; (for example: title="She said &quot;Hi!&quot;").
  • Replace any visual spacing made with multiple spaces by proper CSS margin/padding, and keep &nbsp; only where a non-breaking space is required (e.g., between a number and its unit).

? Summary

  • HTML special characters like <, >, and & must be escaped using entities.
  • Named entities (like &copy;) and numeric entities (like &#169;) both represent symbols.
  • Entities keep your examples safe in both text and attribute values.
  • Use &nbsp; for non-breaking spaces, but rely on CSS for general layout and spacing.
  • Correct escaping prevents broken HTML, unexpected rendering, and confusing bugs in your pages.