HTML Comments
Master HTML comments for better code documentation, debugging, and team collaboration. Learn best practices for commenting your HTML code.
How do you write a comment in HTML?
Wrap the comment in <!-- and -->: <!-- This is a comment -->. Comments aren't displayed on the page, but anyone can read them with View Source, so never put passwords or private notes in them. They can span several lines but can't be nested.
<!-- Main navigation -->
<nav>…</nav>
<!--
Temporarily disabled:
<section class="promo">…</section>
-->Try It: Complete HTML Comments Guide
Explore various types of HTML comments used in real-world development. View the page source to see all comments in action.
Code Editor
<!-- This comment is not shown on the page -->
<h1>Comments demo</h1>
<!--
Multi-line comment.
<p>This paragraph is "commented out", so it doesn't render.</p>
-->
<p>Only this paragraph is visible.</p>
<p id="count"></p>Preview
What Are HTML Comments?
HTML comments are invisible notes in your code that help document your work, explain complex sections, and communicate with other developers. They're ignored by browsers but visible in the source code.
Comment Syntax:
<!-- This is a single-line comment -->
<!--
This is a multi-line comment
that can span multiple lines
for detailed explanations
-->Comments Are:
- • Invisible to website visitors
- • Visible in page source code
- • Ignored by browsers
- • Essential for documentation
- • Helpful for debugging
Common Uses:
- • Explaining complex code
- • Organizing code sections
- • Temporary code removal
- • TODO and reminder notes
- • Author and date information
Types of HTML Comments
Single-Line Comments
<!-- Quick note -->Best for brief explanations, quick notes, and simple annotations.
Multi-Line Comments
<!--
Detailed explanation
spanning multiple lines
-->Perfect for detailed documentation, complex explanations, and section headers.
Special Comment Patterns
TODO Comments
<!-- TODO: Add form validation -->
<!-- FIXME: Fix mobile layout -->
<!-- BUG: Form not submitting -->Mark tasks to complete, bugs to fix, and improvements needed.
Section Headers
<!-- ==================
HEADER SECTION
================== -->Organize your code into clear, visually distinct sections.
Documentation
<!-- Author: John Doe -->
<!-- Created: 2024-01-01 -->
<!-- Purpose: User form -->Document authorship, creation dates, and purpose of code sections.
Debugging
<!-- Commented out for testing
<div class="problematic-section">
</div>
-->Temporarily disable code for testing and debugging purposes.
Comment Best Practices
Good Practices:
- Explain the "why," not the "what"
Focus on reasoning behind decisions
- Use consistent formatting
Establish and follow comment conventions
- Keep comments up-to-date
Update comments when code changes
- Use section dividers
Organize code with clear section headers
Avoid These:
- ✗Obvious comments
Don't state what the code clearly shows
- ✗Sensitive information
Never include passwords or API keys
- ✗Outdated comments
Remove or update obsolete information
- ✗Excessive commenting
Too many comments can clutter code
Professional Tips:
Team Collaboration:
- • Include author names and dates
- • Explain complex business logic
- • Document temporary workarounds
Debugging & Maintenance:
- • Mark known issues clearly
- • Document performance considerations
- • Explain browser-specific code
Comments and Performance
While HTML comments are invisible to users, they can affect your website's performance and should be used thoughtfully in production environments.
Performance Impact:
- •Comments increase file size
- •Larger files = slower download times
- •More bandwidth usage
- •Browser still needs to parse them
Production Strategy:
- Use build tools to remove comments
- Keep important legal/copyright comments
- Use minification for production files
- Maintain separate documented versions
Security Considerations:
Remember: HTML comments are visible to anyone who views your page source. Never include sensitive information such as:
- • Passwords or API keys
- • Internal server information
- • Database connection details
- • Confidential business logic
Your Learning Path
Master HTML comments for better code documentation
What You've Learned
- ▶HTML comment syntax and structure
- ▶Single-line and multi-line comments
- ▶Commenting out code for debugging
- ▶Documentation and code organization
- ▶Best practices for professional commenting
What's Next
- ▶Block vs Inline - Element display types
- ▶HTML Attributes - Add functionality
- ▶HTML Tables - Structure data
- ▶HTML Forms - Collect user input
- ▶Semantic HTML - Meaningful markup
Pro Learning Tip
Good comments explain the "why" behind your code, not just the "what". Use them to document complex logic, provide context, and help your future self understand your decisions.
HTML Comments — step by step
Write an HTML comment
HTML comments start with <!-- and end with -->. Everything between them is ignored by the browser. They are not rendered but are visible in the page source.
🎯 Quick Check
Are HTML comments visible to users viewing the page in a browser?
HTML Comments: summary
Key points
- In VS Code, Ctrl/Cmd + / toggles a comment on the selected lines.
- A comment can't contain
--inside it, and comments can't be nested. - CSS uses
/* */and JavaScript uses//or/* */; HTML comment syntax doesn't work inside<style>or<script>.
Frequently asked questions
›Are HTML comments visible to users?
They aren't rendered, but anyone can see them with View Source or DevTools. Treat them as public.
›Do HTML comments affect SEO or performance?
Search engines ignore them. They add a few bytes to the page, which build tools usually strip out in production.