HTML Comments

This item is used to add a comment to an HTML document. An HTML comment begins with <!–– and the comment closes with ––>.

HTML comments can be viewed by anyone looking at the source code of a page but are not rendered when the HTML document is rendered by a browser. This element serves as a commentary on an HTML document.

HTML Comment Tags 

You can add comments to your HTML source by using the following syntax:

<!-- Write your comments here -->

Note: Comments are not displayed by the browser, but they can help document your HTML source code.

Example:

<!DOCTYPE html>
<html>
<body> <!-- This is a comment -->
<p>This is a paragraph.</p>
<!-- Comments are not displayed in the browser --> </body> </html>

Output:

 

 

 

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 ensure that the start of comment string does not contain any spaces.

Example:

<!DOCTYPE html>
<html>
<head>
<title>Valid Comment Example</title>
</head>
<body> <!-- This is valid comment -->
<p>Document content goes here.....</p> </body>
</html>

 

Output:

 

 

 

 

 

But, following line is not a valid comment and will be displayed by the browser. This is because there's a space between the left angle bracket and the exclamation mark.

 

Example:

<!DOCTYPE html>
<html>
<head>
<title>Invalid Comment Example</title>
</head>
<body> < !-- This is not a valid comment -->
<p>Document content goes here.....</p>

</body>
</html>

Output:

 

 

Multiline Comments

So far we have seen single line comments, but HTML supports multi-line comments as well.

Example:

<!DOCTYPE html>
<html>
<head>
<title>Multiline Comments</title>
</head>
<body> <!--
This is a multiline comment and it can
span through as many as lines you like.
-->
<p>Document content goes here.....</p> </body>
</html>

Output:

 

 

Conditional Comments

Conditional comments only work in Internet Explorer (IE) on Windows but they are ignored by other browsers. They are supported from Explorer 5 onwards, and you can use them to give different versions of IE conditional instructions.

Example:

<!DOCTYPE html>
<html>
<head>
<title>Conditional Comments</title>
<!--[if IE 6]>
Special instructions for IE 6 here
<![endif]-->
</head> <body>
<p>Document content goes here.....</p> </body>
</html>

Output:

 

 

Using Comment Tag

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

Example:

<!DOCTYPE html>
<html>
<head>
<title>Using Comment Tag</title>
</head>
<body> <p>This is <comment>not</comment> Internet Explorer.</p> </body>
</html>

 

Output: