HTML comments are not displayed in the browser, but they can help document your HTML source code.
HTML Comment Tag
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.
Add Comments
With comments you can place notifications and reminders in your HTML code:
Example:
<!-- This is a comment --> <p>This is a paragraph.</p> <!-- Remember to add more information here -->
Hide Content
Comments can be used to hide content. Which can be helpful if you hide content temporarily:
Example:
<p>This is a paragraph.</p> <!-- <p>This is another paragraph </p> --> <p>This is a paragraph too.</p>
You can also hide more than one line, everything between the <!--
and the -->
will be hidden from the display.
Example
Hide a section of HTML code
<p>This is a paragraph.</p> <!-- <p>Look at this cool image:</p> <img border="0" src="pic_trulli.jpg" alt="Trulli"> --> <p>This is a paragraph too.</p>
Comments are also great for debugging HTML, because you can comment out HTML lines of code, one at a time, to search for errors.
Hide Inline Content
Comments can be used to hide parts in the middle of the HTML code.
Example
Hide a part of a paragraph:
<p>This <!-- great text --> is a paragraph.</p>
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
Here, the given comment is a valid comment and will be wiped off by the browser.
<!DOCTYPE html> <html> <head> <title>Valid Comment Example</title> </head> <body> <!-- This is valid comment --> <p>Document content goes here.....</p> </body> </html>
Output
Document content goes here.....
Multiline Comments
So far we have seen single line comments, but HTML supports multi-line comments as well. You can comment multiple lines by the special beginning tag <!– and ending tag –> placed before the first line and end of the last line as shown in the given example below.
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
Document content goes here.....
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 conditional instructions to different versions of IE.
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>
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 then it is recommended to put that style sheet code inside proper HTML comments so that old browsers can work properly.
Example
<!DOCTYPE html> <html> <head> <title>Commenting Style Sheets</title> <style> <!-- .example { border:1px solid #4a7d49; } //--> </style> </head> <body> <div class = "example">Hello , World!</div> </body> </html>
Output
Hello , World!