What Is HTML Comment: Simple Explanation and Usage
HTML comment is a piece of text in your HTML code that the browser ignores and does not display on the page. It is written between <!-- and --> tags and is used to leave notes or explanations inside the code.How It Works
Think of an HTML comment like a sticky note you leave inside a book. It helps you or others understand the content without changing what the reader sees. When the browser reads your HTML file, it skips anything inside the comment tags <!-- and -->, so it won't show up on the webpage.
This is useful because you can write reminders, explanations, or temporarily hide parts of your code without deleting them. It keeps your code clean and easier to understand, especially when working with others or coming back to your code later.
Example
This example shows a comment inside an HTML file. The comment will not appear on the webpage.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>HTML Comment Example</title> </head> <body> <h1>Welcome to My Website</h1> <!-- This is a comment and will not be shown in the browser --> <p>This paragraph is visible on the page.</p> </body> </html>
When to Use
Use HTML comments when you want to leave notes for yourself or other developers without affecting the page display. For example, you can explain why a section of code is there or remind yourself to fix something later.
Comments are also helpful when testing changes. You can temporarily disable parts of your code by turning them into comments instead of deleting them. This way, you can easily bring them back if needed.
Key Points
- HTML comments start with
<!--and end with-->. - They are ignored by browsers and do not show on the webpage.
- Use comments to explain code, leave reminders, or temporarily hide code.
- Comments help keep your code organized and easier to understand.