Lesson 12: Character Entity References

Master the use of HTML entities to display special characters and symbols on your web pages

Start Learning

Understanding Character Entities

HTML provides character entity references to display special characters that are reserved for use in HTML or that don't appear on standard keyboards. These entities ensure your content displays correctly across all browsers and devices.

Key Insight: Character entities are essential for displaying symbols, mathematical operators, currency signs, and characters from different languages. They also prevent browsers from interpreting special characters as HTML code.

Why Use Character Entities?

HTML reserves certain characters for its syntax. For example:

  • The less-than sign (<) is used to start HTML tags
  • The ampersand (&) is used to start entity references
  • Quotation marks (") are used for attribute values

To display these characters as content, you must use their corresponding HTML entities.

Important: Forgetting to encode special characters can break your page layout or cause security vulnerabilities (like cross-site scripting attacks).

Essential Character Entities

Here are the most commonly used character entities in HTML:

Common HTML Character Entities
Character Entity Name Entity Number Description Usage
< &lt; &#60; Less than Prevent HTML tag interpretation
> &gt; &#62; Greater than Prevent HTML tag interpretation
& &amp; &#38; Ampersand Prevent entity interpretation
" &quot; &#34; Double quotation mark Inside attribute values
' &apos; &#39; Single quotation mark Inside attribute values
  &nbsp; &#160; Non-breaking space Prevent line breaks between words
© &copy; &#169; Copyright symbol Copyright notices
® &reg; &#174; Registered trademark Trademark notices
&euro; &#8364; Euro currency Financial content

Using Entities in Practice

Let's see how to use character entities in real HTML code:

Basic Entity Usage

<p>To display HTML tags, use entities: &lt;div&gt;Content&lt;/div&gt;</p>
<p>Copyright &copy; 2023 My Company. All rights reserved.</p>
<p>The price is 100&euro; &plus; 20&percnt; VAT.</p>
<p>Special characters: &hearts; &diams; &clubs; &spades;</p>
<p>Mathematical operators: &sum; &prod; &radic; &infin;</p>

Result:

To display HTML tags, use entities: <div>Content</div>

Copyright © 2023 My Company. All rights reserved.

The price is 100€ + 20% VAT.

Special characters: ♥ ♦ ♣ ♠

Mathematical operators: ∑ ∏ √ ∞

Advanced Entity Usage

<!-- Displaying code examples -->
<pre>
function calculate(a, b) {
    return a &lt; b ? a : b;
}
</pre>

<!-- Using entities in attributes -->
<input type="text" placeholder="Search &hellip;">

<!-- Creating non-breaking spaces -->
<p>Dr.&nbsp;Liew Voon&nbsp;Kiong</p>

<!-- Currency symbols -->
<p>Price: &dollar;49.99 | &pound;39.99 | &yen;5499</p>

Result:

function calculate(a, b) {
    return a < b ? a : b;
}

Dr. Liew Voon Kiong

Price: $49.99 | £39.99 | ¥5499

Special Characters & Symbols

HTML provides entities for thousands of special characters. Here are some useful categories:

Common Symbols

©
&copy;
Copyright
®
&reg;
Registered
&trade;
Trademark
&euro;
Euro
£
&pound;
Pound
¥
&yen;
Yen
¢
&cent;
Cent
§
&sect;
Section

Mathematical Symbols

&sum;
Sum
&infin;
Infinity
&radic;
Square Root
&int;
Integral
±
&plusmn;
Plus/Minus
×
&times;
Multiply
÷
&divide;
Divide
&ne;
Not Equal

Arrows & Shapes

&larr;
Left Arrow
&rarr;
Right Arrow
&uarr;
Up Arrow
&darr;
Down Arrow
&harr;
Left-Right Arrow
&hearts;
Heart
&diams;
Diamond
&clubs;
Club

Practical Exercise: Using Entities

Create a webpage section using HTML entities with the following requirements:

Your Task:

  1. Create a copyright notice: © 2023 Your Company Name
  2. Display a mathematical equation: 5 × 3 < 20 ÷ 2
  3. Show currency values: $49.99, €44.99, ¥5499
  4. Include a registered trademark: HTML Tutor®
  5. Create a navigation indicator: Home → Products → Details
  6. Add a decorative element: I ♥ HTML!

Tips:

  • Use &copy; for the copyright symbol
  • Use &times; for multiplication and &divide; for division
  • Remember to encode less-than signs: &lt;
  • Use &reg; for the registered trademark
  • Use arrow entities: &rarr;
  • Use &hearts; for the heart symbol