Basics

HTML Entities

Using HTML Entities

HTML entities like & and < encode special characters for display.

What are HTML Entities?

HTML entities are a way to represent special characters in HTML. They are used to display characters that have a special meaning in HTML, such as <, >, and &. Using entities ensures these characters are displayed correctly by the browser.

Common HTML Entities

Some of the most commonly used HTML entities include:

  • &amp; for &
  • &lt; for <
  • &gt; for >
  • &quot; for "
  • &apos; for ' (not standard in HTML4, but available in XHTML and HTML5)

Using HTML Entities in Code

To use an HTML entity, you simply include it in your HTML code where you want the special character to appear. This is particularly useful for characters that would otherwise be interpreted as HTML tags or have special functions.

In the example above, the entities &lt; and &gt; are used to display the "less than" and "greater than" symbols, which are crucial for the correct interpretation of the statement in a webpage.

HTML Entities for Non-breaking Spaces

The non-breaking space entity &nbsp; is used to prevent automatic line breaks at its position. This can be useful for maintaining the formatting of text.

In the example, &nbsp; is used between words to ensure they stay together on the same line.

HTML Entity References

HTML entities can be referred to by their name or numeric code. Numeric codes are useful when there is no named entity available. For example, the euro sign (€) can be represented as &euro; or &#8364;.

Both representations will display the euro sign in the browser.

Previous
Quotations