HTML Comments
A comment is a note in your code that the browser ignores. It is not shown on the page. Comments help you and other developers to understand the code later.
Syntax
<!-- This is a comment -->
A comment starts with <!-- and ends with -->.
Examples
Single-line comment
<!-- Main heading of the page -->
<h1>Welcome</h1>
Output in the browser
Welcome
Multi-line comment
<!--
This is a long comment.
It can go on many lines.
-->
<p>Hello</p>
Comment out code
You can also use a comment to hide a part of the code for some time, without deleting it.
<p>This paragraph is visible.</p>
<!-- <p>This paragraph is hidden.</p> -->
Output in the browser
This paragraph is visible.
Keyboard shortcut
In most editors (VS Code, Sublime Text) select the lines and press Ctrl + / (or Cmd + / on Mac) to add or remove HTML comments.
Note:
Comments are hidden on the page, but they are not secret. Anyone can see them with “View page source”. Never write passwords or private notes in an HTML comment.
Read the interview question How to add comments in HTML?
Summary
- Write comments between
<!--and-->. - The browser does not show them.
- Use them for notes and to switch off code for a while.
- Do not store secrets in comments.
Congratulations! Chapter Finished. Learn more about the similar topics:
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| How to add comments in HTML? |