HTML <!-- --> Tag

The <!-- --> tag in HTML is used to insert comments in the source code. These comments are not displayed in the browser when the page is rendered. They are primarily used to provide explanations, notes, or reminders for developers who read the code. Comments help in understanding the purpose of code blocks, make debugging easier, and improve the overall maintainability of the HTML document.

HTML Comments: How to Use <!-- --> for Documentation

Syntax

<!-- This is a comment -->

Attributes

The HTML comment tag does not have any specific attributes. It solely consists of the opening <!-- and closing --> delimiters.

Example

<!DOCTYPE html>
<html>
<head>
  <title>HTML Comments Example</title>
</head>
<body>
  <!-- This is a heading -->
  <h1>Hello World</h1>

  <!-- This is a paragraph with some text -->
  <p>This is a paragraph of text.</p>

  <!-- Let's add a list -->
  <ul>
    <li>Item 1</li>
    <li>Item 2</li>
  </ul>
</body>
</html>

More Examples

  1. Multi-line Comments:
    HTML comments can span multiple lines. This is useful for providing detailed explanations.

    <!--
        This is a multi-line comment.
        It can contain several lines of text.
        Use it to describe complex parts of your HTML.
    -->
    <p>Some content here</p>
    
  2. Commenting out Code:
    You can use comments to temporarily disable parts of your HTML code without deleting it. This is helpful for debugging or testing different implementations.

    <!--
    <h1>This heading is temporarily disabled</h1>
    <p>This paragraph will not be displayed</p>
    -->
    <p>This paragraph is visible.</p>
    
  3. Conditional Comments (For IE, though not recommended now):
    While not standard HTML comments, you might encounter these older conditional comments used to target specific versions of Internet Explorer. Note that they are deprecated and should not be used in modern web development.

      <!--[if IE]>
        <p>This will only show in IE</p>
      <![endif]-->
      <p>This will show in other browsers</p>
    
  4. Adding TODOs and Reminders: Comments can be used to insert reminders or TODOs which helps track progress of the code and its maintainence.

     <!-- TODO: Replace this text with the actual content -->
     <p>Placeholder text here.</p>
      <!-- FIXME: Check responsive behavior of this element -->
     <div class="responsive-container"></div>
    

Browser Support

The HTML comment tag (<!-- -->) is supported by all major browsers, including:

  • Chrome
  • Firefox
  • Safari
  • Edge
  • Opera

Notes and Tips

  • Comments are not displayed in the browser's output, but they are visible in the HTML source code.
  • Avoid including sensitive information in your comments, as they can be viewed by anyone inspecting the page's source.
  • Use comments to explain complex logic, not to state the obvious.
  • Good comments should explain why the code is written a particular way, not just what it does.
  • Keep your comments concise and clear. Overly verbose comments can be as bad as no comments at all.
  • Use comments consistently throughout your project to maintain code consistency.
  • Ensure that you close comments properly with -->, an unclosed comment can break the page layout or functionality.
  • Use comments to document your code as you write it, rather than waiting until the end. This ensures that your documentation is accurate and up-to-date.
  • While conditional comments were used for Internet Explorer, modern approaches (feature detection, CSS hacks only where absolutely necessary) are recommended.