HTML Comments
- Comment is a piece of code which is ignored by any web browser.
- It is a good practice to add comments into your HTML code, especially in complex documents, to indicate sections of a document, and any other notes to anyone looking at the code.
- Comments help you and others understand your code and increases code readability.
- HTML comments are placed in between <!-- ... --> tags. So any content placed with-in <!-- ... --> tags will be treated as comment and will be completely ignored by the browser.
Example of comment:
<!DOCTYPE html>
<html>
<head>
<title>This is document title</title>
</head>
<body>
<!-- I am single line comment -->
<!--
This is a multiline comment and it can
span through as many as lines you like.
-->
<p>Document content goes here.....</p>
</body>
</html>
This will produce following result without displaying the content given as a part of comments:
Output:
Document content goes here.....
Multi-line comment:
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.
<!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.....
</body>
</html>
Output:
Document content goes here.....
Important Notes:
- Comments do not nest which means a comment cannot be put inside another comment.
- 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.