0
0
HtmlHow-ToBeginner · 3 min read

How to Write Comments in HTML: Syntax and Examples

In HTML, you write a comment by enclosing your text between <!-- and -->. Anything inside these tags will not show on the webpage but helps explain the code for developers.
📐

Syntax

HTML comments start with <!-- and end with -->. Anything between these markers is ignored by the browser and not displayed on the page.

This is useful for adding notes or temporarily disabling code without deleting it.

html
<!-- This is a comment in HTML -->
💻

Example

This example shows a comment inside an HTML document. The comment will not appear on the webpage, but the heading will.

html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Comment Example</title>
</head>
<body>
  <!-- This is a comment and will not be visible on the page -->
  <h1>Welcome to my website</h1>
</body>
</html>
Output
Welcome to my website
⚠️

Common Pitfalls

  • Do not nest comments inside each other; HTML does not support nested comments.
  • Make sure the comment starts with <!-- and ends with --> exactly; missing dashes or angle brackets will break the comment.
  • Comments cannot contain -- inside them because it closes the comment early.
html
<!-- Wrong comment -- with extra dashes -->
<!-- Correct comment -->
📊

Quick Reference

UsageSyntax Example
Basic comment<!-- This is a comment -->
Comment out code<!-- <p>Hidden paragraph</p> -->
Avoid nested commentsNot supported in HTML

Key Takeaways

Use to write comments in HTML.
Comments are invisible on the webpage but visible in the code.
Do not nest comments or use double dashes inside comments.
Comments help explain code or temporarily disable parts without deleting.
Always close comments properly to avoid breaking your HTML.