HTML Comments
HTML Comments
Section titled “HTML Comments”HTML comments <!-- ... --> allow you to add notes within your code that are ignored by the browser.
HTML Comment Tag
Section titled “HTML Comment Tag”You can add comments to your HTML source using the following syntax:
<!-- Write your comments here -->Note: The opening tag starts with <!-- (including the exclamation mark), and the closing tag is -->. Comments cannot be nested. |
|---|
Comments are not displayed by the browser, but they are invaluable for documenting your HTML structure, explaining complex sections, or leaving reminders for yourself or other developers.
Example: Adding notes
<!DOCTYPE html><html><head> <title>My Page</title></head><body>
<h1>Main Content</h1> <!-- TODO: Add introductory paragraph here -->
<section> <h2>About Us</h2> <p>Information about the company.</p> <!-- Remember to update contact details --> </section>
</body></html>Comments are also useful for temporarily disabling sections of code during development or debugging, without deleting them:
<!-- Temporarily hide this section<div class="promo-banner"> <img src="special-offer.png" alt="Special Offer"></div>-->Historical Note: Conditional Comments
Section titled “Historical Note: Conditional Comments”You might encounter syntax like <!--[if IE 9]> ... <![endif]--> in older codebases.
These were ‘conditional comments’, a Microsoft-specific feature used to provide specific HTML or CSS for different versions of Internet Explorer. As Internet Explorer is no longer supported and modern web development relies on feature detection and standardized practices, conditional comments are obsolete and should not be used in new projects.