Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What are Entities in HTML?

If you have ever tried learning HTML, you might have encountered some special characters like  , <, and &. These special characters are known as entities in HTML. In this blog post, we'll discuss what entities are, why they are essential, and how to use them in your HTML code. We'll also provide some real-world examples to help you better understand the concept.

What are entities?

Entities are a way of representing special characters or symbols that cannot be directly used in HTML. They're like secret codes that the browser can understand and display as the intended character. Think of entities as a secret language that only browsers can decipher.

Entities usually start with an ampersand (&) and end with a semicolon (;). There are two types of entities: named entities and numeric entities. Named entities use a predefined name, while numeric entities use a numeric code.

Here's an example of a named entity:

<

This entity represents the less-than symbol (<). If you were to type this entity into your HTML code, the browser would display a less-than symbol instead of interpreting it as the start of an HTML tag.

And here's an example of a numeric entity:

&#60;

This numeric entity also represents the less-than symbol. The number 60 corresponds to the Unicode value of the less-than symbol.

Why are entities important?

Entities are essential for several reasons:

To avoid ambiguity: Some characters have special meanings in HTML, such as angle brackets (< and >), which are used to define HTML tags. If you want to display these characters in your HTML document, you need to use their corresponding entities to avoid confusing the browser.

To display non-ASCII characters: HTML documents are often limited to ASCII characters (the first 128 Unicode characters). If you want to display characters outside this range, such as accented letters or symbols from other languages, you need to use entities.

To display reserved characters: Some characters are reserved in HTML and cannot be used directly. For example, the ampersand (&) is used to start an entity. If you want to display an ampersand in your HTML document, you need to use its corresponding entity (&amp;).

How to use entities

Using entities in your HTML code is pretty simple. All you need to do is replace the special character you want to display with its corresponding entity. Let's look at some examples:

Example 1: Displaying special characters

Let's say you want to display the following text in your HTML document:

5 < 10

If you type this directly into your HTML code, the browser will interpret the less-than symbol as the start of an HTML tag and display an error. To avoid this, you can use the named entity for the less-than symbol:

5 &lt; 10

Or the numeric entity:

5 &#60; 10

Either of these will display the intended text correctly in the browser:

5 < 10

Example 2: Displaying non-ASCII characters

Let's say you want to display the following French word in your HTML document:

café

The letter é is not an ASCII character, so you'll need to use its corresponding entity. You can use the named entity:

caf&eacute;

Or the numeric entity:

caf&#233;

Both of these will display the word correctly in the browser:

café

Example 3: Displaying reserved characters

Suppose you want to display the following text in your HTML document:

Tom & Jerry

The ampersand is a reserved character, so you'll need to use its corresponding entity:

Tom &amp; Jerry

This will display the intended text correctly in the browser:

Tom & Jerry

Some common entities

Here are some common entities that you might find useful in your HTML code:

| Character | Named Entity | Numeric Entity | Description                 | |-----------|--------------|----------------|-----------------------------| |       | &nbsp;     | &#160;       | Non-breaking space          | | <       | &lt;       | &#60;        | Less-than symbol            | | >       | &gt;       | &#62;        | Greater-than symbol         | | &       | &amp;      | &#38;        | Ampersand                   | | "       | &quot;     | &#34;        | Double quotation mark       | | '       | &apos;     | &#39;        | Single quotation mark (apostrophe) | | ©       | &copy;     | &#169;       | Copyright symbol            | | ®       | &reg;      | &#174;       | Registered trademark symbol | |       | &euro;     | &#8364;      | Euro sign                   |

To find more entities, you can refer to online resources like the HTML Entity List on W3Schools.

Conclusion

Entities are an essential part of HTML that allows you to display special characters in your documents. By understanding how to use entities, you can avoid ambiguity, display non-ASCII characters, and use reserved characters in your HTML code. Next time you come across an entity, you'll know it's just a secret code for the browser to display the correct character. Happy coding!