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­velopers, to provide ex­plan­a­tions and to tem­por­ar­ily de­ac­tiv­ate code.

Web hosting
The hosting your website deserves at an un­beat­able price
  • Loading 3x faster for happier customers
  • Rock-solid 99.99% uptime and advanced pro­tec­tion
  • Only at IONOS: up to 500 GB included

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­larly useful when longer ex­plan­a­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­hens­ive 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­ment­a­tion: Comments help to document the code so that other de­velopers (or your future self) can un­der­stand the purpose and func­tion­al­ity of certain sections of code more easily.
  • De­ac­tiv­ate code: Pro­gram­mers can tem­por­ar­ily de­ac­tiv­ate parts of the code or HTML tags without having to delete them. This is par­tic­u­larly useful when debugging or ex­per­i­ment­ing.
  • Or­gan­isa­tion: Large HTML documents can be struc­tured and organised by using HTML comments. This makes it easier to navigate and edit the code.
  • An­nota­tions: De­velopers 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­velopers 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­ab­il­ity. Long comments can make the code harder to follow, so avoid adding un­ne­ces­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.

Register your domain name
Launch your business on the right domain
  • Free WordPress with .co.uk
  • Free website pro­tec­tion with one Wildcard SSL
  • Free Domain Connect for easy DNS setup
Go to Main Menu