Chapter 19 of 26

Character Entities & Symbols

Try this experiment: you're writing an HTML tutorial (like this one!) and want the reader to see the text <p> on the screen. If you simply type <p> in your file, the browser doesn't show it. It thinks you're starting a new paragraph.

The same problem appears with the & sign, with quotes inside attribute values, and with characters that are hard to type or invisible, like a non-breaking space. HTML solves all of these with character references, usually called entities.

A character reference is a code that starts with & and ends with ;, which the browser replaces with a single character. For example, &lt; displays as < and &copy; displays as ©.


Why Do We Need Entities?

A few characters have a special meaning to the HTML parser:

CharacterSpecial meaning
<Starts a tag
&Starts a character reference
" or 'Ends an attribute value (inside a quoted attribute)
>Ends a tag (safe in normal text, but usually escaped for symmetry)

When you want these characters to appear as text, you write their entity instead. This is called escaping.

You type in the file:   5 &lt; 10 &amp;&amp; 10 &gt; 5
Browser shows:          5 < 10 && 10 > 5

Three Ways to Write a Character

Every character has a number in Unicode, called its code point. The copyright sign © is U+00A9 (hex A9 = decimal 169). You can write it in three ways:

FormSyntaxExample for ©
Named&name;&copy;
Decimal&#number;&#169;
Hexadecimal&#xhex;&#xA9;

All three display the same ©. Named references are easier to read; numeric references work for any Unicode character, even ones with no name.

A great example for Indian developers: the rupee sign ₹ (U+20B9) has no named entity. &rupee; does not exist. You must type ₹ directly or write &#8377; (decimal) or &#x20B9; (hex).

HTML defines 2,231 named character references. 2,125 of them end with a semicolon; the other 106 are old forms without a semicolon kept for backward compatibility (like &copy). You only need to remember a handful.


A Complete Example

Save this as entities.html:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Entities Demo</title>
</head>
<body>
  <p>To make a paragraph, write &lt;p&gt;Hello&lt;/p&gt; in your file.</p>
  <p>Tom &amp; Jerry, 5 &lt; 10, 10 &gt; 5, she said &quot;hi&quot; and it&apos;s fine.</p>
  <p>&copy; 2026 CS Learning Portal &middot; Price: &#8377;499 &middot; Smile: &#128512;</p>
  <p>Same symbol three ways: &copy; &#169; &#xA9;</p>
  <p>Maths: 3 &times; 4 = 12, 10 &divide; 2 = 5, x &le; y, a &ne; b, &pi; &asymp; 3.14</p>
  <p>Arrows: &larr; &rarr; &uarr; &darr; &hArr;</p>
  <p>Case matters: &alpha; is not &Alpha;</p>
  <p>Typing &amp;nbsp; vs &amp;NBSP;: [&nbsp;] vs [&NBSP;]</p>
</body>
</html>

Output in the browser:

To make a paragraph, write <p>Hello</p> in your file.

Tom & Jerry, 5 < 10, 10 > 5, she said "hi" and it's fine.

© 2026 CS Learning Portal · Price: ₹499 · Smile: 😀

Same symbol three ways: © © ©

Maths: 3 × 4 = 12, 10 ÷ 2 = 5, x ≤ y, a ≠ b, π ≈ 3.14

Arrows: ← → ↑ ↓ ⇔

Case matters: α is not Α

Typing &nbsp; vs &NBSP;: [ ] vs [&NBSP;]

