Friday, 10 August 2012

HTML Comments


Comments are piece of code which is ignored by an web browser. It is good practice to comment your code, especially in complex documents, to indicate sections of a document, and any other notes to anyone looking at the code.
HTML Comment lines are indicated by the special beginning tag <!—and ending tag --> placed  at the beginning and end of EVERY line to be treated as a comment.
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  -->


Multiline Comments:
You have seen how to comment a single line in HTML. You can comment multiple lines by the special beginning tag <!--  and ending tag --> placed before the first line and end of the lastline to be treated as a comment.

For example:

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

Conditional Comments:
Conditional comments only work as follows:
<!—[if IE 6]>
Special instructions for IE 6 here
<![endif]-->

·         Their basic structure is the same as an HTML comment (<!-- -->). Therefore all other browsers will see them as normal comments and will ignore them entirely.
·         Explorer Windows, though, has been programmed to recognize the special <!—[if IE]> syntax, resolves the if and parses the content of the conditional comment as if it were normal page content.
·         Since conditional comments use the HTML comment structure, they can only be included in HTML 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 in your HTML code then it is recommended to put that script code inside proper HTML Comments to make old browser works properly.

For example:
<script>
<!—
Document.write(“Hello World!”)
//-->
</script>

Similarly if you are using Casecading 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.

For example:
<style>
<!—
Img{
Border:0px;
}
</style>



No comments:

Post a Comment