HTML Comments

Beginner
8 min read
Updated: Nov 2026
6 examples
<!-- -->

Definition and Usage

An HTML comment adds explanatory notes to your markup or prevents the browser from interpreting specific parts of the document. Comments start with <!-- and end with -->. They are not rendered as page content.

What are comments for in an HTML tutorial?

Label sections for teammates, leave TODOs, or temporarily disable a block while debugging—without deleting the code. The browser skips the comment; View Source still shows it.

Where can you place comments?

As content between tags (in <head>, <body>, inside most elements). You cannot put a comment inside a start tag (for example before an attribute). Avoid HTML comments as the content of raw-text elements like <script>, <style>, <title>, and <textarea>—use JS or CSS comment syntax there instead.

When should you use them?

Document non-obvious structure, mark regions in large templates, or comment out experimental markup. Keep notes short; never store secrets in comments.

Not an HTML element.

A comment is a special construct in the HTML syntax—not a tag you open and close like <p>.

Comments cannot nest.

The first --> ends the comment. Nested <!-- breaks parsing and can spill markup onto the page.

Visible in source.

Anyone can open View Source or DevTools and read your comments. Treat them as public notes.

HTML Comment Syntax

The basic form:

comment.html
<!-- Comment text -->
HTML
<!-- A one-line comment -->

<!--
A comment
that stretches
over several
lines
-->

<!-- Disables the HTML contained within -->
<!--
<p>This content will not be rendered.</p>
-->

Empty comments are allowed: <!--> or <!---->. Comment text must not start with > or ->, must not contain --> or --!>, and must not end with <!-.

Core HTML Concepts

Rules that keep comments valid and useful:

ConceptMeaning
<!-- … -->Opens and closes an HTML comment
Not renderedBrowser skips comment content when painting the page
Document / disableNotes for developers, or wrap markup to turn it off
Content onlyBetween tags—not inside a tag’s attribute list
No nestingFirst --> closes the comment
Raw-text elementsPrefer JS/CSS comments in script / style (and avoid HTML comments in title / textarea)
View SourceComments remain in the delivered HTML file

Comment vs element

HTML commentHTML element (e.g. <p>)
Visible on page?NoUsually yes
In DOM as a node?Comment node (tools can see it)Element node
Attributes?NoYes

How to Use Comments in HTML

Practical habits for clean markup.

  1. Write <!-- note --> next to the markup it explains.
  2. Explain why, not what every obvious tag does.
  3. To disable a block, wrap it in one comment—do not nest comments.
  4. Label major regions (<!-- Site header -->) in large templates.
  5. Remove obsolete comments when the code changes.
  6. Never put passwords, tokens, or private customer data in comments.
GoalPattern
One-line note<!-- Primary stylesheet -->
Multi-line note<!-- … several lines … -->
Disable markup<!-- <p>…</p> -->
Script notes// or /* */ inside <script>
CSS notes/* … */ inside <style> or a stylesheet

Live Preview

Toggle whether a paragraph is commented out and see what the browser renders.

Shows rendered output vs source shape. Comments never appear as page text.

Rendered page

Comments demo

This paragraph is visible on the page.

<!-- This note is for developers only -->
<p>This paragraph is visible on the page.</p>

HTML Examples

Six patterns: basic syntax, commenting out markup, multi-line notes, inline TODOs, head notes, and a full page.

Example 1 — Basic Comment Syntax

HTML
<h1>Comments demo</h1>
<!-- This note is for developers only -->
<p>This paragraph is visible on the page.</p>
Try It Yourself

How It Works

  1. <!-- … --> marks text the browser ignores for rendering.
  2. h1 and p still display normally.
  3. View Source still shows the comment line.
  4. Use for short developer notes next to markup.

Example 2 — Comment Out a Block

HTML
<p>This paragraph appears.</p>
<!--
<p>This paragraph is commented out.</p>
-->
Try It Yourself

