Lesson 13 of 51 · Module 2, lesson 7 of 9 · 3 min
Comments
Leave notes in your code that the browser ignores, and use them to find problems.
What you'll learn
- How to write a comment in HTML
- What comments are good for
- What never to put in a comment
Why this matters
Code is read far more often than it is written — by you, weeks later, and by other people. A short note in the right place saves time. Comments are also a safe way to switch off part of a page while you look for a problem.
Writing a comment
A comment starts with <!-- and ends with -->. Everything between them is ignored by the browser. It does not appear on the page.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Soup recipe</title>
</head>
<body>
<!-- Main title: keep it the same as the <title> -->
<h1>Tomato soup</h1>
<!-- TODO: add a photo of the soup here -->
<p>A warm soup for four people.</p>
<!--
Old version, kept for now:
<p>A soup for two people.</p>
-->
</body>
</html>
What you'll see: only the heading and one paragraph. None of the comments appear — not even the old paragraph inside the last comment.
A comment can be on one line, or across several lines like the last one.
Good uses for comments
- Label the big parts of a page, such as
<!-- Recipe steps -->, so that you can find them quickly. - Explain why, when the reason is not obvious from the code.
- Leave a reminder, such as
<!-- TODO: add a photo -->. Many developers use the word TODO so that they can search for it later. - Switch off code while you test. If the page looks wrong, put part of it inside a comment. If the problem disappears, you have found where it is.
A shortcut in VS Code
Select one or more lines and press Ctrl + / (Windows) or Command + / (macOS). VS Code wraps them in a comment. Press it again to remove the comment.
Comments are not secret
The browser does not show comments, but anyone can see them with View Source or the developer tools. Never put passwords, private notes about people or anything else private in a comment.
Try it yourself
- Select the example above and choose Try it.
- Remove the
<!--and-->around the old paragraph. It appears on the page. - Put the heading inside a comment using the shortcut. It disappears. Undo it.
Common mistakes
- Forgetting to close a comment. Everything after
<!--disappears from the page, often most of it. How to spot it: in VS Code the rest of the file turns the comment colour (usually green or grey). Fix: add-->. - Putting a comment inside a comment. The first
-->ends the whole comment, and the rest shows on the page. Fix: never nest comments. - Writing private information in a comment. Anyone can read it with View Source. Fix: keep secrets out of your code.
- Commenting every line. Too many comments hide the code. Fix: comment the parts that need explaining.
Check yourself
- What are the opening and closing marks of an HTML comment?
- Can a visitor read your comments? How?
- How can comments help you find a problem in a page?
Answers
<!--and-->.- Yes, with View Source or the developer tools.
- Put part of the page inside a comment. If the problem goes away, it is in that part.
Next: some characters, such as <, have a special meaning in HTML. The next lesson shows how to write them as text — and how to add symbols and emoji.

