Adding comments to HTML

As you create more pages, it may become difficult for you to understand your code in the future unless you use comments. By adding comments to your code, it could be a valuable reference guide for you when you look at your code in the future. Ideally, comments should be added to indicate what the code does. Comments are personal; so add anything that you think is impotent about your code. Remember, as you add comments to your pages, the file size of the web page will increase. That, in turn, may increase web page download time. So avoid use of excessive comments.

As you first start working with HTML, you may start by adding comments to every single line to remind you of what the code does. As you develop more experience and gather more knowledge about HTML, you will learn to comment only major parts of your web page. You may, for example, comment the width of nested tables you are using in your web page:

Examples of comments in HTML
Examples of comments in HTML

In HTML, a comment begins with <!-- and ends with -->. Any text you place after <!-- is comment. Browsers ignore comment text. Again, comment is for your reference; it does not get displayed on the web page. In a comment, you can freely include special characters, such as ampersands, quotation marks, and angle brackets. Your comments can also span multiple lines. The browser will stop ignoring text once it reads -->.

Example of a single line comment:

<!--This is a small comment-->

Example of a multi-line comment:

<!--This comment is long. It is displayed on more than one line. Adding multi-line comments in HTML is easy as adding a single line comment. Whether the comment is single line or multi-line, it starts with <-- and ends with -->-->.

Important points about comments:

  • Use comments as a reference guide. Avoid use of excessive comments.
  • Add comments to major parts of your code; or for parts, that you are unsure of.
  • Use of comments is a great way to communicate with other people working on the same web page.
  • Start your comment with <-- and end with -->
  • Browsers and search engines ignore comments.