HTML Comments

Beginner
5 min read
Updated: Oct 2026
4 examples
Live preview

Definition and Usage

An HTML comment is a note in your markup that the browser does not display on the page. Use it to explain code, label sections, or temporarily disable HTML while you debug.

Comments use the <!-- --> delimiters. There is no <comment> element — comments are syntax, not a tag like <p> or <div>.

Baseline · Widely available.

Not deprecated. Core HTML comment support is stable in every modern browser (and has been for decades).

Deprecated / legacy.

Do not use Internet Explorer conditional comments (<!--[if IE]>). Do not wrap <script> bodies in HTML comments — that old habit is obsolete; use JavaScript comments instead.

Not an HTML element.

A comment is not a DOM element you style or script. Prefer View Source or DevTools to inspect it.

HTML Syntax

syntax.html
<!-- comment text -->

Start with <!-- and end with -->. Single-line and multi-line forms use the same delimiters. Comments are only allowed as content (between tags) — not inside a start tag before attributes.

  • Do not nest comments; the first --> closes the comment.
  • Comment text must not start with > or ->, must not contain --> or --!>, and must not end with <!- (HTML living standard).
  • Inside <script> and <style>, use language comments (//, /* */), not HTML comments.

Attributes

HTML comments are not an element, so they have no attributes.

ItemDescriptionTry it
<!-- -->Only valid form — start delimiter, optional text, end delimiterTry it
class / idNot applicable — comments are not DOM elementsN/A
NestingForbidden — first --> ends the commentTry it

Global Attributes

Because a comment is not an HTML element, it does not take Global Attributes in HTML. Those attributes apply to real elements such as <p> and <div>.

Live Preview

Pick a comment style. The page shows only visible HTML; the comment stays in the source line below.

Comments never appear in the rendered page — only in source.

Rendered page

Welcome! Please log in to continue.

Source includes: <!-- Login message section -->

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Document Your Code

Leave a short note that explains what the next markup does.

HTML
<!-- This section handles the login message -->
<p>Welcome! Please log in to continue.</p>
Try It Yourself

How It Works

  1. Open the comment with <!--.
  2. Write a clear note for humans reading the source.
  3. Close with --> before the real HTML.
  4. Render. The browser shows only the <p>, not the comment.

Example 2 — Comment Out HTML for Debugging

Wrap markup in a comment to disable it without deleting the code.

HTML
<!--
<div class="alert">
  Temporarily disabled block
</div>
-->
<p>Only this paragraph renders.</p>
Try It Yourself

How It Works

  1. Place <!-- before the markup you want to hide.
  2. Keep the HTML inside so you can restore it later.
  3. Close with --> after that block.
  4. Check the page. Only content outside the comment is rendered.

Example 3 — Label Page Sections

Use comments as signposts in larger HTML files.

HTML
<!-- ===== Header ===== -->
<header>
  <h1>Site Header</h1>
</header>

<!-- ===== Main ===== -->
<main>
  <p>Page content goes here.</p>
</main>
Try It Yourself

How It Works

  1. Add a label comment above each major region.
  2. Keep labels short (Header, Main, Footer).
  3. Write the real elements after each comment.
  4. Browse the page. Only <header> and <main> content shows.

Example 4 — Rules and Common Mistakes

Valid syntax, no nesting, and never store secrets in comments.

HTML
<!-- Valid: short source note -->

<!-- Valid multi-line comment
     that spans several lines -->

<!-- WRONG ideas (do not do these):
     - invent a <comment> element
     - nest <!-- inside another comment
     - store API keys or passwords here
-->

<p>Only this paragraph is visible.</p>
Try It Yourself

How It Works

  1. Use only <!-- -->. There is no <comment> tag.
  2. Never nest. The first --> ends the comment.
  3. Treat comments as public. View Source shows them to everyone.
  4. Prefer short notes. Long commented-out blocks belong in version control history once you are done debugging.

Edge Cases & Pitfalls

Most comment bugs come from nesting, wrong placement, or treating source notes as private.

security

Comments are public

View Source and DevTools show every comment. Never store secrets there.

syntax

No nesting

The first --> closes the comment. Nested <!-- breaks the following markup.

placement

Not inside a start tag

Comments are content only. Do not put them between a tag name and its attributes.

raw text

Avoid in script / style / title / textarea

Those elements treat content as raw text. Use JS/CSS comments inside <script> and <style>.

legacy

IE conditional comments are gone

<!--[if IE]> was a non-standard IE feature. Do not use it in modern pages.

Browser Support

HTML comments are core HTML syntax in every browser. They are not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

<!-- -->

Supported everywhere for decades. Chrome, Firefox, Safari, Edge, and Opera all ignore comments when rendering and keep them in the source.

100% Global support (approx.)
Google Chrome All
Full support
Mozilla Firefox All
Full support
Apple Safari All
Full support
Microsoft Edge All
Full support
Opera All
Full support
Samsung Internet All
Full support
Internet Explorer All
Supported
<!-- --> Widely supported

Bottom line: Safe to use everywhere for documentation and temporary disable. Treat comments as public text in the delivered HTML.

Key Takeaways

  • Syntax: only <!-- … --> — there is no <comment> tag.
  • Visibility: hidden on the page, visible in View Source.
  • Uses: document sections, label regions, or comment out markup while debugging.
  • Rules: no nesting, no secrets, no HTML comments inside script/style bodies.

One line: <!-- --> is for developers reading the source — never for hiding private data.

Default CSS Settings

Comments do not create boxes in the page, so user-agent stylesheets do not style them. There is no default CSS to override.

default.css
/* HTML comments are not rendered nodes.
   No user-agent CSS applies. */

To hide visible content for users, use CSS or remove the markup — do not rely on comments as a UI feature.

Frequently Asked Questions

No. HTML comments use the <!-- comment text --> syntax. There is no <comment> element in the HTML specification.
No. Browsers ignore comments when rendering. They remain only in the HTML source (View Source or DevTools).
No. Anyone can read comments via View Source or developer tools. Never put passwords, API keys, or private data in comments.
No. The first --> closes the comment. Nested <!-- inside a comment is invalid and breaks the markup.
Only as content between tags — not inside a start tag before attributes. Avoid HTML comments inside title, textarea, script, and style; those treat content as raw text. Prefer // or /* */ inside script and style.
Developers use them to document markup, label sections, and temporarily disable HTML blocks while debugging — without deleting the code.

Next: data

Mark machine-readable values with the data element and value attribute.

data tutorial →

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.

14 people found this page helpful