deCODE - Our Newsletter for Jan 2022 is available for Download. 🗞   🥳
  Signup/Sign In

How to write comments in HTML?

The comments can be added to the HTML codes. These comments are not displayed on the webpage but have an important role in HTML coding. The comments can be added to the code to explain some important lines within the program or add some text to explain each block. You can add comments for future reference.

The comment tag in the HTML codes has the following usefulness

  • It helps to understand the complex code better.
  • It makes dubbing easier.
  • It is useful for future reference.

HTML Comment Tag

We can comment on a single line as well as multiple lines in HTML. Here we will explain both the ways to comment. To comment a text add <!--comment text --> tag in HTML. The text between the comment tag is ignored by the browser. While commenting you should always remember that the comments do not support nested comments. Further, there are no spaces between the comment string.

Example: Comment a single line in HTML

Here, we have illustrated the valid way to add comments to a single line in HTML. If you add an invalid tag to comments. The comment will appear on the webpage.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>HTML</title>
</head>
<body>
    <!-- valid way to comment a single line -->
	<h2>commenting in HTML</h2>
	< ! --  oops! invalid comment -->
</body>
</html>

Output

Here is the output of the above program.

comments in HTML

Example: Commenting multiple lines in HTML

Here we have commented multiple lines in HTML

Conclusion

So, we can easily comment on the HTML codes. You can comment single or multiple lines in HTML. Using comments while coding is a good practice and helps others to easily understand your code.