HTML Comments | Adding Notes and Explanations in Code

HTML Comments are used to add notes or explanations within the HTML code, which are not visible to users on the webpage. They help in improving code readability and serve as reminders or documentation for developers.

Syntax of HTML Comments:

An HTML comment starts with ``. Any text placed between these symbols is treated as a comment.

Basic Example of HTML Comments

<!DOCTYPE html>
<html>
    <head>
        <title>HTML Comments Example</title>
    </head>
    <body>
        <!-- This is a single-line comment -->
        <p>This paragraph is visible.</p>

        <!-- 
        This is a multi-line comment.
        It can span multiple lines.
        -->
        <div>This div is visible.</div>
    </body>
</html>

Uses of HTML Comments:

Example of Using Comments for Debugging

<!DOCTYPE html>
<html>
    <head>
        <title>Debugging with Comments</title>
    </head>
    <body>
        <p>This is visible content.</p>

        <!-- Commenting out the next section for debugging -->
        <!--
        <div>This section is currently disabled.</div>
        -->
    </body>
</html>

Output

This is visible content.

Best Practices for Using HTML Comments:

HTML comments are a simple yet powerful tool to document and debug your code effectively. Make them a habit while writing HTML to maintain clear and maintainable code.