Look at the last two lines carefully:

  • Entity names are case-sensitive. &alpha; is the Greek small letter α, while &Alpha; is the capital Α (which looks just like the Latin letter A).

  • &NBSP; is not a real entity, so the browser shows it literally as the text "&NBSP;". A validator reports it as an unrecognised character reference. (A few entities, like &COPY; and &AMP;, happen to have uppercase versions too, but don't rely on that; use the lowercase forms.)

  • In that same last line we wanted to show the text "&nbsp;", so we escaped its & as &amp;.


The Most Useful Entities

Must-Know: The Syntax Characters

CharacterNamedDecimalDescription
<&lt;&#60;Less than
>&gt;&#62;Greater than
&&amp;&#38;Ampersand
"&quot;&#34;Double quote
'&apos;&#39;Apostrophe (single quote)
(invisible)&nbsp;&#160;Non-breaking space

&apos; is valid in HTML5. It wasn't defined in HTML 4, which is why older code uses &#39;.

Symbols

CharacterNamedNumericDescription
©&copy;&#169;Copyright
®&reg;&#174;Registered trademark
™&trade;&#8482;Trademark
₹(none)&#8377;Indian rupee
€&euro;&#8364;Euro
£&pound;&#163;Pound
°&deg;&#176;Degree
…&hellip;&#8230;Ellipsis
–&ndash;&#8211;En dash (ranges: 10–20)
—&mdash;&#8212;Em dash
“ ”&ldquo; &rdquo;&#8220; &#8221;Curly double quotes
•&bull;&#8226;Bullet
·&middot;&#183;Middle dot
✓&check;&#10003;Check mark
♥&hearts;&#9829;Heart

Maths, Greek and Arrows

CharacterNamedCharacterNamed
×&times;÷&divide;
±&plusmn;≠&ne;
≤&le;≥&ge;
≈&asymp;∞&infin;
√&radic;∑&sum;
½&frac12;π&pi;
α&alpha;β&beta;
←&larr;→&rarr;
↑&uarr;↓&darr;
⇒&rArr;⇔&hArr;

Tip: the name usually describes the shape. larr = left arrow, rArr = right double arrow (capital A for "double"), le = less than or equal, ne = not equal.


The Non-Breaking Space

You've probably noticed that HTML collapses whitespace: several spaces in a row show as just one, and the browser can wrap a line at any space. The non-breaking space &nbsp; is different in both ways:

  1. The browser will never break the line at it, so the words on either side stay together.

  2. It is not collapsed with neighbouring spaces.

Use it to keep things together that would look wrong split across two lines:

<p>The campus is 25&nbsp;km from the station.</p>
<p>Contact Dr.&nbsp;Sharma for the lab manual.</p>
<p>The fee is ₹&nbsp;45,000 per semester.</p>
Without &nbsp; (bad wrap):       With &nbsp;:
...is 25                         ...is
km from the station.             25 km from the station.

Don't use a row of &nbsp;&nbsp;&nbsp; to indent text or push things apart. That's layout, and layout is CSS's job (margin, padding, text-indent). Spacing with &nbsp; breaks on different screen sizes and confuses screen readers.

Other Invisible Helpers

ReferenceWhat it does
&shy;Soft hyphen: an invisible "you may break here" point. If the word is broken at that point, a hyphen appears
&#8203;Zero-width space: allows a line break with no hyphen (the <wbr> element does a similar job)
&ensp;, &emsp;, &thinsp;Spaces of specific widths (used in typography)

Do You Still Need Entities in 2026?

Mostly no, thanks to UTF-8. Since your page declares <meta charset="utf-8"> (see the HTML Document Structure chapter) and your editor saves the file as UTF-8, you can type ©, ₹, ×, →, Hindi, Tamil or emoji directly:

<p>© 2026 · Price: ₹499 · नमस्ते · 😀</p>

This is easier to read than a line full of &#...; codes, and it's what most modern sites do.

You still need entities for:

SituationExample
< and & in text&lt;div&gt;, R&amp;D
The quote character inside an attribute that uses the same quotetitle="She said &quot;yes&quot;"
Invisible characters you want to be visible in the source&nbsp;, &shy;
Characters you can't easily type, if you prefer&rarr;, &#8377;

Where Entities Don't Work

Entities are decoded in normal text and in attribute values, but not inside <script> and <style>. Their content is raw text that goes straight to the JavaScript or CSS engine:

<script>
  console.log("5 &lt; 10");   // prints the literal text: 5 &lt; 10
  if (5 < 10) { }               // a real < is fine here
</script>

Elements like <title> and <textarea> are in between: tags inside them are treated as plain text, but entities are decoded.


Always Write the Semicolon

For historical reasons, browsers accept some old entities without the semicolon. That causes surprises:

<p>&copy2026 CSLP</p>               <!-- shows: ©2026 CSLP -->
<p>I'm &notit; I tell you</p>      <!-- shows: I'm ¬it; I tell you -->

In the second line, &notit; is not an entity, but the browser finds the legacy entity &not (¬) at its start and replaces it. Writing every entity with its semicolon, and escaping every literal & as &amp;, avoids all such bugs. This matters a lot in URLs inside href:

<a href="search.php?topic=trees&amp;level=gate">BST questions</a>

(Browsers are forgiving here: inside an attribute, a legacy entity followed by = or a letter is not decoded, so a raw href="page?a=1&copy=2" stays exactly as typed, while the same &copy2026 in normal text became ©2026. Rather than memorising such rules, write &amp;; it is always correct.)


Escaping and Security (XSS)

Escaping isn't just about looks. Suppose a comment box shows whatever users type, inserted into the page without escaping. A user submits this "comment":

<script>stealCookies()</script>

Every visitor's browser would run it. This is Cross-Site Scripting (XSS), one of the most common web vulnerabilities. If the server escapes the comment before inserting it, the page source contains:

&lt;script&gt;stealCookies()&lt;/script&gt;

and visitors just see the harmless text. The rule: always escape user-supplied text (at least &, <, >, " and ') before putting it into HTML. Templating engines and frameworks like React do this for you automatically.


Common Mistakes

  • Forgetting the semicolon, as in &copy 2026. It may work, but it's sloppy and can produce surprises.

  • Wrong case: &NBSP; or &Nbsp; instead of &nbsp;.

  • Inventing names like &rupee;. Use ₹ or &#8377;.

  • Using &nbsp; for layout. Use CSS spacing.

  • Unescaped < when showing code, so the browser treats your example as real tags.

  • Expecting entities to work in <script> or <style>.

  • Inserting user input without escaping, which leads to XSS.


Interview Questions

Q1. What are the three ways to write the character ©, and what does each mean?

Named: &copy;. Decimal: &#169; (the code point in base 10). Hexadecimal: &#xA9; (the code point U+00A9 in base 16). All render the same character.

Q2. What is the difference between a normal space and &nbsp;?

A normal space can be a line-break point and is collapsed with neighbouring whitespace. A non-breaking space (U+00A0) is never used as a line-break point and is not collapsed, so it keeps the words on either side together.

Q3. Which characters must you escape when inserting user text into HTML, and why?

<, >, &, and both quote characters (when inside attributes). Otherwise user input can create real tags or break out of attributes, allowing Cross-Site Scripting (XSS).

Q4. What does the browser display for &amp;lt;?

It displays &lt; as text. The &amp; becomes &, and the following lt; is ordinary text, because character references are decoded only once.

Q5. With UTF-8, do we still need &copy;?

No. With UTF-8 you can type © directly. Entities remain necessary for HTML syntax characters (<, &, quotes in attributes) and are handy for invisible characters like &nbsp;.


Conclusion

Character references let you write characters that HTML would otherwise misread, or that are hard to type: &name;, &#decimal; or &#xhex;.

  • Always escape < and & in text, and the matching quote inside attributes.

  • Names are case-sensitive and need a semicolon.

  • ₹ has no name: type it or use &#8377;.

  • &nbsp; keeps words together; it isn't a layout tool.

  • With UTF-8, type normal symbols directly.

  • Escaping user input prevents XSS.

Next, in Responsive Images: srcset & picture, we'll return to images and learn how to send the right size and format of image to every screen.