Html Comment

html comments

A comment is a slice of code which is ignored by any browser. It's a good to add comments to our HTML code, specially in complex documents, to indicate sections of the document, and any another notes for anyone viewing the source code.

Comments help you and other people understand your code and increase the readability of the code. The comments used for an HTML file are called HTML comments.


how to add comment in html

HTML comments are placed between <!-- ... --> tags. Therefore, <!--...--> The tag will be treated as a comment and will be completely ignored by the browser.

Comments of any code make the code easy to understand and increase the readability of the code.

Syntax

<!-- Write your comments here -->

Note: The html commented code will not be display to a web page, and hence user can use comment tag for debugging purpose, and documentation purpose.

Example


Variety of comments

there are mainly 5 Variety of Comments in Html Documents

  1. Multiline Comments
  2. Conditional Comments
  3. Using Comment Tag
  4. Commenting Script Code
  5. Commenting Style Sheets

Multiline Comments

In HTML code, we can also comments multiple lines at a time. In multiline comment we can use any details about code or multiple line code for debug etc.

HTML supports multi-line comments as well. we can comment multiple lines through special opening tag <!--and ending tag--> located before the first line and end of the last line as represent in the given example below.

Example


Conditional Comments

Conditional comments only work in Internet Explorer (IE) on Windows but they are ignored by other browsers..

Example for Conditional Comments


Using Comment Tag

There are few browsers that support <comment> tag to comment a part of HTML code.

Note − The <comment> tag deprecated in HTML5. Do not use this element.

Example

If you are using IE, then it will produce following result −

This is Internet Explorer.

But, if you are not using IE, then it will produce following result −

This is not Internet Explorer.


Commenting Script Code

here you must note that if you are using VB Script or Java Script in your HTML code then it is recommended to place that script code inside proper HTML comments so that older browsers can works properly.

Example

This will produce the following result −Hello World!

Hello , World!


Commenting Style Sheets

Though you will learn using style sheets with HTML in a separate tutorial, but here you must make a note that if you are using Cascading Style Sheet (CSS) in your HTML code.

Example

That gives the following result :

Hey there are W3C.


Valid vs Invalid Comments

Comments do not nest which means a comment cannot be put inside another comment. Second 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.

Example

But, in the next example, comment is not a valid comment and will be displayed by the web browser. this is because there is a space between the left angle bracket and the exclamation mark.

Example

It will give the following result −

< !-- This is not a valid comment --/>Document contents.....