HTML Comment Tag: A Step-by-Step Guide

The HTML Comment Tag is one of the simplest but most useful features in HTML. It allows developers to write notes inside their code that are not displayed in the browser.

If you are learning HTML, understanding comments is very important. They help you organize code, explain sections, and temporarily disable elements.

In this beginner-friendly guide, you will learn everything about the HTML comment tag step by step.

What is the HTML Comment Tag?

he HTML comment tag is used to insert notes or explanations inside HTML code.

These comments:

  • Are not visible on the webpage

  • Help developers understand the code

  • Improve readability

  • Make teamwork easier

In simple words, comments are messages for developers, not for users.

HTML Comment Tag Syntax

The syntax of the HTML comment tag is very simple:

<!-- This is a comment -->

Important Points:

  • It starts with <!--

  • It ends with -->

  • Everything inside is ignored by the browser

Basic Example of HTML Comment

Here is a simple example:

<!DOCTYPE html>
<html>
<head>
    <title>My Page</title>
</head>
<body>

    <!-- This is a heading -->
    <h1>Welcome to My Website</h1>

</body>
</html>

The text “This is a heading” will NOT appear on the webpage.

Step-by-Step Guide: How to Use HTML Comment Tag

Let’s understand how to use it properly.

Step 1: Open Your HTML File

Create or open your HTML file in a code editor like:

  • Notepad

  • VS Code

  • Sublime Text

Step 2: Write the Comment Syntax

Add the comment syntax:

<!-- Your comment here -->

Step 3: Add Description Inside

Example:

<!-- Navigation Bar Starts Here -->

Step 4: Save and Refresh

Save your file and refresh the browser.

You will NOT see the comment displayed on the page.

Multi-Line Comments in HTML

HTML does not have a separate multi-line comment syntax. But you can write long comments like this:

<!--
This is a multi-line comment.
You can write explanations
across multiple lines.
-->

Everything inside the comment block will remain hidden.

Commenting Out Code in HTML

Commenting Out Code in HTML

<!-- <p>This paragraph is hidden.</p> -->

The paragraph will not appear on the webpage.

This is very useful while testing or debugging.

Real-World Use Cases of HTML Comments

1. Explaining Code Sections

<!-- Header Section -->
<header>
<h1>My Website</h1>
</header>
 

2. Dividing Large HTML Files

<!-- ===================== -->
<!– Footer Section Starts –>
<!– ===================== –>
 

3. Temporary Testing

 
<!-- <img src=”banner.jpg” alt=”Banner”> -->
 

4. Developer Notes

 
<!--Remember to add contact form here -->
 

Comments vs Visible Text (Comparison Table)

FeatureHTML CommentVisible Text
Visible on webpage❌ No✅ Yes
Used for users❌ No✅ Yes
Used for developers✅ Yes❌ No
Affects layout❌ No✅ Yes
Helps debugging✅ Yes❌ No

This table clearly shows that HTML comments are only for developers.

Common Mistakes in HTML Comments

Beginners often make small mistakes. Let’s look at them.

 Missing Closing Tag

Wrong:

<!-- This is a comment
Correct:
<!-- This is a comment –>
 Using Nested Comments

Wrong:

<!-- Outer comment
<!-- Inner comment-->
-->
HTML does NOT support nested comments.

 Using Double Hyphens Inside

Avoid this:

<!-- This is wrong — example -->
Double hyphens inside comments can cause issues.

Best Practices

Follow these best practices:

 Keep Comments Short and Clear

Do not write very long explanations.

 Use Comments for Sections

Divide large files using comments.

 Avoid Over-Commenting

Do not comment every single line.

Remove Unnecessary Comments

Clean up before publishing.

 Do Not Put Sensitive Information

Comments are visible in page source (View Source option).

Anyone can see them by right-clicking → View Page Source.

Advanced Example: Structured HTML with Comments

<!DOCTYPE html>
<html>
<head>
    <title>Sample Page</title>
</head>
<body>

    <!-- Header Section -->
    <header>
        <h1>Website Title</h1>
    </header>

    <!-- Main Content -->
    <main>
        <p>This is the main content area.</p>
    </main>

    <!-- Footer Section -->
    <footer>
        <p>© 2026 My Website</p>
    </footer>

</body>
</html>

FAQs About HTML Comment Tag

 Are HTML comments visible in the browser?

No, they are not visible on the webpage. However, they can be seen in the page source.

Can we use comments inside tags?

No. You should not break HTML tags using comments.

Wrong:

 
<p <!– comment –> >Hello</p>
 

 Can HTML comments affect SEO?

Search engines usually ignore comments. They do not affect ranking.

 Can we use comments in CSS and JavaScript?

Yes, but they have different syntax.

CSS:

 
/* This is a comment */
JavaScript:
// This is a comment
 

Can we hide content using comments?

Yes, but it is not a secure method. Anyone can see it in the source code.

Related Topics

Read More

How to Create Database in MySQL

  • By admin
  • November 27, 2021
  • 65 views
How to Create Database in MySQL

How to create table in MySQL

  • By admin
  • November 6, 2021
  • 46 views
How to create table in MySQL

MySQL commands with examples

  • By admin
  • September 11, 2021
  • 138 views
MySQL commands with examples

MySQL use database

  • By admin
  • May 28, 2021
  • 45 views
MySQL use database

System Software

  • By admin
  • May 20, 2021
  • 66 views

Introduction to software

  • By admin
  • May 13, 2021
  • 56 views
Introduction to software