Lesson 45 +10 XP

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 ;.

Code

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

The big three you MUST know

If you write &lt; or &gt; directly in text, the browser might think you're writing a tag! Use entities instead:

Code

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

More useful entities

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

Non-breaking space (&amp;nbsp;)

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

Code

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

Number entities

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

Code

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

TL;DR

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