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.


HTML Comments Quiz

Q1: What is the correct syntax for an HTML comment?

A. /* comment */
B. // comment
C. <!-- comment -->
D. # comment

Q2: What happens to the code inside a comment?

A. It runs as usual
B. It shows in the browser
C. It is ignored by the browser
D. It becomes hidden text

Q3: Can comments be used to disable parts of HTML code?

A. No
B. Only in JavaScript
C. Yes
D. Only in CSS

Q4: Which of these is a valid multiline comment?

A. <!-- Multiline -->
B. <!-- line1 line2 line3 -->
C. <!-- line1 --> <!-- line2 -->
D. All of the above

Q5: Where can you place comments in HTML?

A. Only at the top
B. Only inside <body>
C. Anywhere in the document
D. Only inside <html>

Q6: Will the following code display the paragraph '<!-- <p>This is hidden</p> -->' ?

A. Yes
B. No
C. Only in some browsers
D. Only when JavaScript is enabled

Q7: Why are comments useful in coding?

A. They reduce loading time
B. They display instructions to users
C. They help organize and explain code
D. They add CSS

Q8: What is a good practice for writing comments?

A. Make them very long
B. Use them for every line
C. Keep them short and clear
D. Avoid using them

Q9: What does the browser do with HTML comments?

A. Executes them
B. Displays them
C. Ignores them
D. Converts to text

Q10: What tag is used to write comments in HTML?

A. <comment>
B. <!-- -->
C. <hide>
D. #

Go Back Top