How It Works

  1. Wrap markup to disable it without deleting files.
  2. One block — do not put another --> inside.
  3. Restore by removing the comment delimiters.
  4. Great for A/B drafts and temporary UI experiments.

Example 3 — Multi-Line Section Comment

HTML
<!--
  Site header region
  Updated: Nov 2026
-->
<header>
  <h1>My Blog</h1>
</header>
Try It Yourself

How It Works

  1. Several lines can sit between one open and one close.
  2. Section labels help navigate large HTML templates.
  3. Keep current — update the “Updated” note when the region changes.
  4. Still public in View Source.

Example 4 — Inline Comment in a Paragraph

HTML
<p>
  This is a paragraph.
  <!-- TODO: add link here -->
  This text is still visible.
</p>
Try It Yourself

How It Works

  1. Comments inside elements are fine as content nodes.
  2. Surrounding text still flows in the paragraph.
  3. TODO comments remind you of unfinished work.
  4. Whitespace around the comment may collapse like normal HTML text.

Example 5 — Comments in the Head

HTML
<head>
  <title>My Page</title>
  <!-- Primary stylesheet — do not remove -->
  <link rel="stylesheet" href="styles.css">
</head>
Try It Yourself

How It Works

  1. head can hold comments between metadata tags.
  2. Document assets so critical links are not deleted by mistake.
  3. No on-page UI — comments never paint.
  4. See also the HTML Head metadata tutorial.

Example 6 — Complete HTML Page

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <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>&copy; 2026 My Website</p>
  </footer>
</body>
</html>
Try It Yourself

How It Works

  1. Comments label head, main, and footer regions.
  2. One paragraph is disabled inside a comment.
  3. Visible content is only the active tags.
  4. Source still includes every comment for developers.

Edge Cases & Pitfalls

Broken pages often come from nested comments or forbidden strings inside the comment body.

nest

Nested comments

The first --> closes the comment. Leftover text can become live HTML.

inside

Comment inside a start tag

Invalid. Place comments between elements, not among attributes.

script

HTML comments wrapping entire scripts

Legacy practice for ancient browsers. Do not rely on it—use proper JS comments.

secrets

Secrets in comments

View Source exposes them. Use environment config, not HTML comments.

strings

Forbidden sequences in the body

Avoid starting with > / ->, containing --> / --!>, or ending with <!-.

bloat

Huge comment blocks in production

They increase download size slightly and clutter source. Prefer short, useful notes.

Browser Support

HTML comments () are part of the HTML language and work in all browsers.

✓ Baseline · Widely available

HTML comments

Every browser that parses HTML understands comments and skips them when rendering.

100% comment syntax
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
HTML comments Universal

Bottom line: Use freely for developer notes—just remember they ship with your HTML source.

Key Takeaways

  • <!-- … --> documents or disables markup without showing on the page.
  • Cannot nest and cannot sit inside a start tag.
  • View Source still shows comments—no secrets.
  • script/style use language comments, not HTML comments.

One line: Write <!-- notes or disabled HTML --> between tags so the browser skips it while teammates can still read the source.

Frequently Asked Questions

An HTML comment adds notes to your markup or hides markup from the browser. It starts with <!-- and ends with -->. The browser does not render comment content on the page.
Use <!-- your note here -->. For several lines, keep text between the same opening and closing delimiters. Do not put --> inside the comment body.
Not on the rendered page, but comments appear in View Source, DevTools, and downloaded HTML. Never put secrets, API keys, or private data in comments.
Wrap the markup in <!-- and -->. The browser skips that block. Remove the delimiters when you want the markup active again. Comments cannot nest.
No. Comments are only allowed as content—between tags—not inside a start tag before attributes.
No for modern code. Use JavaScript // or /* */ and CSS /* */. title, textarea, script, and style treat their contents as raw text, so HTML comments are not appropriate there.

Practice in the editor

Open a Try It demo and comment markup in and out.

Open Try It editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful