Skip to content

07. HTML Comments

HTML comments allow you to add notes, explanations, and reminders directly in your HTML code without affecting how the page is displayed. Comments are invisible to users viewing the page but remain visible in the source code, making them valuable for documentation, debugging, and code organization.

HTML comments use a special syntax that browsers ignore when rendering the page:

<!-- This is a comment -->

Comments start with <!-- and end with -->. Everything between these markers is treated as a comment and won’t be displayed in the browser.

<!-- This is a single-line comment -->
<p>This paragraph is visible.</p>
<!--
This is a multi-line comment.
It can span multiple lines.
Useful for longer explanations.
-->
<p>This paragraph is visible.</p>
<p>This is visible text <!-- but this comment is not --> and this is visible again.</p>

Explain what code does and why:

<!-- Main navigation menu -->
<nav>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/about">About</a></li>
</ul>
</nav>
<!-- Footer section with copyright information -->
<footer>
<p>&copy; 2024 My Website</p>
</footer>

Organize large HTML documents into logical sections:

<!DOCTYPE html>
<html lang="en">
<head>
<!-- Document metadata -->
<meta charset="UTF-8">
<title>My Page</title>
</head>
<body>
<!-- Header section -->
<header>
<h1>Welcome</h1>
</header>
<!-- Main content area -->
<main>
<article>
<h2>Article Title</h2>
<p>Content here.</p>
</article>
</main>
<!-- Footer section -->
<footer>
<p>Footer content</p>
</footer>
</body>
</html>

Temporarily disable code without deleting it:

<!-- Temporarily hiding this section for testing -->
<!--
<div class="featured-content">
<h2>Featured Article</h2>
<p>This content is currently hidden.</p>
</div>
-->
<!-- Active content -->
<div class="main-content">
<h2>Main Article</h2>
<p>This content is visible.</p>
</div>

Add reminders for future work:

<!-- TODO: Add social media links -->
<!-- FIXME: Update this section with new content -->
<!-- NOTE: This section needs accessibility review -->

Internet Explorer used to support conditional comments (now deprecated):

<!--[if IE]>
<p>You are using Internet Explorer</p>
<![endif]-->

Note: Conditional comments are no longer supported in modern browsers and should not be used.

Write clear, helpful comments:

<!-- Good: Clear and descriptive -->
<!-- Navigation menu with main site links -->
<!-- Avoid: Obvious or unhelpful -->
<!-- Navigation -->

Update comments when code changes:

<!-- Bad: Comment doesn't match code -->
<!-- User login form -->
<form>
<!-- Actually a registration form now -->
</form>
<!-- Good: Accurate comment -->
<!-- User registration form -->
<form>
<!-- Registration form -->
</form>

Avoid commenting obvious code:

<!-- Bad: Commenting the obvious -->
<!-- This is a paragraph -->
<p>Text here</p>
<!-- Good: Comment only when helpful -->
<!-- Introduction paragraph for the article -->
<p>Text here</p>

Help organize large files:

<!-- ============================================
HEADER SECTION
============================================ -->
<header>
<!-- Logo and site title -->
<div class="logo">Logo</div>
<!-- Main navigation -->
<nav>Navigation</nav>
</header>
<!-- ============================================
MAIN CONTENT
============================================ -->
<main>
<!-- Article content -->
</main>

You cannot nest comments inside other comments:

<!-- Outer comment
<!-- Inner comment - THIS WON'T WORK -->
-->

The browser will interpret this incorrectly. The comment ends at the first -->.

Comments cannot be used inside attribute values:

<!-- This won't work as expected -->
<img src="image.jpg" alt="Description <!-- comment -->">

The comment syntax will be treated as part of the attribute value.

Comments inside <script> and <style> tags use different syntax:

<script>
// JavaScript comment (not HTML comment)
/* Multi-line JavaScript comment */
</script>
<style>
/* CSS comment (not HTML comment) */
.class { color: red; }
</style>

Remember that HTML comments are visible in the page source:

<!-- Bad: Don't put sensitive information in comments -->
<!-- Admin password: secret123 -->
<!-- API key: abc123xyz -->
<!-- Good: Keep sensitive info out of comments -->
<!-- User authentication section -->

Anyone can view page source and see your comments. Never include:

  • Passwords
  • API keys
  • Personal information
  • Sensitive business logic

Comments are typically removed during production minification, but don’t rely on this for security.

<!-- Temporarily disabled for testing -->
<!--
<div class="old-feature">
<p>Old content</p>
</div>
-->
<div class="new-feature">
<p>New content</p>
</div>
<!--
<section>
<h2>Section Title</h2>
<p>Section content</p>
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
</section>
-->

Different languages use different comment syntax:

<!-- HTML Comment -->
<script>
// JavaScript single-line comment
/* JavaScript multi-line comment */
</script>
<style>
/* CSS comment */
</style>
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Document metadata -->
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Web Page</title>
<!-- External stylesheet -->
<link rel="stylesheet" href="styles.css">
</head>
<body>
<!-- Site header with navigation -->
<header>
<nav>
<!-- Main navigation links -->
<ul>
<li><a href="/">Home</a></li>
<li><a href="/about">About</a></li>
<li><a href="/contact">Contact</a></li>
</ul>
</nav>
</header>
<!-- Main content area -->
<main>
<article>
<h1>Article Title</h1>
<p>Article content goes here.</p>
</article>
</main>
<!-- Site footer -->
<footer>
<p>&copy; 2024 My Website. All rights reserved.</p>
</footer>
</body>
</html>
  1. Use comments for clarity: Explain complex or non-obvious code
  2. Keep comments current: Update them when code changes
  3. Organize with comments: Use them to structure large files
  4. Avoid sensitive data: Never put passwords or keys in comments
  5. Don’t over-comment: Skip comments for obvious code
  6. Use consistent style: Establish a commenting convention
  7. Remove debug comments: Clean up temporary comments before production