Wednesday, November 13, 2013

Comments in HTML

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:
<!-- 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 last line 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 in Explorer on Windows, and are thus excellently suited to give special instructions meant only for Explorer on Windows.

Conditional comments 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.

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

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

No comments:

Post a Comment