HTML Comments


🔹 What are HTML Comments?

HTML comments are lines in your HTML code that are not displayed in the browser. They are used to:

  • Explain the code

  • Leave notes for yourself or other developers

  • Temporarily disable parts of code during testing


🔹 Syntax of a Comment

<!-- This is a comment -->
💡 Comments can be added anywhere in the HTML document.

🔹 Example: Basic Comment

<!-- This is a heading section -->
<h1>Welcome to My Website</h1>

🔹 Commenting Out Code

You can use comments to disable code without deleting it.

<!-- <p>This paragraph will not be shown.</p> -->

🔹 Multiline Comment

<!--
This is a multiline comment.
It will not be displayed in the browser.
Use it to explain complex code.
-->

🔹 Where Can You Use Comments?

✅ Inside <body> or <head>
✅ Before or after elements
✅ Inside complex blocks of HTML code


🔹 Why Use Comments?

  • To help others understand your code

  • To test code by disabling specific sections

  • To organize code with section labels


🛑 Best Practices

  • Don’t overuse comments — write clean code instead

  • Keep comments short and meaningful

  • Avoid leaving unnecessary commented-out code in final version


Practice Questions

Q1. Add a comment saying “Header section” before an <h1> tag.

Q2. Write a comment above a paragraph describing the content.

Q3. Disable a <div> element using a comment.

Q4. Write a multiline comment explaining your HTML layout.

Q5. Add a comment to separate the header and footer sections.

Q6. Use a comment to explain a link’s purpose.

Q7. Comment out an image tag and check it disappears.

Q8. Place a comment inside the <head> tag.

Q9. Use comments to label different sections of a web page.

Q10. Write a comment inside a form describing each input field.


Go Back Top