HTML Comments

Bookmark
Learn about how to add comments in HTML documents.

Comments are used to annotate source code. It will add information describing the code and comments will not be displayed in browser. It is possible to look at the source code of the HTML pages using the option provided in a web browser. These comments will be visible when a user does the 'view source'. Comments help the programmer to understand purpose of the code.

  • HTML comments starts with <!-- tag, followed by text description and ends with -->.
  • Single line or multiple lines can be commented by enclosing them between the comments tag.
  • Comments tag do not have any attributes.

HTML Comments Example

<!DOCTYPE html>
<html>
<head>
<title>HTML Comments Example</title>
</head>
<body>
    <p>Comments are not displayed in browser. <!-- this commented part will not be displayed. --></p>
</body>
</html>

Output

Comments are not displayed in browser.

Cannot Nest HTML Comments

HTML comments cannot be nested, that is a comment cannot be inserted inside another comment. Following is an example of invalid nested comment.

<p>Comments should not be nested. <!-- comment started <!-- another comment inserted --> parent comment --></p>

Exception Occured:

TypeErrorException
Messageerror_log(app.log): Failed to open stream: Permission denied
File/home/dh_czz6eb/tutorialwalk.com/common/error.php
Line36
Trace#0 (): log_error(2, 'error_log(app.log): Failed to open stream: Permission denied', '/home/dh_czz6eb/tutorialwalk.com/common/error.php', 36)
#1 /home/dh_czz6eb/tutorialwalk.com/common/error.php(36): error_log('27-05-2024 03:16:54 am (IST),/home/dh_czz6eb/tutorialwalk.com/common/common.php,193,0,ErrorException,Optional parameter $per_page declared before required parameter $href is implicitly treated as a required parameter,/html/comments.html,#0 /home/dh_czz6eb/tutorialwalk.com/common/error.php(115): log_error(8192, 'Optional parame...', '/home/dh_czz6eb...', 193)
#1 [internal function]: check_for_fatal()
#2 {main}
', 3, 'app.log')
#2 /home/dh_czz6eb/tutorialwalk.com/common/error.php(8): log_exception(ErrorException)
#3 /home/dh_czz6eb/tutorialwalk.com/common/error.php(115): log_error(8192, 'Optional parameter $per_page declared before required parameter $href is implicitly treated as a required parameter', '/home/dh_czz6eb/tutorialwalk.com/common/common.php', 193)
#4 (): check_for_fatal()
REQUEST_URI/html/comments.html