HTML Comments

Comments in HTML are used to add notes or documentation within the source code. These comments are not rendered by browsers, allowing developers to leave messages for themselves or others without affecting the display of the web page.

Basic Syntax

The syntax for writing a comment in HTML is straightforward. It starts with <&lt;!--> and ends with <--&gt;>. Anything between these tags will be treated as a comment.

Example
Try yourself
        
            <!-- This is a single-line comment -->
<p>This is a paragraph.</p>
        
    

Multi-Line Comments

HTML comments can span multiple lines. This is useful for adding detailed documentation or temporarily disabling sections of code without deleting them.

Example
Try yourself
        
            <!--
This is a multi-line comment.
It can span multiple lines.
-->
<p>This is a paragraph.</p>
        
    

Commenting Out Code

Comments can be used to disable parts of the code for testing or debugging purposes. By commenting out code, you can prevent it from executing without removing it entirely.

Example
Try yourself
        
            <!--
<h1>This is a heading</h1>
<p>This paragraph is commented out and will not be displayed.</p>
-->
<p>This is another paragraph.</p>
        
    

Best Practices

Here are some best practices for using comments in HTML:


Important Notes

Here are some important notes about HTML comments:


Summary

HTML comments are a useful tool for documenting and explaining code, as well as for testing and debugging. By following best practices and using comments effectively, you can make your code more understandable and maintainable.