How to display HTML code on a web page?

Web pages are made up of HTML code, so if you post or write some HTML code in a web pages, a browser would generally consider them normal code. Instead of showing them as they are literally, they have become part of the underlying HTML structure of that page.

Now how to address this problem and display HTML code and HTML tags correctly in a web page as the page content?

The answer is, use HTML entities.

HTML entities are a way to encode special HTML characters such as < and > into literal characters that cannot be interpreted by browsers as HTML code. Refer to the most common HTML entities conversion table below:

    ‘&’ (ampersand) becomes ‘&amp;‘
    ‘"’ (double quote) becomes ‘&quot;‘
    ”’ (single quote) becomes ‘&#039;‘
    ‘<’ (less than) becomes ‘&lt;‘
    ‘>’ (greater than) becomes ‘&gt;‘

Therefore, to display HTML code as they are and show them to your web site visitors, write the special characters such as < and > in HTML entities. For example,

<h2>Beautiful town</h2>

Has to be written:

&lt;h2&gt;Beautiful town&lt;/h2&gt;