08

HTML – Comments

Chapter 8 • Beginner

12 min

Comments let you write notes in your HTML code that are not shown in the browser. They are only visible in the source code.

Comment syntax

HTML comment:

<!-- This is a comment -->

Anything between <!-- and --> is treated as a comment.

Why comments are useful

  • Explain complex parts of the code
  • Leave reminders for yourself or your team
  • Temporarily hide code without deleting it
  • Mark different sections in long files

Examples of using comments

  1. Explaining a section:

<!-- Main navigation menu starts here -->

  1. Temporarily disabling code:

<!-- <p>This paragraph is disabled for now</p> -->

  1. Making TODO notes:

<!-- TODO: Add a link to the contact page here -->

Valid vs invalid comments

Valid comment:

  • <!-- This is a valid comment -->

Invalid comment examples:

  • <!— wrong dash characters —>
  • <-- missing a dash -->
  • <!-- unclosed comment

Invalid comments can cause strange rendering issues and may break parts of the page, so always close comments properly.

Suggested image prompt

"Screenshot of HTML code in an editor with comments highlighted, and the resulting browser preview showing that comments are not visible."

Interview-style questions from this topic

  1. How do you write a comment in HTML?
  2. Why are comments useful in a team project?
  3. Can the user see comments on the actual web page?
  4. What happens if you forget to close a comment?
  5. How can you use comments to temporarily hide a part of the page?

Hands-on Examples

HTML Comments Example

<!DOCTYPE html>
<html>
<head>
  <title>Comments Example</title>
  <!-- This is a comment in the head section -->
</head>
<body>
  <!-- This is a comment in the body section -->
  <h1>This is a heading</h1>
  <!-- 
    This is a multiline comment
    that spans multiple lines
  -->
  <p>This is a paragraph.</p>
  <!-- <p>This paragraph is commented out and will not appear.</p> -->
</body>
</html>

This example shows single-line and multi-line comments. Notice that the commented-out paragraph does not appear in the browser at all. Comments are ignored by the browser, but they are very helpful for developers reading the code.