Join us Now |  Forgot Password? | Forgot UserName?


Learn step by step

HTML Comments

Comments are piece of code which is ignored by any web browser. It is always a good practice to have proper comment on your code, especially in complex documents, to indicate special sections of a document, and any other notes. Comments helps us and others to understand the code.
HTML Comment lines are indicated by the beginning tag <!-- and ending tag --> within which comment text lies.
Comments do not nest, and the double-dash sequence "--" may not appear inside a comment except as part of the closing --> tag. You must also make sure that there are no spaces in the start-of-comment string.
For example: Given line is a valid comment in HTML

<!-- This is commented out -->

But following line is not a valid comment and will be displayed by the browser. This is because there is a space between the left angle bracket and the exclamation mark.

< !-- This is commented out -->

We should be careful while using comments to "comment out" HTML that can otherwise be shown to the user. Since some older browsers still pay attention to angle brackets inside the comment and can close the comment prematurely dues to which some of the text that was supposed to be inside the comment can appear as part of the document.

Multiline Comments:

In previous example we had seen how to comment a single line in HTML, now lets see how to comment multiple lines by the special beginning tag <!-- and ending tag --> placed before the first line and end of the last line to be treated as a comment.

<!-- This is a multiline comment <br /> and can span through as many as lines you like. -->
Conditional Comments :

Conditional comments only work in internet explorer, and are thus excellently suited to give special instructions meant only for specific IE versions. They are supported from IE 5 onwards, and it is even possible to distinguish between 5.0, 5.5 and 6.0.
Conditional comments work as follows:

<!--[if IE 6]> Special instructions for IE 6 here <![endif]-->
  • Their basic structure is same as an HTML comment (<!-- -->). Therefore all other browsers will see them as normal comments and will ignore them.
  • IE though, has been programmed to recognize the special <!--[if IE]> syntax, it resolves the if and parses the content of the conditional comment as if it were normal page content.
  • Since conditional comments follow HTML comment structure, they can only be used in HTML files, and not in CSS files.
Using Comment tag

There are few browsers who supports <comment> tag to comment a part of code.

<p>This is <comment>not</comment> Internet Explorer.</p>
Commenting Scripts and Style Sheets:

If you are using Java Script or VB Script in your HTML code then it is recommended to put that script code inside proper HTML Comments to make old browser works properly.

<script> <!-- document.write("Hello World!") //--> </script>

Similarly if you are using Cascading Style Sheet in your HTML code then it is recommended to put that style sheet code inside proper HTML Comments to make old browser works properly.

<style> <!-- img{ border:0px; } //--> </style>

Related Videos