Skip to content

10. HTML Entities

HTML entities are special codes used to represent characters that have special meaning in HTML or characters that are difficult to type directly. They allow you to display reserved characters, special symbols, and characters from various languages and character sets. Understanding HTML entities is essential for creating properly formatted and accessible web content.

HTML entities are sequences of characters that represent other characters. They start with an ampersand (&) and end with a semicolon (;).

&entity_name;
&#entity_number;
  • Named entities: & (ampersand)
  • Numeric entities: & (decimal) or & (hexadecimal)

Some characters have special meaning in HTML and must be escaped:

<!-- These would break HTML if used directly -->
<p>Price: $10 < $20</p> <!-- < breaks HTML -->
<p>He said "Hello"</p> <!-- Quotes may cause issues -->
<p>Tom & Jerry</p> <!-- & starts an entity -->

Solution:

<p>Price: $10 &lt; $20</p> <!-- &lt; for < -->
<p>He said &quot;Hello&quot;</p> <!-- &quot; for " -->
<p>Tom &amp; Jerry</p> <!-- &amp; for & -->

Display characters that aren’t on your keyboard:

<p>&copy; 2024</p> <!-- Copyright symbol -->
<p>&reg; Trademark</p> <!-- Registered trademark -->
<p>&trade; Brand</p> <!-- Trademark symbol -->
<p>&euro; 100</p> <!-- Euro symbol -->
<p>&pound; 50</p> <!-- Pound symbol -->

Entities help ensure characters display correctly across different systems and browsers.

CharacterEntity NameEntity NumberDescription
<&lt;&#60;Less than
>&gt;&#62;Greater than
&&amp;&#38;Ampersand
"&quot;&#34;Double quote
'&apos;&#39;Apostrophe (single quote)
CharacterEntity NameEntity NumberDescription
©&copy;&#169;Copyright
®&reg;&#174;Registered trademark
&trade;&#8482;Trademark
&euro;&#8364;Euro
£&pound;&#163;Pound
¥&yen;&#165;Yen
§&sect;&#167;Section
&para;&#182;Paragraph
&bull;&#8226;Bullet
&hellip;&#8230;Ellipsis
&mdash;&#8212;Em dash
&ndash;&#8211;En dash
°&deg;&#176;Degree
±&plusmn;&#177;Plus-minus
×&times;&#215;Multiplication
÷&divide;&#247;Division
½&frac12;&#189;One half
¼&frac14;&#188;One quarter
¾&frac34;&#190;Three quarters
CharacterEntity NameEntity NumberDescription
&nbsp;&#160;Non-breaking space
&ensp;&#8194;En space
&emsp;&#8195;Em space
&thinsp;&#8201;Thin space
CharacterEntity NameEntity NumberDescription
&infin;&#8734;Infinity
&sum;&#8721;Summation
&prod;&#8719;Product
&radic;&#8730;Square root
&int;&#8747;Integral
&asymp;&#8776;Approximately equal
&ne;&#8800;Not equal
&le;&#8804;Less than or equal
&ge;&#8805;Greater than or equal
CharacterEntity NameEntity NumberDescription
&larr;&#8592;Left arrow
&rarr;&#8594;Right arrow
&uarr;&#8593;Up arrow
&darr;&#8595;Down arrow
&harr;&#8596;Left-right arrow
<p>Copyright &copy; 2024 My Company</p>
<p>Price: &euro;29.99</p>
<p>5 &times; 3 = 15</p>
<p>Temperature: 25&deg;C</p>
<img src="photo.jpg" alt="Tom &amp; Jerry">
<a href="/page" title="Price &lt; $100">Link</a>

When displaying HTML code in content:

<p>Use <code>&lt;div&gt;</code> for containers.</p>
<p>Escape ampersands: <code>&amp;amp;</code></p>

You can use numeric codes instead of named entities:

<p>&#169; Copyright</p> <!-- Same as &copy; -->
<p>&#38; Ampersand</p> <!-- Same as &amp; -->
<p>&#xA9; Copyright</p> <!-- Same as &copy; -->
<p>&#x26; Ampersand</p> <!-- Same as &amp; -->
<footer>
<p>&copy; 2024 Company Name. All rights reserved.</p>
<p>Product&reg; and Brand&trade; are trademarks.</p>
</footer>
<p>Area = &pi;r&sup2;</p>
<p>5 &times; 3 = 15</p>
<p>Temperature: 25&deg;C</p>
<p>x &le; 10</p>
<p>He said, &quot;Hello, world!&quot;</p>
<p>It&apos;s a beautiful day.</p>
<p>Price: &euro;29.99</p>
<p>Cost: &pound;50.00</p>
<p>Amount: &yen;1000</p>
  1. Reserved characters: Always escape <, >, & in content
  2. Special symbols: Use entities for symbols not on keyboard
  3. Consistency: Use entities for consistent display across browsers
  4. Accessibility: Ensure screen readers can interpret characters
  1. Regular text: Don’t use entities for normal letters and numbers
  2. UTF-8 encoding: With proper UTF-8, many characters can be typed directly
  3. Over-encoding: Don’t encode characters unnecessarily

With UTF-8 encoding, you can often type characters directly:

<!-- Both work with UTF-8 -->
<p>&copy; 2024</p>
<p>© 2024</p>
<!-- UTF-8 is preferred for readability -->
<meta charset="UTF-8">
<p>© 2024</p>

However, entities are still useful for:

  • Reserved characters (<, >, &)
  • When you can’t type the character
  • Ensuring compatibility

Problem:

<p>Copyright &copy 2024</p> <!-- Missing semicolon -->

Solution:

<p>Copyright &copy; 2024</p> <!-- Correct -->

Problem:

<p>&amp;amp;</p> <!-- Shows &amp; instead of & -->

Solution:

<p>&amp;</p> <!-- Correct -->

Problem:

<p>&#72;&#101;&#108;&#108;&#111;</p> <!-- Unnecessary -->

Solution:

<p>Hello</p> <!-- Just type it -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>HTML Entities Example</title>
</head>
<body>
<header>
<h1>HTML Entities Demo</h1>
</header>
<main>
<section>
<h2>Reserved Characters</h2>
<p>Less than: &lt; | Greater than: &gt; | Ampersand: &amp;</p>
<p>Quote: &quot;Hello&quot; | Apostrophe: It&apos;s</p>
</section>
<section>
<h2>Symbols</h2>
<p>Copyright: &copy; 2024</p>
<p>Trademark: Brand&reg; and Product&trade;</p>
<p>Currency: &euro;100, &pound;50, &yen;1000</p>
</section>
<section>
<h2>Mathematical</h2>
<p>5 &times; 3 = 15</p>
<p>Area = &pi;r&sup2;</p>
<p>Temperature: 25&deg;C</p>
<p>x &le; 10 and y &ge; 5</p>
</section>
<section>
<h2>Spacing</h2>
<p>Non-breaking&nbsp;space&nbsp;example</p>
</section>
</main>
<footer>
<p>&copy; 2024 Example Site. All rights reserved.</p>
</footer>
</body>
</html>