logo

Character Entities

1) Need for Character Entities

Sometimes you must show characters that HTML normally uses for its own tags or characters that are invisible or special symbols.

  • Reserved characters: <, >, & are part of HTML tags. If you type them directly, the browser may treat them as code.
  • Invisible spacing: Extra spaces and line breaks collapse in HTML. You need a special space when you want it not to collapse.
  • Symbols/foreign characters: ©, ₹, €, ±, →, etc.
  • Inside attributes: Quotes (" ') may need escaping.

Even with UTF-8 (<meta charset="UTF-8">)—which lets you type most symbols directly—you must still escape <, >, and & when they appear as text.


2) Common Entities

PurposeEntity (named)Numeric (decimal)HexShows
Less-than&lt;&#60;&#x3C;<
Greater-than&gt;&#62;&#x3E;>
Ampersand&amp;&#38;&#x26;&
Non-breaking space&nbsp;&#160;&#xA0;␠ (no wrap)
Double quote&quot;&#34;&#x22;"
Single quote/apostrophe&apos;*&#39;&#x27;'
Rupee sign&#8377;&#x20B9;
Copyright&copy;&#169;&#xA9;©
Registered&reg;&#174;&#xAE;®
Trade mark&trade;&#8482;&#x2122;
En dash&ndash;&#8211;&#x2013;
Em dash&mdash;&#8212;&#x2014;
Euro&euro;&#8364;&#x20AC;

* &apos; wasn’t in old HTML4 but works in modern browsers. &#39; is safest.


3) Using Entities in HTML Pages

A) Showing angle brackets or ampersands in text

<p>Use &lt;h1&gt; for the main heading and write &amp; to show an ampersand.</p>

B) Writing math or comparisons

<p>If x &lt; y &amp; y &lt; z, then x &lt; z.</p>

C) Preventing line breaks or extra-space collapse

  • Normal spaces collapse: A B → “A B”
  • Use non-breaking space to keep spacing or to stick words together:
<p>Roll&nbsp;No.&nbsp;23</p>
<!-- stays on one line -->
<p>Marks:&nbsp;98/100</p>

Tip: Don’t use &nbsp; for layout. Prefer CSS margins/padding.

D) Inside attributes

<p title='He said "Hello" to me'>Hover me</p>

E) Symbols and currency

<p>Price: &#8377; 499 &mdash; Limited offer!</p>

F) Named vs Numeric

  • Named (&copy;) are easy to remember.
  • Numeric (&#169; or &#xA9;) cover any Unicode character, even if no name exists.

Mini Cheat Sheet (exam-ready)

  • Always escape: &lt; &gt; &amp;

  • Non-breaking space: &nbsp; (prevents wrapping)

  • Quotes in attributes: &quot; and &#39;

  • Use UTF-8:

    <meta charset="UTF-8" />
    

    Then type symbols directly when safe; still escape <, >, &.