HTML Comments

HTML Comments are used to add notes inside HTML code that are not displayed in the browser.
They help developers understand and manage code easily.

In this tutorial, you will learn how to write comments in HTML, their uses, and see examples with output.

What Are HTML Comments

HTML comments are invisible notes added in the code.

They are used to:

  • Explain code
  • Add reminders
  • Hide content temporarily

Comments are not shown in the browser output.


HTML Comment Syntax

Example

<!-- This is a comment -->

Explanation

PartDescription
<!--Start of comment
-->End of comment

HTML comments, HTML comment syntax, add comments in HTML, HTML notes, HTML basics


Simple Comment Example

Example Code

<!-- This is a heading -->
<h1>Hello HTML</h1>

Output

Hello HTML

(Comment is not visible)


Comment Inside Paragraph

Example Code

<p>This is a paragraph <!-- hidden text --> in HTML.</p>

Output

This is a paragraph in HTML.


Multi-Line Comment

HTML comments can span multiple lines.

Example Code

<!--
This is a multi-line comment
used to explain code
-->
<p>Learning HTML comments</p>

Output

Learning HTML comments


Using Comments to Hide Code

You can hide HTML code using comments.

Example Code

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

Output

This paragraph is visible


Why Use HTML Comments

Benefits of Comments

BenefitDescription
Code ExplanationHelps understand code
DebuggingTemporarily hide code
Team WorkHelps other developers
OrganizationKeeps code clean

HTML Comments Summary Table

FeatureDescription
VisibleNo
Syntax<!-- comment -->
Multi-lineYes
Used forNotes and explanations

Best Practices for Comments

PracticeDescription
Keep comments clearWrite meaningful notes
Do not overuseAvoid too many comments
Update commentsKeep them relevant
Use for sectionsDivide code into parts

Common Mistakes

MistakeResult
Missing closing -->Code error
Nested commentsNot supported
Overusing commentsCluttered code
Writing important dataNot secure

HTML Comments Example with Structure

Example Code

<!DOCTYPE html>
<html>
<head>
<!-- Page title -->
<title>HTML Comments</title>
</head>
<body> <!-- Main heading -->
<h1>Welcome</h1> <!-- Paragraph -->
<p>This is a simple example.</p></body>
</html>

Output

Welcome
This is a simple example.


You may like


Frequently Asked Questions (FAQ)

1. What are HTML comments?

They are notes in HTML code that are not visible in the browser.

2. How do you write a comment in HTML?

Using <!-- comment -->.

3. Can comments hide content?

Yes, they can temporarily hide code.

4. Are comments visible to users?

No, they are hidden.

5. Can comments span multiple lines?

Yes, multi-line comments are supported.

Related Article
Introduction to HTML

HTML Introduction – Learn the Basics of HTML HTML is the starting point of web development. Every website you see Read more

What is HTML

What Is HTML – Complete Beginner Explanation HTML is the foundation of the web. Every website you visit, from simple Read more

HTML Basic Examples

HTML Basic Examples – Learn HTML by Practice The best way to learn HTML is by writing and testing simple Read more

HTML text Editors

HTML Text Editors – Learn How to Write HTML Code To write HTML code, you need a text editor. A Read more

HTML Tags

HTML Tags are the basic units used to create HTML elements.They define how content is displayed in a web browser. Read more

HTML Elements

HTML Elements are the building blocks of every web page.Each element defines a part of the webpage, such as headings, Read more

HTML Attributes

HTML Attributes provide additional information about HTML elements.They help control how elements behave, display, or store extra data. In this Read more

HTML Headings

HTML Headings are used to define titles and subtitles on a web page.They help organize content and improve readability for Read more