The Wayback Machine - https://web.archive.org/web/20250404164236/https://www.geeksforgeeks.org/html-comments/
Open In App

HTML Comments

Last Updated : 04 Feb, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

HTML comments are used to add notes or explanations in the HTML code that are not displayed by the browser.

  • They are useful for documenting the code, making it easier to understand and maintain.
  • To add a comment, use the syntax <!– your comment here –>.
HTML
<!-- This is a comment and will not be displayed on the webpage -->
<p>This is visible text.</p>

In this example:

  • The text within the <!--  and --> tags will not appear on the webpage.
  • These comments can include reminders, warnings, or explanations about the code, which can be useful for anyone reading or editing the HTML document.

Different Ways to Add Comments in HTML

There are two main ways to write comments in HTML: single-line and multi-line comments. Both use the same basic syntax but differ in how they are implemented

Comment

Descriptions

Syntax

Single-line

The single-line comment is given inside the ( <!--  comment --> ) tag. 

<!--  comment -->

Multi-line

It follows the syntax of a single-line comment by adding multiple lines in the comment.

<!-- Multi
Line 
Comment -->

Note: The shortcut for adding the comment through the keyboard is by typing Ctrl + / in Windows, and Command + / for Mac users.

More Examples of HTML Comments

Single-Line and Inline Comments

HTML
<html>
<body>
    <!--This is heading Tag-->
    <h1>GeeksforGeeks</h1>
    <!--This is single line comment-->
    <h2>This is <!--given for--> single line comment</h2>
</body>
</html>

In this example:

  • The code uses HTML comments (<!– comment –>) to add notes for developers, which are not displayed on the webpage.
  • The <h1> and <h2> tags display headings, while inline comments in <h2> do not affect the visible text.

Multi-Line Comments and Hidden Code Sections

HTML
<html>
<body>
    <!-- This is
         heading tag -->
    <h1>GeeksforGeeks</h1>
    <!-- This is
    multi-line
         comment -->
    <h2>This is multi-line comment</h2>
    <!-- <button style="font-family: Sans-serif;">
           Click Me
         </button> -->

</body>
</html>

In this example:

  • The code includes HTML comments (<!– comment –>) that are not displayed by browsers but serve as notes for developers.
  • The <h1> and <h2> tags define headings, with comments explaining their purpose; the commented-out <button> element is not rendered on the page.

Uses of HTML Comments

  • Code Organization: Comments can help break up sections of code, making it easier to navigate, especially in larger projects.
  • Collaboration: When working with others, comments are crucial for explaining why certain HTML elements are used, or for leaving instructions for team members.
  • Debugging: Temporarily disabling parts of HTML code by commenting them out is a common use, allowing developers to isolate issues.
  • Documentation: Providing details or documentation within the code for future reference without the need for external documentation.

Best Practices for Writing Useful and Clear Comments

  • Be Concise and Relevant: Write short, meaningful comments that explain the why behind the code, not the obvious what.
  • Avoid Overcommenting: Don’t comment on everything. Focus on complex or non-intuitive sections of the code.
  • Keep Comments Updated: Regularly revise comments to ensure they stay accurate as the code evolves.
  • Use Consistent Style: Maintain a uniform tone and format for comments across the project to ensure clarity.
  • Avoid Sensitive Information: Never include passwords, keys, or sensitive data in comments

HTML Comments – FAQs

How to write comments in HTML?

In HTML, comments are written using <!-- ... -->. They allow you to add notes or explanations within the code that are not displayed in the browser.

What are conditional comments in HTML?

Conditional comments in HTML are used to apply specific HTML or CSS for Internet Explorer versions 10 and below, allowing developers to address browser compatibility issues.

What are the three types of comments in HTML?

  • Single-line comment.
  • Inline comment.
  • Multi-lines comment.

Why are HTML comments important?

HTML comments are crucial for code documentation, temporarily disabling code, providing instructions, ensuring cross-browser compatibility, and improving readability, aiding in collaboration, debugging, and maintaining clean, understandable code.

Can HTML comments be nested?

No, HTML comments cannot be nested. If you try to nest comments like <!– <!– Nested Comment –> –>, it can lead to unintended behavior.

How do you comment out a large block of HTML code?

To comment out a large block of code, wrap the entire section with <!– and –>. This is useful for temporarily disabling sections of code.



Next Article

Similar Reads

three90RightbarBannerImg