Pages

Monday, May 5, 2014

HTML Comments

Comments are piece of code which is ignored by any 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. Comments help you and others understand your 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. Comments are a great asset to new developers and a great way to place little notes to yourself reminding yourself what pieces of code are doing what. Comments are also great ways to troubleshoot bugs and code errors, as they give you the ability to comment out lines of code one at a time to search for the exact line causing problems. As a sprouting young web developer, HTML code comments are your friends! A comment is a way to control which lines of code are to be ignored by the web browser and which lines of code to incorporate into your web page. There are three main reasons why you may want your code to be commented out or ignored.
  • Comment out elements temporarily rather than removing them, especially if they've been left unfinished.
  • Write notes or reminders to yourself inside your actual HTML documents.
  • Create notes for other scripting languages like JavaScript which requires them.
 
 

Comment Tags:

<!-- Opening Comment Tag
-- > Closing Comment Tag

As you can see, comments are also comprised of an opening and closing tag, (<!-- -->). Like other HTML elements, these tags can span across multiple lines of code, allowing you to comment out large blocks of HTML code. Any HTML elements that are encapsulated inside of the comment tags will be ignored by the web browser. This makes comment tags quite useful for debugging, as they allow the developer to temporarily comment out pieces of an HTML document without having to immediately delete the code from the HTML document.

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 borwser. This is because there is a space between the left angle bracket and the exclamation mark.
< !--   This is commented out -->

Be careful if you use comments to "comment out" HTML that would otherwise be shown to the user, since some older browsers will still pay attention to angle brackets inside the comment and close the comment prematurely -- so that some of the text that was supposed to be inside the comment mistakenly appears as part of the document.

 

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 in Explorer on Windows, and are thus excellently suited to give special instructions meant only for Explorer on Windows. They are supported from Explorer 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 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, 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.
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>


 
html<!-commenting existing code->:

As a web designer, you may sometimes have different websites in progress at once, and it might be easy to leave some HTML elements unfinished. In this case, you may comment out an element until it is 100% ready for the site. Below, we have commented out an input form element, since we are not quite ready to receive input from our users.

HTML Code:

<!-- <input type="text" size="12" /> -- Input Field -->

Now when we are ready to show that element, we can simply remove the comment tags, and our browser will readily display the element!

HTML Code:

<input type="text" size="12" />

Input Field:

Comment out elements and bits of code that you may want to recall and use at a later date. Nothing is more frustrating than deleting bits of code only to turn around moments later and realize that you now need to recode them.

No comments:

Post a Comment