Example 1 — Basic Comment Syntax
<h1>Comments demo</h1>
<!-- This note is for developers only -->
<p>This paragraph is visible on the page.</p> How It Works
Only the h1 and p render. The comment line is skipped entirely.

HTML comments are snippets of text or code that are not visible to visitors when the webpage is rendered. They help developers understand, organize, and navigate source files.
Comments can also temporarily disable markup during development and testing without deleting it. This tutorial covers syntax, practical use cases, pitfalls, and a complete annotated page example.
<!-- -->
Explain code.
Comment out.
head & body.
No nesting.
Best habits.
HTML comments are text blocks enclosed within special delimiters that the browser ignores during rendering. They let developers leave notes, explanations, or reminders in the markup without affecting what users see on screen.
Comments are for humans reading source code—not for styling (use CSS) or hiding content from users (use appropriate HTML/CSS techniques instead).
There is no <comment> HTML element. Comments are written with the <!-- ... --> syntax only. See the comment tag reference for a deep dive on rules and edge cases.
To write a comment in HTML, use this syntax:
<!-- This is a comment --> Everything between <!-- and --> is a comment and will not appear in the browser. The delimiters themselves are never shown on the page.
For longer notes, span multiple lines:
<!--
Navigation partial
Last updated: July 2026
--> During development, you may want to temporarily disable a section of HTML without deleting it. Wrap the markup in comment delimiters:
<!--
<p>This paragraph is commented out and will not appear on the webpage.</p>
--> This technique is useful for A/B testing layouts, isolating bugs, or keeping draft content in the file until it is ready to publish.
Commented-out scripts and styles are still in the file—they are just not parsed as HTML. Do not comment out production security headers or critical accessibility markup and forget to restore them.
HTML does not support nested comments. If you put --> inside what you think is an outer comment, the browser closes the comment at the first -->, which can expose raw markup or break the page.
<!--
This is a comment
<!-- This inner comment is NOT supported -->
The rest may be parsed as HTML!
--> If you need to disable a block that already contains comments, remove or rewrite the inner <!-- / --> pairs first, or use version control instead of nesting.
Comments can be placed almost anywhere in an HTML document—in the head, body, or between elements. They are commonly used to label major sections:
<head> — document scripts, stylesheets, or meta tags.<body> — mark header, main, sidebar, and footer regions.You can also place a comment inline inside a paragraph:
<p>This is a paragraph. <!-- Comment inside a paragraph --> This text is visible.</p> Avoid placing comments inside attribute values (e.g. inside class="...") or splitting opening tags—that breaks HTML parsing.
HTML comments do not appear on the rendered page, so they do not directly change how content looks to users or screen readers. Search engines generally ignore comment text for ranking purposes.
However, excessive comments increase HTML file size slightly, which can marginally affect download time on very large pages. Keep comments concise and remove obsolete notes before shipping to production when file size matters.
Security: Comments are visible in View Source. Never store passwords, API keys, internal URLs, or personal data in comments.
| Task | Syntax |
|---|---|
| Single-line comment | <!-- note --> |
| Multi-line comment | <!-- line 1 |
| Comment out HTML | Wrap tags in <!-- ... --> |
| Opens comment | <!-- |
| Closes comment | --> |
| Nested comments | Not supported |
| Visible to users? | No (only in source) |
Six examples from basic syntax to a full documented page. Each includes View Output and Try It Yourself.
Syntax and disabling markup.
<h1>Comments demo</h1>
<!-- This note is for developers only -->
<p>This paragraph is visible on the page.</p> Only the h1 and p render. The comment line is skipped entirely.
<p>This paragraph appears.</p>
<!--
<p>This paragraph is commented out.</p>
--> The second paragraph is inside the comment block, so the browser never creates it in the DOM.
Multi-line notes and inline reminders.
<!--
Site header region
Updated: July 2026
-->
<header>
<h1>My Blog</h1>
</header> Multi-line comments help teammates find sections quickly in large templates.
<p>
This is a paragraph.
<!-- TODO: add link here -->
This text is still visible.
</p> The TODO comment is invisible; surrounding sentence text flows normally.
<head>
<title>My Page</title>
<!-- Primary stylesheet — do not remove -->
<link rel="stylesheet" href="styles.css">
</head> Document critical assets in head so future edits do not accidentally remove required files.
A simple HTML document with comments in head, body, and footer:
<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML Comments Example</title>
<!-- Head: metadata and assets -->
</head>
<body>
<h1>Welcome to My Website</h1>
<!-- Main content starts here -->
<p>This is a paragraph on the website.</p>
<!-- <p>This paragraph is commented out.</p> -->
<footer>
<!-- Footer information -->
<p>© 2026 My Website</p>
</footer>
</body>
</html> Comments label each region. The inner commented p is disabled; the footer comment documents purpose without showing text.
header, main, and footer in large files.HTML comments have been supported since the earliest HTML specifications. Every modern and legacy browser ignores comment content when parsing markup.
HTML comments have been supported since the earliest HTML specifications. Every modern and legacy browser ignores comment content when parsing markup.
Bottom line: Use comments freely in any HTML document. Behavior is identical across browsers.
HTML comments are a valuable tool for developers. They document code, organize complex projects, and let you disable markup safely during development. Follow best practices, avoid nesting, and never treat comments as a place for secrets.
For exhaustive rules and edge cases, read the HTML comment tag reference. Next, continue with HTML Multimedia to embed audio and video in your pages.
Syntax.
CoreNot on page.
RenderComment out.
DebugOne level.
RulesNo secrets.
SecurityHTML comments inspired similar syntax in other languages—but the rules differ. In HTML, the sequence --> anywhere inside a comment ends it immediately, which is why nested comments fail. CSS and JavaScript have their own comment syntax (/* */ and //) that does not work in HTML markup.
Comment out paragraphs, add TODO notes, and see what still renders on the page.
6 people found this page helpful