Explain it simply
Explain it simply
A comment is a sticky note inside your code. It starts with <!-- and ends with -->, and the browser does not show anything between them on the page: the note is only for people reading the code. A note like "header starts here" rescues the future you who opens the file again six months later.
But the sticky note is not hidden. The whole file reaches the browser of everyone who opens the page, so anyone can read every comment just by choosing View Source.
Problem → Why → Solution
The problem: a password left in a comment
During development, a team member noted the staging admin login in a comment, thinking "it does not show on the page anyway". After launch, a curious visitor presses Ctrl+U and finds the admin path and the password. Search engines and scraper bots download the full HTML of pages too, so that comment can end up copied in many places.
<!-- TODO before launch: remove staging login
admin page: /admin user: admin password: teashop-2024 -->
<h1>Welcome to ThutaTech</h1>Why does this happen?
The browser does not draw comments, but it does not delete them either. The server sends the whole file to every visitor, and the browser even keeps each comment as a comment node in the DOM, so it is visible in View Source and in DevTools. Secrets belong in environment variables on the server or in a password manager, nowhere in the HTML.
Core concept: comment rules and uses
A comment starts with <!-- (less-than, exclamation mark, two hyphens) and ends with -->. It can be one line or many, and if it contains tags, the browser treats them as plain comment text and does not draw them. That is why you can wrap a piece of markup in a comment to switch it off temporarily without deleting it, which is called "commenting out".
The most important rule is that a comment closes at the first --> the parser meets. So comments cannot be nested: in <!-- A <!-- B --> C -->, the comment ends at the --> after B, and "C -->" appears on the page as text. You usually hit this when commenting out a block of code that already contains a comment.
| Use | Example |
|---|---|
| mark sections in a long file | <!-- ===== Footer ===== --> |
| explain why, not what | <!-- Phone has +95 so it works from abroad --> |
| temporarily disable markup | <!-- <section class="banner">...</section> --> |
Comments can go almost anywhere between tags, in the head as well as the body, but never inside a tag itself: <img <!-- logo --> src="logo.png"> does not work, because inside a start tag the browser expects only attributes.
This syntax only works in HTML. Inside <style>, use CSS comments, /* ... */; inside <script>, use JavaScript comments, // or /* ... */. Write // in the HTML body and it is not a comment at all: it shows up on the page as text. A good comment explains why rather than repeating what: anyone can see that an <h1> is a heading, but the code alone cannot say why the phone number starts with +95.
- <!-- opens the comment
- Everything inside is not drawn
- The first --> closes it
- It still reaches every visitor
Comments are undrawn, not invisible: off the page, but still in the source.
General pattern
<!-- a single-line note -->
<!--
A note that spans
several lines
-->Everything between <!-- and --> is a comment. For multi-line comments, putting the opener and closer on their own lines keeps them easy to read.
Example: marking where a section starts and ends
Here two comments mark the start and end of the hero section. In a small file this looks unnecessary, but once a file grows to hundreds of lines, it makes finding where a section ends much easier.
<!-- Hero section starts here -->
<section>
<h1>Welcome to ThutaTech</h1>
</section>
<!-- Hero section ends here -->Code breakdown
- Line 1
<!-- Hero section starts here -->A single-line comment; the browser does not draw the text between <!-- and -->.
- Line 2
<section>Normal markup continues after the comment. The comment sits outside the section, so it is not one of the section's children.
- Line 3
<h1>Welcome to ThutaTech</h1>The only content that is visible on the page.
- Line 5
<!-- Hero section ends here -->An end marker after </section>. In files with many nested sections, it shows which </section> closes which section.
Only the heading "Welcome to ThutaTech" appears on the page. Neither comment line is shown there, but both are visible in View Source (Ctrl+U) and in the DevTools Elements panel.Commenting out markup, and the nesting trap
To hide a banner until the Thadingyut sale starts without deleting it, wrap it in a comment. The banner contains no comment of its own, so this works: only the h1 appears on the page.
<!-- Sale banner, hidden until the sale starts
<section class="banner">
<h2>Thadingyut sale: 30% off</h2>
</section>
-->
<h1>Welcome to ThutaTech</h1>The footer below already contains a comment. The outer comment ends at the --> after "contact details", so the phone paragraph and the final --> reappear on the page. The fix is to remove the inner comment first, or to use your editor's comment shortcut (Ctrl+/ in VS Code) line by line.
<!-- Old footer, disabled:
<footer>
<!-- contact details -->
<p>Call 09 123 456 789</p>
</footer>
-->In real projects
How this is used in real projects
Teams use comments sparingly and on purpose: section markers in long templates, and a note with an issue-ticket link explaining why some odd markup works around a browser bug. Many build tools can strip HTML comments from production files to make them smaller, but whether they do depends on configuration, so never count on stripping to protect a secret.
When you read the source of real websites, you will also find comments nobody on the team wrote. WordPress plugins often leave comments with their name and version, and attackers read those versions to look up known vulnerabilities. Pages server-rendered with React often contain small empty comments, <!-- -->, which the framework uses as markers between pieces of text. Checking comments before launch is a routine step in security reviews.
Common mistakes
Common mistakes
1. Keeping secrets in comments
Passwords, API keys, internal URLs and customer data in comments can be read by anyone through View Source, and "delete before launch" comments are exactly the ones that get forgotten. Never put a secret in the HTML at all.
2. Commenting out code that already contains a comment
The outer comment ends at the inner -->, so half of the markup you thought was hidden, plus a stray "-->", shows up on the page. Find and remove any inner comments before commenting a block out.
3. Writing // comments in HTML
Out of JavaScript habit, you write // <p>Old price</p>: "//" appears on the page as text and the paragraph still shows normally. /* */ does not work in HTML either. Use <!-- --> in HTML.
4. Leaving outdated comments behind
Change the code but not the comment, and <!-- Shows 3 products --> now lies above code that shows six. Update comments together with the code, and delete commented-out code you no longer need; git history keeps it anyway.
Mini challenge
Mini challenge: a page organised with comments
Build a small page with a header, main content and footer, then use comments to mark the sections, switch off a sale banner and explain one decision.
- A marker comment above each of the three sections
- A sale banner section with an h2 that is commented out and does not appear on the page
- A comment in the footer that explains why, not what
- No stray "-->" text anywhere on the page
<!-- TODO: a marker comment, then a header with the site name in an h1 -->
<!-- TODO: a sale banner section with an h2, commented out until it is needed -->
<!-- TODO: a marker comment, then main content with one paragraph -->
<!-- TODO: a marker comment, then a footer with a phone number -->
<!-- TODO: inside the footer, a comment that explains WHY the number has +95 -->Expected: The page shows only the site name heading, one paragraph and the footer with the phone number. The sale banner and all comments are visible only in View Source.
Show hint
When commenting out the banner, make sure it contains no other comment. Open the page and press Ctrl+U to confirm the comments are still there in the source.
Show solution
<!-- ===== Header ===== -->
<header>
<h1>ThutaTech</h1>
</header>
<!-- Sale banner: hidden until the Thadingyut sale starts
<section class="banner">
<h2>Thadingyut sale: 30% off all courses</h2>
</section>
-->
<!-- ===== Main content ===== -->
<main>
<p>Short, practical web development lessons in Myanmar and English.</p>
</main>
<!-- ===== Footer ===== -->
<footer>
<!-- +95 country code so the number also works when dialled from abroad -->
<p>Call +95 9 123 456 789</p>
</footer>Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- HTML comments start with <!-- and end with -->, and may span many lines.
- A comment ends at the first -->, so comments cannot be nested.
- Use them for section markers, explaining why, and temporarily disabling markup.
- Every visitor can read comments through View Source, so never keep secrets in them.
HTML Living Standard — HTML Syntax — WHATWG