HTML Comments


Comments are one of the simplest but most useful tools in HTML. They allow you to add notes inside your code without affecting how the webpage looks or works. Comments are meant only for developers. Browsers completely ignore them, so visitors won’t see them on the page. They help you explain sections of your code, hide temporary content, organize large files and make your work easier to maintain.

As your projects grow, you’ll often revisit old files months later. Without comments, it becomes harder to remember why you wrote certain lines or what purpose a section serves. Comments solve that problem by acting like reminders in your code. They are also helpful in team environments where multiple developers work on the same project and need clarity about each part of the layout.

In this chapter, you’ll learn how to write comments, where to use them, how to hide content temporarily, how to comment multiple lines and how comments help in real-world development.

What Is an HTML Comment?

An HTML comment starts with <!-- and ends with -->. Everything between these two markers is ignored by the browser.

Basic syntax:

<!-- This is a comment -->

You can place comments anywhere in your HTML file—inside the head section, before or after elements, between lines or even within body content.

Example:

<p>This paragraph will appear on the page.</p>
<!-- This note is only for the developer -->

The visitor will see only the paragraph, not the comment.

Why Use Comments in HTML?

Comments help improve clarity, readability and organization. They allow you to:

  • Explain what a block of code does

  • Mark sections for future updates

  • Temporarily disable part of the code

  • Leave notes for other developers

  • Indicate the start or end of layout sections

  • Organize long pages by dividing them into labeled segments

Well-written comments save time when revisiting a file after weeks or when someone else needs to understand the structure.

Adding Single-Line Comments

You can write short, simple comments before or after lines of code.

Example:

<!-- Main heading of the page -->
<h1>Travel Diaries from India</h1>

<p>Riya shared her experiences from her Jaipur trip.</p> <!-- Traveler story -->

Single-line comments are useful for short notes or quick reminders.

Adding Multi-Line Comments

For longer explanations, you can write comments that span multiple lines. HTML treats everything between the comment markers as part of the same comment, even if it takes several lines.

Example:

<!--
  This section contains the traveler stories.
  Each paragraph describes a visit to an Indian city.
  More stories will be added later.
-->
<div>
  <p>Kavya visited Mumbai and explored Marine Drive.</p>
  <p>Aarohi enjoyed her stay in Hyderabad and tried famous biryani.</p>
</div>

Multi-line comments are useful when you need to explain entire sections.

Hiding HTML Content Temporarily

Sometimes you want to remove an element from the page without deleting it. Comments allow you to “hide” that element for testing or redesigning.

Example:

<!-- <h2>This heading is temporarily hidden</h2> -->

The heading will not be displayed, but it will remain in your code in case you want to reuse it later.

This technique is helpful when debugging layouts or testing different designs.

Organizing Sections with Comments

Large webpages often contain multiple parts—header, navigation, banner, content, sidebar and footer. Developers use comments to mark section boundaries.

Example:

<!-- Header Section -->
<header>
  <h1>India Travel Blog</h1>
</header>

<!-- Navigation Menu -->
<nav>
  <a href="#">Home</a>
  <a href="#">Stories</a>
  <a href="#">Gallery</a>
</nav>

<!-- Content Section -->
<section>
  <p>Meera visited Pune to explore local cafes and gardens.</p>
</section>

<!-- Footer Section -->
<footer>
  <p>Contact: info@example.com</p>
</footer>

Comments like these keep large files neat and understandable.

Comments Inside the Head Section

Comments are useful inside the <head> for explaining metadata, CSS links or script tags.

Example:

<head>
  <!-- Metadata for search engines -->
  <meta charset="UTF-8">

  <!-- Linking external stylesheet -->
  <link rel="stylesheet" href="styles.css">

  <!-- Page title -->
  <title>Indian City Explorer</title>
</head>

This is especially helpful when your project grows and contains many linked files.

Comments Inside the Body Section

The body contains the actual visible content of a webpage. Comments here help divide content, remove unwanted sections or clarify layout purpose.

Example:

<body>

<!-- Main content starts -->
<h2>Popular Indian Destinations</h2>

<p>Aditi shared her Mumbai travel story.</p>

<!-- Image gallery will be added here -->

<p>Neha explored Bengaluru for a week-long work trip.</p>

<!-- Main content ends -->

</body>

This structure makes collaboration easier when multiple people work on a layout.

Avoid Overusing Comments

Comments are helpful, but too many comments can clutter your file. The goal is to make code readable, not crowded. Use comments only where needed.

Avoid comments like:

<!-- This is a paragraph -->
<p>Riya visited Chennai last year.</p>

This doesn’t add meaningful information. Comments should explain intent, not repeat what is obvious.

Practical Example: Commented Webpage

Here is a complete example showing different types of comments used in one file.

<!DOCTYPE html>
<html>
<head>
    <title>Travel Notes</title>

    <!-- Stylesheet will be added later -->
    <!-- <link rel="stylesheet" href="style.css"> -->
</head>
<body>

<!-- Header with main title -->
<h1>Travel Notes from Indian Cities</h1>

<!-- Introduction paragraph -->
<p>Meera visited Hyderabad and enjoyed exploring its historic sites.</p>

<!-- Stories Section -->
<div>
  <p>Kavya recently traveled to Delhi and explored India Gate.</p>

  <!-- This story is temporarily hidden
  <p>Aarohi visited Udaipur and enjoyed the lakes.</p>
  -->
</div>

<!-- Footer -->
<footer>
  <p>Contact: travel@example.com</p>
</footer>

</body>
</html>

This example demonstrates how comments help organize a simple webpage and make development easier.

Practice Task

Create a webpage with:

  1. Comments marking different sections

  2. A hidden heading using comments

  3. A commented block explaining a group of paragraphs

  4. At least two multi-line comments

  5. Comments inside both the head and body

Practicing this will help you understand how comments shape clean, professional HTML files.

Summary of HTML Comments

HTML comments help you explain, organize and temporarily disable content in your webpage. You learned how to create single-line comments, multi-line comments, hidden content and organized sections. Well-written comments are a valuable habit that makes your code easier to maintain, understand and update over time.


Practice Questions

Q1. Add a comment saying “Header section” before an <h1> tag.

Q2. Write a comment above a paragraph describing the content.

Q3. Disable a <div> element using a comment.

Q4. Write a multiline comment explaining your HTML layout.

Q5. Add a comment to separate the header and footer sections.

Q6. Use a comment to explain a link’s purpose.

Q7. Comment out an image tag and check it disappears.

Q8. Place a comment inside the <head> tag.

Q9. Use comments to label different sections of a web page.

Q10. Write a comment inside a form describing each input field.


Try a Short Quiz.

coding learning websites codepractice

No quizzes available.

Go Back Top