Introduction

HTML entities are used to display reserved characters in HTML, such as <, >, and &, which would otherwise be interpreted as HTML code. They are also used to display characters that are not present on a standard keyboard, such as ©, ®, and ™.

Key Concepts

  1. What are HTML Entities?

    • HTML entities are special codes that represent characters in HTML.
    • They start with an ampersand (&) and end with a semicolon (;).
  2. Common HTML Entities

    • &lt; for <
    • &gt; for >
    • &amp; for &
    • &quot; for "
    • &apos; for '
  3. Why Use HTML Entities?

    • To display reserved characters that have special meanings in HTML.
    • To display characters that are not available on the keyboard.
    • To ensure that the HTML code is correctly parsed by browsers.

Practical Examples

Example 1: Displaying Reserved Characters

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>HTML Entities Example</title>
</head>
<body>
    <p>To display a less than sign, use &lt;.</p>
    <p>To display a greater than sign, use &gt;.</p>
    <p>To display an ampersand, use &amp;.</p>
</body>
</html>

Explanation:

  • The &lt; entity is used to display the < character.
  • The &gt; entity is used to display the > character.
  • The &amp; entity is used to display the & character.

Example 2: Displaying Special Characters

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>HTML Entities Example</title>
</head>
<body>
    <p>Copyright symbol: &copy;</p>
    <p>Registered trademark symbol: &reg;</p>
    <p>Trademark symbol: &trade;</p>
</body>
</html>

Explanation:

  • The &copy; entity is used to display the © symbol.
  • The &reg; entity is used to display the ® symbol.
  • The &trade; entity is used to display the ™ symbol.

Practical Exercises

Exercise 1: Display Reserved Characters

Task: Create an HTML page that displays the following text:

To write HTML code, use &lt;html&gt; tags.

Solution:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Exercise 1 Solution</title>
</head>
<body>
    <p>To write HTML code, use &lt;html&gt; tags.</p>
</body>
</html>

Exercise 2: Display Special Characters

Task: Create an HTML page that displays the following text:

The copyright symbol is ©, the registered trademark symbol is ®, and the trademark symbol is ™.

Solution:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Exercise 2 Solution</title>
</head>
<body>
    <p>The copyright symbol is &copy;, the registered trademark symbol is &reg;, and the trademark symbol is &trade;.</p>
</body>
</html>

Common Mistakes and Tips

  • Mistake: Forgetting the semicolon (;) at the end of an entity.

    • Tip: Always ensure that your HTML entities are complete with a semicolon.
  • Mistake: Using HTML entities for characters that do not need them.

    • Tip: Only use HTML entities for reserved characters or special symbols that are not available on the keyboard.

Conclusion

In this section, you learned about HTML entities, their purpose, and how to use them to display reserved and special characters in HTML. Understanding HTML entities is crucial for writing clean and functional HTML code, especially when dealing with characters that have special meanings in HTML. In the next section, we will explore how to use iframes in HTML.

© Copyright 2024. All rights reserved