Lesson 45 +10 XP

HTML Entities

HTML Entities

Some characters are special in HTML, and some can't be typed easily. HTML entities solve both problems!

What is an entity?

An entity is a special code that stands for a character. It starts with & and ends with ;.

&lt;  stands for <
&gt;  stands for >
&amp; stands for &

The big three you MUST know

If you write < or > directly in text, the browser might think you're writing a tag! Use entities instead:

<p>2 is less than &lt; 3</p>
<p>5 is greater than &gt; 4</p>
<p>Tom &amp; Jerry</p>

More useful entities

  • &quot;: double quote "
  • &apos; (or &#39;): single quote '
  • &nbsp;: a non-breaking space (keeps words together!)
  • &copy;: copyright ©
  • &reg;: registered ®
  • &trade;: trademark
  • &euro;: euro sign
  • &pound;: pound sign £
  • &yen;: yen sign ¥

Non-breaking space (&nbsp;)

&nbsp; is a space that never lets the words split across lines. Use it to keep "10 km" or "Chapter 5" together.

<p>It's about 10&nbsp;km away.</p>

Number entities

You can also use numbers: &#65; is the character number 65, which is "A".

<p>&#65; is the letter A</p>

TL;DR

  • Entities start with & and end with ;.
  • Use &lt;, &gt;, &amp; for <, >, &.
  • &nbsp; is a space that keeps words together.
  • &copy;, &euro;, &reg; and friends type special symbols.
  • Number entities like &#65; use character numbers.