H.dev

HTML Comments

HTML Comments

When writing code, it's often helpful to leave notes for yourself or other developers. In HTML, these notes are called comments.

Comments are sections of code that are completely ignored by the web browser. They will not be displayed on the screen when a user visits your website.

Syntax

To create a comment in HTML, you use a special syntax. A comment starts with <!-- and ends with -->.

<!-- This is a comment. The browser will ignore it. -->
<p>This paragraph will be displayed on the page.</p>

Why Use Comments?

There are several great reasons to use comments in your HTML:

1. Documenting Your Code

Comments can help you explain what a specific section of your HTML does. This is especially useful for complex layouts or when working in a team where others need to understand your logic.

<!-- The main navigation menu starts here -->
<nav>
  <ul>
    <li><a href="/">Home</a></li>
    <li><a href="/about">About</a></li>
  </ul>
</nav>
<!-- The main navigation menu ends here -->

2. Debugging and Temporarily Hiding Code

If you are trying to figure out why your webpage isn't looking right, you can use comments to temporarily disable certain parts of your HTML without completely deleting them.

<h1>Welcome to My Website</h1>
<!-- 
  Hiding the broken banner image for now
  <img src="broken-banner.png" alt="Banner"> 
-->
<p>Here is some content.</p>

As shown in the example above, comments can span multiple lines!

Quick Tip

Most modern code editors (like VS Code) have a handy shortcut for toggling comments. You can usually select the lines of code you want to comment out and press Ctrl + / (on Windows/Linux) or Cmd + / (on Mac) to instantly wrap them in comment tags.

💡 Himubey Tip

Never put sensitive information like API keys, passwords, or internal server logic inside HTML comments! Remember, anyone can right-click and view your page source. HTML comments are public.

Interview Questions

  1. How do you write a comment in HTML?
  2. Why is it a bad idea to leave sensitive information in HTML comments?
  3. Besides leaving notes for developers, what is another practical use case for HTML comments while coding?

Design & Developed by Himanshu Dubey © 2026