HTML Comments

HTML

HTML comments are used to make document note, or remove a piece of code. Comments are not displayed. Following is an example of a comment.

<!-- this is a single line comment -->
<!-- this is a
multiline
comment -->

Comment HTML code

To comment out piece of HTML code from the web page enclose it inside the comment tag.

<p>This is a sample paragraph.</p>
<!--
<p>This is a paragraph.</p>
-->
<p>This is another paragraph.</p>

Output

This is a sample paragraph.

<!--

This is a paragraph.

-->

This is another paragraph.

Conditional comment

This works only for Internet Explorer on Windows and is used to set conditional instruction. Following is an example.

<!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
  <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->

If IE version is less than 9 then HTML inside the comment will be used else ignored.

Script comment

We can also comment out script using the comment tag. This helps older browser to render the page properly. If the browser does not support JavaScript then the script code is ignored because of the comment.

<p>Simple Line</p>
<!--
<script>
  document.write("Hello World!");
</script>
//-->
<p>Simple Line</p>

Style comment

We can also comment out style using the comment tag.

<html>
  <head>
    <style>
    <!--
        #container {
          color: #lightblue;
        }
    //-->
    </style>
  </head>
  <body>
  </body>
</html>

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT