HTML comment tag is an asset for the programmers as it is the great way to comprehend the code and trouble shoots the coding errors. It is used to pop in the comments within the code. Browser will ignore all the text inside the tag. This tag is invisible for the users.
The HTML Comment tag is consist of an opening and closing tag(<!– –>). These tags can extent across multiple lines of code, which allows you to comment large blocks of HTML code.
Let play with the example here : Copy the following code in to the file and name it example.html and open it using the web browser and see what you get.
You can see the comment in the result has not been displayed, commenting your code is the good practice and is always recommended.
There must not be any space at the start of the comment string as
1 |
<!-- This is not a valid comment --> |
Multiline Comments:
We have demonstrated how to comment a single line in HTML, now lets have a look what it takes to extent the comment in to multiple line. If your comment extent to multiple lines you can include opening tag() at the end of the last line as demonstrated below.
1 2 3 |
<!--This is a multiline comment and can span through as many lines you like. --> |
Conditional Comments:
This type of comments works only for Internet Explorer on windows and hence can be use to give special instruction to explorer on windows.
Here is the example snippet :
1 2 3 4 5 |
<!--[if IE 6]> Instructions ment only for IE 6 and not for other browsers <![endif]--> |
All other browsers will take it as normal as the comment starts and ends normally with opening and closing comment tag and will ignore them entirely and only internet explorer 6 will process the instruction given.
You can follow HTML Tags to know more about other available HTML tags that follow HTML 4.01 specifications or to grasp the knowledge about HTML5 Tags your can follow our HTML5 Tags post.