HTML comments are an essential part of web development, allowing developers to add notes, explanations, or disable code without deleting it.
Comments do not affect the rendered page and are ignored by browsers, but they help in code readability, debugging, and collaboration.
In this tutorial, we will cover:
- Basic Syntax of HTML Comments
- Uses of HTML Comments
- Commenting Out Code
- Multiline Comments
- Best Practices for HTML Comments
- Using HTML Comments for Debugging
- Hiding JavaScript and CSS in Old Browsers
- Comments in Other Web Technologies
1. Basic Syntax of HTML Comments
An HTML comment is written inside <!– and –>.
Example: Basic Comment
<!-- This is a comment -->
This will not be displayed in the browser but remains visible in the source code.
Example: Commenting Inside HTML Code
<p>This is visible.</p>
<!-- <p>This is hidden.</p> -->
Only “This is visible.” will appear on the webpage.
2. Uses of HTML Comments
Comments are useful in several ways:
- Adding Explanations – Helps developers understand code structure.
- Temporarily Disabling Code – Allows quick removal of elements without deleting them.
- Debugging – Helps in testing different parts of a webpage.
- Collaboration – Assists teams by providing contextual information.
- Organizing Code – Makes large codebases easier to navigate.
3. Commenting Out Code
HTML comments can be used to disable certain elements while keeping them in the source code.
Example: Disabling a Section
<!--
<h1>Welcome to My Website</h1>
<p>This section is temporarily hidden.</p>
-->
The h1 and p elements will not be displayed on the webpage.
4. Multiline Comments
HTML comments can span multiple lines.
Example: Multiline Comment
<!--
This is a multiline comment.
It can span multiple lines to describe a section in detail.
-->
Example: Commenting Out Multiple Elements
<!--
<nav>
<ul>
<li>Home</li>
<li>About</li>
<li>Contact</li>
</ul>
</nav>
-->
The entire navigation section will be hidden from display but remains in the source code.
5. Best Practices for HTML Comments
1. Keep Comments Concise
Comments should be short and to the point.
<!-- Main content section -->
<section>
<p>Content goes here.</p>
</section>
Avoid unnecessary comments:
<!-- This is a section element -->
<section>
<p>Content goes here.</p>
</section>
If the tag name itself is self-explanatory, avoid redundant comments.
2. Use Comments to Separate Sections
For large pages, use comments to divide different sections.
<!-- Header Section -->
<header>...</header>
<!-- Main Content -->
<main>...</main>
<!-- Footer Section -->
<footer>...</footer>
3. Do Not Overuse Comments
Too many comments can make the code cluttered.
Bad Practice:
<!-- This is a div -->
<div>
<!-- This is a paragraph -->
<p>Some text</p>
</div>
Good Practice:
<!-- Hero Section -->
<div>
<p>Some text</p>
</div>
6. Using HTML Comments for Debugging
When testing a webpage, temporarily removing code helps in debugging issues.
Example: Debugging a Button
<!-- Temporarily disabling the button -->
<!-- <button>Click Me</button> -->
If the button is causing layout issues, commenting it out can help identify the problem.
7. Hiding JavaScript and CSS in Old Browsers
Older browsers may not support JavaScript or CSS properly. HTML comments were historically used to hide JavaScript or CSS from those browsers.
Example: Hiding JavaScript in Older Browsers
<script>
<!--
document.write("This is JavaScript code.");
-->
</script>
Modern browsers ignore this since they properly recognize <script> tags.
Example: Hiding CSS in Old Browsers
<style>
<!--
body {
background-color: lightgray;
}
-->
</style>
Again, modern browsers do not require this.
8. Comments in Other Web Technologies
1. CSS Comments
CSS uses /* */ for comments.
/* This is a CSS comment */
body {
background-color: white;
}
2. JavaScript Comments
JavaScript supports:
- Single-line comments: //
- Multiline comments: /* */
// This is a single-line comment
console.log("Hello, World!");
/*
This is a multiline comment.
It spans multiple lines.
*/
console.log("Goodbye, World!");
3. XML Comments
Similar to HTML, XML comments use <!– –>.
<!-- This is an XML comment -->
<note>
<to>User</to>
<from>Admin</from>
</note>
HTML Comments – A Guide
HTML comments are an essential part of web development, allowing developers to add notes, explanations, or disable code without deleting it.
Comments do not affect the rendered page and are ignored by browsers, but they help in code readability, debugging, and collaboration.
In this tutorial, we will cover:
1. Basic Syntax of HTML Comments
An HTML comment is written inside <!– and –>.
Example: Basic Comment
This will not be displayed in the browser but remains visible in the source code.
Example: Commenting Inside HTML Code
Only “This is visible.” will appear on the webpage.
2. Uses of HTML Comments
Comments are useful in several ways:
3. Commenting Out Code
HTML comments can be used to disable certain elements while keeping them in the source code.
Example: Disabling a Section
The h1 and p elements will not be displayed on the webpage.
4. Multiline Comments
HTML comments can span multiple lines.
Example: Multiline Comment
Example: Commenting Out Multiple Elements
The entire navigation section will be hidden from display but remains in the source code.
5. Best Practices for HTML Comments
1. Keep Comments Concise
Comments should be short and to the point.
Avoid unnecessary comments:
If the tag name itself is self-explanatory, avoid redundant comments.
2. Use Comments to Separate Sections
For large pages, use comments to divide different sections.
3. Do Not Overuse Comments
Too many comments can make the code cluttered.
Bad Practice:
Good Practice:
6. Using HTML Comments for Debugging
When testing a webpage, temporarily removing code helps in debugging issues.
Example: Debugging a Button
If the button is causing layout issues, commenting it out can help identify the problem.
7. Hiding JavaScript and CSS in Old Browsers
Older browsers may not support JavaScript or CSS properly. HTML comments were historically used to hide JavaScript or CSS from those browsers.
Example: Hiding JavaScript in Older Browsers
Modern browsers ignore this since they properly recognize <script> tags.
Example: Hiding CSS in Old Browsers
Again, modern browsers do not require this.
8. Comments in Other Web Technologies
1. CSS Comments
CSS uses /* */ for comments.
2. JavaScript Comments
JavaScript supports:
3. XML Comments
Similar to HTML, XML comments use <!– –>.
Related Articles
HTML Paragraphs – A Guide
HTML Favicon : A Tutorial