HTML Comments

HTML comments help you leave notes in your code. They are not displayed in the browser UI, but they are visible in the page source.

1) Basic Syntax


<!-- This is a comment -->
<p>Visible content</p>

Rendered output shows only: Visible content

2) Comment Out Code (Disable Temporarily)


<!-- <h2>This heading is hidden</h2> -->
<p>This paragraph is visible</p>

3) Don’t Split Tags with Comments


<!-- Correct -->
<a href="page.html">Read more</a>

<!-- Avoid putting a comment between tag name and > or attributes -->
<!-- Wrong: <a <!-- note --> href="page.html">... -->

4) Multi-line Notes / TODOs


<!--
 TODO: Replace placeholder image
 NOTE: Keep this section short
-->

5) Conditional Comments (Legacy IE Only)


<!--[if lt IE 9]>
  <p>You are using an old version of Internet Explorer.</p>
<![endif]-->

These worked only in very old Internet Explorer versions. Modern browsers ignore them.

6) Comments are Not Secure


<!-- API key goes here (DON'T DO THIS) -->

Never put secrets in comments. Anyone can view source and read them.

Best Practices:

  • Use comments to explain why (not obvious what).
  • Keep comments short, clear, and up-to-date.
  • Remove temporary or debug comments before publishing.
  • Remember: comments increase HTML size—don’t overuse.