HTML Comments
HTML comments are not displayed in the browser, but they can help document your HTML source code.
How to Add Comments in HTML
You can add comments to your HTML source by using the following syntax:
html
<!-- Write your comments here -->Notice that there is an exclamation point (!) in the start tag, but not in the end tag.
Example: Single-Line Comment
html
<!-- This is a single-line comment -->
<p>This is a paragraph.</p>Why Use Comments?
With comments you can place notifications and reminders in your HTML code:
- Explain your code: This can be helpful if you edit the source code at a later date.
- Leave notes for others: If you work in a team, comments can help other developers understand your code.
- Temporarily hide content: You can "comment out" parts of your code to hide them from the browser without deleting them. This is useful for debugging.
Multi-line Comments
Comments can also span multiple lines.
Example: Multi-Line Comment
html
<!--
This is a multi-line comment.
It can span across several lines.
-->
<p>This is another paragraph.</p>Conditional Comments
Conditional comments were a feature specific to Internet Explorer. While largely obsolete now, you might still encounter them in older codebases. They were used to provide specific instructions or styles for different versions of IE.
Example: Conditional Comment
html
<!--[if IE 9]>
<p>This content is only displayed in Internet Explorer 9.</p>
<![endif]-->Test Yourself with an Exercise
What is the correct syntax for an HTML comment?