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 understand for developers, to provide explanations and to temporarily deactivate code.

Web hosting
The hosting your website deserves at an unbeatable price
  • Loading 3x faster for happier customers
  • Rock-solid 99.99% uptime and advanced protection
  • 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 programmers 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, displaying 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 particularly useful when longer explanations 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 comprehensive beginner’s tutorial for HTML.

What are HTML comments used for?

HTML comments are an essential tool in web development. They have several important functions:

  • Documentation: Comments help to document the code so that other developers (or your future self) can understand the purpose and functionality of certain sections of code more easily.
  • Deactivate code: Programmers can temporarily deactivate parts of the code or HTML tags without having to delete them. This is particularly useful when debugging or experimenting.
  • Organisation: Large HTML documents can be structured and organised by using HTML comments. This makes it easier to navigate and edit the code.
  • Annotations: Developers 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 developers 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 demonstrate:

<!--
    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 readability. Long comments can make the code harder to follow, so avoid adding unnecessary comments. Ideally, the code should explain itself. Comments are valuable, but they shouldn’t be used as a substitute for writing clean, well-structured code.

Register your domain name
Launch your business on the right domain
  • Free WordPress with .co.uk
  • Free website protection with one Wildcard SSL
  • Free Domain Connect for easy DNS setup
Was this article helpful?
Go to Main Menu