HTML Comment Tag
The HTML Comment Tag is used to write notes or explanations inside the HTML code that browsers will ignore. These comments don’t show up on the web page — they are just visible in the HTML file when you view the source code. Notifications and remainders can be added by using comments.
Syntax -
<!-- Write your comments here -->
The comments have the following restrictions -
- The comment start delimiter "<!--".
- The comment end delimiter "-->".
Why Use Comments?
There are several practical reasons why comments are super useful:
- Explain your code to yourself or others.
- Hide parts of the code temporarily during testing.
- Mark TODOs or future changes.
- Add notes or documentation inside the HTML itself.
Example -
Scenario - Explaining Sections of Code
<!-- <p>This content is under construction</p> -->
Explaining Example -
If you're not ready to show something on your web page, you can “comment it out.” This way, the line still exists in the file but won’t display on the page. It’s like putting it in invisible mode.
Multiline Comments -
Multiline comments are self-explanatory. They are used to comment on multiple lines in an HTML document. Browsers do not display multiline comments.
Syntax -
<!--
.... some HTML here (multiline) ....
-->
Example -
Scenario - Example describes how to code multiline comments
<!DOCTYPE html>
<html>
<head>
<title>Multiline Comment example.. </title>
</head>
<body>
<!-- Below is the paragraph commented -->
<p>This is a paragraph1</p>
<!--
<pre>This is a paragraph1.
This is paragraph2.
This is paragraph3.</pre>
-->
</body>
</html>
Explaining Example -
This code is a demonstration of how to use HTML comments to temporarily hide a <pre> block. This code is an HTML comment, so the browser will completely ignore it and show nothing on the page.