HTML Comments

HTML Comments

HTML comments are used to add notes or explanations in your code. Comments are not displayed in the browser and are ignored by It.


Syntax of HTML Comment

<!-- This is a comment -->

Single-Line Comment

<!-- This is a single-line comment -->
<p>This text is visible.</p>

Multi-Line Comment

<!--
This is a multi-line comment.
It can span multiple lines.
-->

<p>Hello World</p>

Commenting Out HTML Code

You can disable code temporarily using comments.

<!-- <p>This paragraph will not appear</p> -->

Comments Inside Structure

<!DOCTYPE html>
<html>
<head>
<!-- Page title -->
<title>HTML Comments</title>
</head>
<body>
<!-- Main heading -->
<h1>Welcome</h1>
</body>
</html>

Incorrect Usage (Avoid ❌)

<!-- This is wrong -- This is also wrong -->
<! -- Invalid comment -->

Important Points to Remember

  • Comments start with <!-- and end with -->

  • Do not nest comments

  • Comments are visible in page source

  • Useful for debugging and documentation


Best Practice Tips ✅

✔ Use comments to explain complex code
✔ Remove unnecessary comments in production
❌ Never write sensitive information in comments

You may also like...