HTML Comments

Rumman Ansari   Software Engineer   2023-03-27   6190 Share
☰ Table of Contents

Table of Content:


Comment tags are used to insert comments in the HTML source code.

You can put comments between any tags in your HTML documents. Comments use the following syntax:

<!-- Write your comments here -->

Anything after will not be displayed. It can still be seen in the source code for the document, but it is not shown onscreen.

Notice that there is an exclamation point (!) in the opening tag, but not in the closing tag.


With comments you can place notifications and reminders in your HTML:

Example


<!DOCTYPE html>
<html>

   <head>
      <title>comments Example</title>
   </head>
	
   <body>
      <h2>Comments Example</h2>
	  <!-- This is a comment -->
      <p>The following word uses an <em>emphasized</em> typeface.</p>
	  
	  <!-- Remember to add more information here -->
	  <p>This is a paragraph.</p>
	  
   </body>
	
</html>

Live Preview

You can even comment out whole sections of code. For example, in the following snippet of code you would not see the content of the <h2< element. You can also see there are comments indicating the section of the document, who added it, and when it was added.

Example


<!DOCTYPE html>
<html>

   <head>
      <title>comments Example</title>
   </head>
	
   <body>
      <!-- Start of this Section added 04-24-04 by atnyla developer -->
   <!-- <h2> Character Entities </h2> -->
   <p> <strong> Character entities </strong> can be used to escape special
     characters that the browser might otherwise think have special meaning. </p>
    <!-- End of Footnotes section -->
   </body>
	
</html>

Live Preview

Comments are also great for debugging HTML, because you can comment out HTML lines of code, one at a time, to search for errors:

Conditional Comments

ou might stumble upon conditional comments in HTML:

Example


 
<!DOCTYPE html>
<html>

   <head>
      <title>comments Example</title>
   </head>
	
   <body>
      <!--[if IE 9]>
    .... some HTML here ....
      <![endif]-->
   </body>
	
</html>

Conditional comments defines some HTML tags to be executed by Internet Explorer only.