HTML / HTML Tags

HTML Comment Tag

Dec 12, 2022
2 min read
HTML Comment Tag

Definition

A <!-- … --> Tag, otherwise known as the comment tag, is used to insert comments in your code. Comments inserted this way are hidden, and do not show to your users.

Example

<!DOCTYPE html>
<html>
<head>
    <title>HTML Title Element</title>
</head>
<body>
    <h1>Displayed Page Title</h1>
    <p>The main content of your page goes here.</p>
<!-- This is a comment that is only visible in the source code. -->
</body>
</html>

Usage

  • The comment tag is used to add comments to your code, such as explanations or adding TODO items. This helps you understand your code at a later date.
  • Comments do not nest, so you cannot put a comment inside another comment.

Attributes

The Anchor tag does not support any attributes, including the Global Attributes and Event Attributes.

Best Practices

  • Single-line Comments should be on one line with the text inside surrounded by spaces.
    <!-- This is a single line comment -->
  • Multi-line Comments should start and end on their own line. The comment text should not be indented..
    <!--
    This is a multi-line
    comment.
    -->
  • Closing Tag Comments are used to mark the closing of a tag. They should include the class or ID name.
    <div id="section-wrapper">
    	...
    </div><!-- #section-wrapper -->
  • Sensitive Information should not be inserted into comments as it is visible to anyone who inspects the source code.
    <!-- Authenticate with password="1234" -->

Browser Support

Desktop

ChromeEdgeFirefoxIEOperaSafari
YesYesYesYesYesYes

Mobile

Android WebviewChrome AndroidFirefox AndroidOpera AndroidiOS SafariSamsung Internet
YesYesYesYesYesYes