HTML comments are text that browsers ignore but other developers can read
An HTML comment is a note you write in your code that the browser does not display on the webpage. When someone visits your site, they see the page normally — the comment stays hidden. Other developers who read your code, or you reading your own code months later, can see the comment and understand what a section of code does or why you wrote it that way.
Comments are useful when you are working on a project with other people, when you need to remember why you made a choice, or when you want to temporarily hide code without deleting it. The syntax is straightforward and works the same way in every HTML file.
Key Takeaways
- HTML comments start with <!-- and end with -->, with your text in between.
- Anything inside a comment is completely hidden from the webpage display and does not affect how the page looks or works.
- You can write a comment on a single line or span multiple lines, and you can place comments anywhere in your HTML file.
- Comments are visible to anyone who views the page source code, so do not put sensitive information like passwords or API keys in comments.
The basic syntax for writing a comment
To write a comment in HTML, type <!--, then your comment text, then -->. Everything between those markers is a comment.
Here is a single-line comment:
<!-- This is a comment -->
Here is a comment that spans multiple lines:
<!-- This is a longer comment that takes up more than one line and explains something important -->
The browser reads the opening <!--, skips everything until it finds -->, and continues reading the rest of your HTML. The comment never appears on the webpage.
Where to place comments in your HTML file
You can put a comment anywhere in your HTML document. A common place is above a section of code to explain what that section does.
<!-- Navigation menu for the top of the page --> <nav> <ul> <li><a href="/">Home</a></li> <li><a href="/about">About</a></li> <li><a href="/contact">Contact</a></li> </ul> </nav>
You can also place a comment next to a single line of code to explain what that line does.
<img src="logo.png" alt="Company logo"> <!-- Logo appears in the header -->
You can even use comments to temporarily hide code you are not using yet. If you want to test your page without a certain section, wrap that section in comment markers instead of deleting it.
<!-- Hiding the footer for now, will add back later <footer> <p>Copyright 2024</p> </footer> -->
What you should and should not put in comments
Comments work best for explaining your code to other developers or to yourself. Write a comment when you have made an unusual choice, when a section is complex, or when you want to note something you plan to change later.
Do not put passwords, API keys, or other sensitive information in comments. Anyone who views the page source code can read your comments, even though they cannot see them on the webpage itself. If you need to store secret information, keep it in a separate file on your server that is not visible to the public.
Avoid comments that just repeat what the code already says. A comment like <!-- This is a paragraph --> above a <p> tag does not add information. A useful comment explains why the code is there, not just what it is.
Common mistakes when writing comments
The most common mistake is forgetting the closing -->. If you leave it out, the browser treats everything after <!-- as a comment until it finds --> somewhere else in your file. This can hide code you did not mean to hide.
Another mistake is putting two hyphens -- inside your comment text. Technically, this can confuse some browsers about where the comment ends. To be safe, avoid -- inside the comment itself.
<!-- Do not write: This is a comment -- with extra hyphens -->
Instead, use a different word or rephrase:
<!-- Do not write: This is a comment with extra hyphens -->
Viewing comments in your browser
To see the comments in an HTML file, open the page in your browser and right-click anywhere on the page. Select "View Page Source" or "Inspect" (the exact wording depends on your browser). A new window or panel opens showing the HTML code, including all the comments. This is how other developers read your comments, and how you can check that your comments are written correctly.
In Firefox, right-click and choose "View Page Source". In Chrome, right-click and choose "Inspect" or "View page source". In Safari, you may need to enable the developer menu first, then right-click and choose "Inspect Element".
Frequently Asked Questions
Can I nest comments inside other comments?
No. Once the browser sees <!--, it looks for the first --> to close the comment. If you try to put a comment inside a comment, the inner comment will not work the way you expect. Keep comments separate and non-nested.
Do comments slow down my webpage?
Comments add a tiny amount of file size, but not enough to matter for most websites. If you are concerned about performance on a very large site, you can use a build tool to remove comments before publishing, but this is rarely necessary for small to medium projects.
Can I use comments to hide code from hackers?
No. Comments are visible to anyone who views the page source. Never use comments to hide sensitive information like passwords or API keys. If you need to hide code, remove it from the public file entirely or store secrets on your server where users cannot access them.
What is the difference between an HTML comment and a comment in CSS or JavaScript?
Each language has its own comment syntax. HTML uses <!-- -->. CSS uses /* */. JavaScript uses // for single-line comments and /* */ for multi-line comments. You must use the correct syntax for the language you are writing in.