HTML comments are text sections within HTML documents that are ignored by the browser and not displayed on the website. They are used to make the code easier to un­der­stand for de­vel­op­ers, to provide ex­pla­na­tions and to tem­porar­i­ly de­ac­ti­vate code.

Web Hosting
Hosting that scales with your ambitions
  • Stay online with 99.99% uptime and robust security
  • Add per­for­mance with a click as traffic grows
  • Includes free domain, SSL, email, and 24/7 support

What are HTML comments and how can they be added?

An HTML comment is a special markup that allows pro­gram­mers to leave notes in the code. These notes are not rendered by web browsers and are therefore invisible to end users. To create a comment in HTML, use the syntax <!-- comment content -->. Below you will find a simple example that shows how to insert a comment into an HTML document:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Example page</title>
</head>
<body>
    <!-- This is a comment. It will not be displayed in the browser -->
    <h1>Hello World!</h1>
    <!-- <p>This text is not displayed in the browser.</p> -->
</body>
</html>
HTML

In the example above, the browser ignores both the first comment and the comment that contains the HTML paragraph, dis­play­ing only the text in the HTML heading.

Note that there is no special syntax for multi-line comments in HTML. You can simply use multiple lines within a single comment. This is par­tic­u­lar­ly useful when longer ex­pla­na­tions or notes are required. Multi-line comments start and end like single-line comments, and the text in between can be any number of lines.

Note

Comments are one of the first things you will come across when learning HTML. You can find out what else you need to pay attention to if you want to program websites with HTML in our com­pre­hen­sive beginner’s tutorial for HTML.

What are HTML comments used for?

HTML comments are an essential tool in web de­vel­op­ment. They have several important functions:

  • Doc­u­men­ta­tion: Comments help to document the code so that other de­vel­op­ers (or your future self) can un­der­stand the purpose and func­tion­al­i­ty of certain sections of code more easily.
  • De­ac­ti­vate code: Pro­gram­mers can tem­porar­i­ly de­ac­ti­vate parts of the code or HTML tags without having to delete them. This is par­tic­u­lar­ly useful when debugging or ex­per­i­ment­ing.
  • Or­ga­ni­za­tion: Large HTML documents can be struc­tured and organized by using HTML comments. This makes it easier to navigate and edit the code.
  • An­no­ta­tions: De­vel­op­ers can leave personal notes in the code if required.

What to keep in mind when using HTML comments

When working with HTML comments, there are a few important points de­vel­op­ers should note. Most notably, nested comments are not permitted. This means a comment cannot be placed inside another, as it would result in an error. Here’s an example to demon­strate:

<!--
    This is a comment.
    <!-- This is a nested comment and leads to an error. -->
-->
HTML

When writing comments, it’s essential to follow best practices. Comments should be clear and to the point to enhance code read­abil­i­ty. Long comments can make the code harder to follow, so avoid adding un­nec­es­sary comments. Ideally, the code should explain itself. Comments are valuable, but they shouldn’t be used as a sub­sti­tute for writing clean, well-struc­tured code.

Domain Name Reg­is­tra­tion
Build your brand on a great domain
  • Free Wildcard SSL for safer data transfers 
  • Free private reg­is­tra­tion for more privacy
  • Free Domain Connect for easy DNS setup
Go to Main Menu