When writing HTML, you may sometimes want to leave a note in your code. For example, you might write a note to remind yourself what a particular section does. You don’t want this note to appear on the webpage. That’s where HTML comments are useful.
What is an HTML Comment?
An HTML comment is a note written inside the HTML code that the browser does not show on the webpage.
This is the HTML comment syntax:
<!-- This is a comment -->
Anything written between <!-- and --> is treated as a comment.
Creating Your First Comment
Let’s see a simple example.
<!DOCTYPE html>
<html>
<head>
<title>HTML Comments</title>
</head>
<body>
<!-- This is the main heading -->
<h1>My Website</h1>
<p>Welcome to my website.</p>
</body>
</html>
Output
My Website
Welcome to my website.
Notice that:
<!-- This is the main heading -- >
does not appear on the webpage. The browser simply ignores it when displaying the page.
Why Use Comments?
Comments are mainly useful for understanding and organizing your code. For example, if your webpage has different sections, you can leave a note before each one.
<!DOCTYPE html>
<html>
<head>
<title>My Website</title>
</head>
<body>
<!-- Header section -->
<header>
<h1>My Website</h1>
</header>
<!-- About section -->
<section>
<h2>About Me</h2>
<p>I am learning HTML.</p>
</section>
<!-- Footer section -->
<footer>
<p>© 2026 My Website</p>
</footer>
</body>
</html>
Output
My Website
About Me
I am learning HTML.
© 2026 My Website
The comments help you understand the code, but visitors don’t see them on the webpage.
Comments Can Be on Multiple Lines
A comment doesn’t have to be only one line.
<!--
This is a longer comment.
It can contain more than one line.
-->
Everything between <!-- and --> is treated as a comment.
One Important Thing
Comments are not completely private. A visitor normally won’t see comments on the webpage, but they can view the HTML source code in their browser. So don’t put passwords, private information, or secret information inside comments.
Quick Recap
- Comments are notes written inside HTML code.
- The browser does not display comments on the webpage.
- Comments start with
<!--and end with-->. - They help you understand and organize your code.
- Comments can also temporarily hide HTML code.
- Never put passwords or private information inside comments.
Frequently Asked Questions (FAQs)
Q1. What are HTML Comments?
HTML Comments are notes written inside HTML code that are not displayed as part of the webpage. They help developers understand and organize their code.
Q2. How do you write a comment in HTML?
To write an HTML Comment, place the text between <!-- and -->. For example: <!-- This is my comment -->.
Q3. Are HTML Comments visible on a webpage?
No. HTML Comments are not normally displayed on the webpage. However, visitors can view them by looking at the page’s HTML source code.
Q4. Why are HTML Comments used?
HTML Comments are used to add notes, explain sections of code, organize a webpage, and temporarily comment out HTML code while testing.
Q5. Can HTML Comments have multiple lines?
Yes. HTML Comments can contain multiple lines as long as the entire comment starts with <!-- and ends with -->.
Q6. Can HTML Comments be used to hide HTML code?
Yes. You can temporarily place HTML code inside a comment so the browser does not display or render that code.
Q7. Should you put passwords or private information in HTML Comments?
No. HTML Comments should never contain passwords, private information, API keys, or other secrets because the HTML source code can be viewed by visitors.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
