• google plus

How to Work with Comments in HTML

It's helpful when writing HTML to be able to put text into a document that is just for use by yourself or future editors of the document. Such text is known as a comment. In this how-to, you'll learn how to work with HTML comments.

Comments are generally used for one of three purposes.

  1. To write helpful notes about the code, for example, why something is written in a specific way.
  2. To comment out some code that is not currently needed, but may be used sometime in the future.
  3. To debug a page.

HTML comments are enclosed in <!-- and -->. For example:

<!-- This is an HTML comment -->

Follow these steps to practice using HTML comments:

  1. Start with a simple HTML document, like the following:
    
    <!DOCTYPE HTML>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title></title>
    </head>
    <body>
    
    </body>
    </html>
    
  2. Add a comment to the <body> element.
    
    <!DOCTYPE HTML>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title></title>
    </head>
    <body>
    	<!--this is the body of the document-->
    </body>
    </html>
    
  3. Add two <p> elements to the document, and then comment one of them out.
    
    <!DOCTYPE HTML>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title></title>
    </head>
    <body>
        <!--this is the body of the document-->
        <p>this paragraph is visible</p>
        <!--<p>this paragraph is commented out-->
    </body>
    </html>
    	
  4. Save the file and open it in a browser. It should look like the following.
    Comments demo
Author: Chris Minnick
Chris is a prolific author and trainer, and the CEO of WatzThis?. His published books include Writing Computer Code, JavaScript for Kids, Coding with JavaScript For Dummies, Beginning HTML5 and CSS3 For Dummies, Webkit For Dummies, CIW eCommerce Certification Bible, and New Riders' XHTML.
About Webucator

Webucator provides instructor-led training to students throughout the US and Canada. We have trained over 90,000 students from over 16,000 organizations on technologies such as Microsoft ASP.NET, Microsoft Office, XML, Windows, Java, Adobe, HTML5, JavaScript, Angular, and much more. Check out our complete course catalog.