Learn HTML Entities

Write characters that have a special meaning in HTML as entities (short codes), so they show as text: &lt; for <, &gt; for >, &amp; for &, &quot; for " in…

How do you show the text <p> inside a paragraph, without the browser reading it as a real tag? The problem: some characters (letters or signs) have a special meaning in HTML. The browser reads < as the start of a tag, and & as the start of a special code.

When you want one of these characters to show as ordinary text, write it as an HTML entity. An entity is a short code that starts with & and ends with a semicolon ;, such as &lt; for the < sign.

Show the < and > signs as text

This paragraph shows the tag name <p> in the middle of its sentence:

<p>Use the &lt;p&gt; tag for paragraphs</p>

The browser shows one whole sentence, with the text <p> in the middle.

If you type it directly, <p>Use the <p> tag for paragraphs</p>, the browser reads the <p> in the middle as a new paragraph tag. The sentence is cut into two paragraphs, and the text <p> is not shown.

One note for the exercises: the checker looks at the text the browser shows, not at the code you type. So &lt;p&gt; in your code counts as <p>.

The & sign is written as an entity too

Because & starts an entity, the browser always looks for an entity name after it. For example, &region can show as ®ion. The browser finds &reg at its start, and &reg is the entity for the ® symbol. To be safe, write an & in your text as &amp;:

<p>Tom &amp; Jerry</p>
<p>Formula: a &lt; b</p>

The browser shows Tom & Jerry and Formula: a < b. Other characters, such as spaces and the % sign, have no special meaning. You type them as usual.

Entities you will use often

You can also write an entity with a number: &#60; gives the same result as &lt;. You can also type other symbols such as ©, € or → directly, as long as the page has the line <meta charset="UTF-8"> (it makes the browser read every symbol correctly). The only characters that really need entities are the ones with a special meaning in HTML: <, >, &, and " inside an attribute.

⚠️ Entities that show up raw, and forced spaces

TL;DR: Write characters that have a special meaning in HTML as entities (short codes), so they show as text: &lt; for <, &gt; for >, &amp; for &, &quot; for " in attributes. An entity starts with & and ends with ;. Other symbols such as © have entities too (&copy;), but on a UTF-8 page (one that has <meta charset="UTF-8">) you can also type them directly.

⚡ Show a tag as text

Write:

<p>Use the &lt;p&gt; tag for paragraphs</p>
Guess first: if you type the <p> in the middle directly, what happens?

The browser reads it as a new paragraph tag. The sentence is cut in two, and the text <p> is not shown.

Meaning: &lt; shows as the < sign, and &gt; as the > sign. Codes like these are called entities. Every entity starts with & and ends with a semicolon ;.

In the exercises, the checker looks at the text the browser shows, so &lt;p&gt; in your code counts as <p>.

⚡ The & sign too

Write:

<p>Tom &amp; Jerry</p>
<p>Formula: a &lt; b</p>

Meaning: & starts an entity, so the browser always looks for an entity name after it: &region can show as ®ion, because &reg is the entity for ®. To be safe, write an & in your text as &amp;.

⚡ The semicolon and the right name

Meaning: &lt without ; sometimes works and sometimes doesn't, so always end it with ;. &less; isn't an entity, so the page shows it as it is. You can type symbols like © directly, as long as the page has the line <meta charset="UTF-8">.

What you will learn