🌱BeginnerAdvanced Basics

HTML Entities

Display reserved and special characters using HTML entities like &, <, ©.

Overview

HTML entities are special codes used to represent characters that would otherwise be misinterpreted by the browser or cannot be typed easily. They start with & and end with ; a named entity like &amp; or a numeric code like &#169;. The most important entities are for reserved characters: - &lt; → < (less-than sign, which would start an HTML tag) - &gt; → > (greater-than sign) - &amp; → & (ampersand, which starts an entity itself) - &quot; → " (quotation mark, useful inside attributes) - &apos; → ' (apostrophe, HTML5) Entities are also used for characters like ©, ®, →, , non-breaking space (&nbsp;).

Key Points

  • •&lt; and &gt; are essential for displaying < and > in content without creating HTML tags
  • •&amp; is required to display a literal & character
  • •&nbsp; is a non-breaking space prevents line breaks between words
  • •Named entities (&copy;) are easier to read; numeric entities (&#169;) work in any encoding
  • •With UTF-8 charset you can type most symbols directly entities are mainly for reserved characters
  • •Entities are case-sensitive: &copy; works; &COPY; does not

Examples

Reserved character entities

<!-- Displaying HTML code in a web page -->
<p>To create a link, use <code>&lt;a href="..."&gt;</code></p>

<!-- Ampersand in text -->
<p>Tom &amp; Jerry is a classic cartoon.</p>

<!-- Quote inside an attribute value -->
<p title="He said &quot;Hello&quot;">Hover for quote</p>

Common named entities

<p>&copy; 2024 My Company</p>          <!-- © -->
<p>&reg; Registered Trademark</p>      <!-- ® -->
<p>&trade; My Product&trade;</p>       <!-- â„¢ -->
<p>Price: &euro;49 / &pound;42</p>    <!-- € £ -->
<p>10&nbsp;&times;&nbsp;5 = 50</p>    <!-- × non-breaking -->
<p>Temperature: 72&deg;F</p>          <!-- ° -->

Non-breaking space and dash entities

<!-- Non-breaking space: prevents line break between words -->
<p>Chapter&nbsp;1</p>
<p>$&nbsp;99.99</p>

<!-- Dashes -->
<p>Monday&ndash;Friday</p>   <!-- – en dash: date/range -->
<p>Wait&mdash;what?</p>      <!-- em dash: pause -->

Common Mistakes

  • Forgetting the semicolon at the end of an entity &amp without ; won't render
  • Using &nbsp; to add visual spacing use CSS margin/padding instead
  • Displaying user-provided content without escaping < and & creates XSS vulnerability

Note

The most important entities to memorize: &lt; &gt; &amp; &copy; &nbsp;. In practice, if your page uses UTF-8 (which it should), you can type most symbols directly entities are mainly needed for <, >, and &.

Exercises

Write HTML to solve each exercise. Use Preview to see it render in a live iframe, then mark as done.

Loading editor…

Completed: 0 / 5 exercises

Knowledge Check

Question 1 of 4

How do you display a literal < character in HTML content?