HTML Comments


 
Comments in HTML

 In HTML, Comments are used to add notes, explanations, or reminders within the code. The comments are ignored by web browsers and do not appear on the web page. Comments are helpful for documenting your code, making it easier to understand for yourself and others.

Syntax

To write a comment in HTML, we enclose our HTML code within '<!--' and '-->'.

Example :


<!- This is a comment in HTML -->

<p>This is visible content.</p>

<!- Comments are not displayed in the browser -->

Output : 


This is visible content.

In the above example the commented line 1 and line 3 are not visible on the web page. These are ignored by web browser.

Uses of Comments

* Explain code :  It is used to describe the purpose of specific parts of the HTML.

* Leave reminders: It is used to add TODOs or notes for future updates.

* Temporarily disable code:  It is used to comment out code you don’t want to run or display.

* Improve collaboration: It is used to help others to understand your code when working in teams.

Best Practices

* Write comments clear and concise.

* Do not overuse comments, we only add them where necessary.

* Avoid putting sensitive information in comments, as the source code can be viewed by anyone.

Example:


<!- Main navigation menu -->

<nav>

  <ul>

    <li><a href="#">Home</a></li>

    <li><a href="#">About</a></li>

  </ul>

</nav>


Note : 

Comments are for developers and do not affect how the web page looks or works.

Comments