HTML comments allow developers to add notes inside the code that do not appear on the webpage. They help in explaining the code or temporarily disabling parts of the code.
A comment starts with <!--
and ends with -->
.
<!-- This is a comment -->
✔ Browsers ignore comments (they are not displayed on the page).
Purpose | Usage Example |
---|---|
Code Explanation | Describe sections of your code for clarity. |
Debugging | Temporarily remove code without deleting it. |
Collaboration | Help teams understand the code. |
Hiding Code | Hide specific elements for testing. |
<!-- This is the header section -->
<header>
<h1>Welcome to My Website</h1>
</header>
✔ Helps document code for better understanding.
<!-- <p>This paragraph is hidden.</p> -->
✔ This prevents the paragraph from being displayed without deleting it.
<!--
This section contains the
navigation menu of the website
-->
<nav>
<ul>
<li>Home</li>
<li>About</li>
<li>Contact</li>
</ul>
</nav>
✔ Use multi-line comments for long explanations.
✅ Keep comments short and relevant.
✅ Do not overuse comments; write clean code instead.
✅ Use multi-line comments for sections of the code.
✅ Avoid sensitive information in comments (e.g., passwords).
@asadmukhtar