HTML Comments are text in an HTML document that is not displayed by the browser. They allow you to leave notes and explanations directly in the code, which can be especially helpful during the development process or when you need to make future updates.
To add a comment in your HTML code, you enclose the text you want to hide from display within <!-- and -->
Here’s what it looks like:
HTML Comment – Example:
HTML
<!-- This is a comment and will not be displayed on the webpage -->
<p>This is visible text.</p>
In this example:
- The text within the
<!-- and --> tags will not appear on the webpage. - These comments can include reminders, warnings, or explanations about the code, which can be useful for anyone reading or editing the HTML document.
Different Ways to Add Comments in HTML
There are two main ways to write comments in HTML: single-line and multi-line comments. Both use the same basic syntax but differ in how they are implemented
Comment
| Descriptions
| Syntax
|
|---|
Single-line
| The single-line comment is given inside the ( <!-- comment --> ) tag.
| <!-- comment -->
|
Multi-line
| It follows the syntax of a single-line comment by adding multiple lines in the comment.
| <!-- Multi Line Comment -->
|
Note: The shortcut for adding the comment through the keyboard is by typing Ctrl + / in Windows, and Command + / for Mac users.
Examples of HTML Comments
Example 1: Single-Line and Inline Comments
This example demonstrates how to write single-line comments and inline comments in HTML:
HTML
<!DOCTYPE html>
<html>
<body>
<!--This is heading Tag-->
<h1>GeeksforGeeks</h1>
<!--This is single line comment-->
<h2>This is <!--given for--> single line comment</h2>
</body>
</html>
Output: As you see, None of the comment will render on the browsers.
Example 2: Multi-Line Comments and Hidden Code Sections
The following example shows how to use multi-line comments to add longer explanations and temporarily hide sections of code:
HTML
<!DOCTYPE html>
<html>
<body>
<!-- This is
heading tag -->
<h1>GeeksforGeeks</h1>
<!-- This is
multi-line
comment -->
<h2>This is multi-line comment</h2>
<!-- <button style="font-family: Sans-serif;">
Click Me
</button> -->
</body>
</html>
Output: None of the comment will render on the browsers.
Uses of HTML Comments:
- Code Organization: Comments can help break up sections of code, making it easier to navigate, especially in larger projects.
- Collaboration: When working with others, comments are crucial for explaining why certain HTML elements are used, or for leaving instructions for team members.
- Debugging: Temporarily disabling parts of HTML code by commenting them out is a common use, allowing developers to isolate issues.
- Documentation: Providing details or documentation within the code for future reference without the need for external documentation.
Best Practices for Writing Useful and Clear Comments
- Be Concise and Relevant: Write short, meaningful comments that explain the why behind the code, not the obvious what.
Example:<!-- Adjusting margin for better alignment on mobile screens -->
- Avoid Overcommenting: Don’t comment on everything. Focus on complex or non-intuitive sections of the code.
Bad:<!-- This is a paragraph -->
<p>Welcome!</p>
- Keep Comments Updated: Regularly revise comments to ensure they stay accurate as the code evolves.
- Use Consistent Style: Maintain a uniform tone and format for comments across the project to ensure clarity.
Example:<!-- Section: Navigation Bar -->
- Avoid Sensitive Information: Never include passwords, keys, or sensitive data in comments
HTML Comments – FAQs
How to write comments in HTML?
In HTML, comments are written using <!-- ... -->. They allow you to add notes or explanations within the code that are not displayed in the browser.
What are conditional comments in HTML?
Conditional comments in HTML are used to apply specific HTML or CSS for Internet Explorer versions 10 and below, allowing developers to address browser compatibility issues.
What are the three types of comments in HTML?
- Single-line comment.
- Inline comment.
- Multi-lines comment.
Why are HTML comments important?
HTML comments are crucial for code documentation, temporarily disabling code, providing instructions, ensuring cross-browser compatibility, and improving readability, aiding in collaboration, debugging, and maintaining clean, understandable code.
Can HTML comments be nested?
No, HTML comments cannot be nested. If you try to nest comments like <!– <!– Nested Comment –> –>, it can lead to unintended behavior.
How do you comment out a large block of HTML code?
To comment out a large block of code, wrap the entire section with <!– and –>. This is useful for temporarily disabling sections of code.