HTML Entities | Special Characters in HTML

HTML Entities are used to display special characters in HTML that have reserved meanings or cannot be directly used in the code. For example, symbols like `<`, `>`, `&`, and others are part of the HTML syntax and need to be represented as entities to display them on a webpage.

Syntax of HTML Entities:

HTML entities begin with an ampersand (`&`) and end with a semicolon (`;`).

Example of Common HTML Entities

<!DOCTYPE html>
<html>
    <head>
        <title>HTML Entities Example</title>
    </head>
    <body>
        <p>Display the less than symbol: &lt;</p>
        <p>Display the greater than symbol: &gt;</p>
        <p>Display an ampersand: &amp;</p>
        <p>Display a copyright symbol: &copy;</p>
        <p>Display a registered trademark: &reg;</p>
    </body>
</html>

Output

Display the less than symbol: <

Display the greater than symbol: >

Display an ampersand: &

Display a copyright symbol: ©

Display a registered trademark: ®

Commonly Used HTML Entities:

Example of Using HTML Entities in Text

<!DOCTYPE html>
<html>
    <head>
        <title>HTML Entities in Text</title>
    </head>
    <body>
        <p>This & that</p>
        <p>She said, &quot;Hello!&quot;</p>
        <p>A &nbsp; space is a non-breaking space.</p>
    </body>
</html>

Output

This & that

She said, "Hello!"

A space is a non-breaking space.

When to Use HTML Entities:

HTML entities are crucial for correctly rendering special characters and symbols in web pages. They ensure that your HTML code is interpreted accurately by browsers while providing the intended visual output.