W3docs

HTML Comments

Comments in HTML are used to indicate sections of a document or insert any notes explaining the code. See examples

HTML comments are used to indicate sections of a document or insert notes explaining the code. They help understand the code and increase its readability. The comment syntax can also be used for excluding code blocks temporarily instead of deleting them.

For defining HTML comments we use the <!-- ... --> syntax. Browsers ignore this syntax and do not show its content to the user.

All browsers support HTML comments and ignore their content.

Tip

HTML comments can be used anywhere in the document except for the <title> tag. They do not work within the <style> tag either, as CSS uses another syntax for comments.

Valid vs Invalid Comments

A comment cannot be inserted into another comment. The double-dash sequence (“--”) may not appear inside a comment unless it is part of the closing sequence (-->). It is also important to ensure that there are no spaces at the start of the comment string.

Multiline Comments

HTML supports both single-line and multi-line comments. We can use multi-line comments with the beginning syntax <!-- and ending syntax -->.

Syntax

Comments are written between <!-- and --> symbols.

Example of an HTML comment:

HTML Comments

<!DOCTYPE html>
<html>
  <head>
    <title>The title of the document.</title>
  </head>
  <body>
    <h1>The main heading</h1>
    <!--  <p>We want to hide this part of the code temporarily. </p> -->
    <a href="https://www.w3docs.com">Homepage</a>  
    <!--  <p>This is a link to the homepage</p> -->
  </body>
</html>

Result

html-comments

Practice

Practice

What is the correct way to comment in HTML?