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 < 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 <p> tag for paragraphs</p>
<p>at the start and</p>at the end are a real paragraph tag.<is the entity for the<sign. The name comes from less than.>is the entity for the>sign, from greater than.- Every entity starts with
&and ends with;. Don't forget the semicolon.
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 <p> 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, ®ion can show as ®ion. The browser finds ® at its start, and ® is the entity for the ® symbol. To be safe, write an & in your text as &:
<p>Tom & Jerry</p>
<p>Formula: a < 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
<becomes<>becomes>&becomes&"becomes". Use it inside an attribute value, so the browser does not read the quote as the end of the value©becomes© becomes a special space: the words on its two sides always stay on the same line
You can also write an entity with a number: < gives the same result as <. 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
- A missing semicolon.
<without;sometimes works and sometimes doesn't. Always end it with;. - A wrong name.
&less;is not an entity, so the page shows the text&less;as it is. - Writing it twice. In
&lt;, the browser only changes&to&. So the page shows the text<, not the<sign. - Entities inside
<script>or<style>. These two tags hold JavaScript and CSS code. Inside them the browser does not change entities into characters, soif (a < b)in JavaScript is an error. to make space. Space between elements is a job for CSS (margin, padding), not a row of .
TL;DR: Write characters that have a special meaning in HTML as entities (short codes), so they show as text: < for <, > for >, & for &, " for " in attributes. An entity starts with & and ends with ;. Other symbols such as © have entities too (©), 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 <p> 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: < shows as the < sign, and > 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 <p> in your code counts as <p>.
⚡ The & sign too
Write:
<p>Tom & Jerry</p>
<p>Formula: a < b</p>
Meaning: & starts an entity, so the browser always looks for an entity name after it: ®ion can show as ®ion, because ® is the entity for ®. To be safe, write an & in your text as &.
⚡ The semicolon and the right name
Meaning: < 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">.