To add a comment into an HTML document, there’s a special tag <!-- comment -->
.
There are times when you need to add comments to an HTML page. Comments are text that will be visible to your colleagues, but not visible to users who browse your website.
Sometimes you just want to leave yourself a reminder of what needs to be done in the future.
No special tags are needed for HTML comments. However, you have to mark them in a way so that it is clear where the beginning of the comment is and where it ends.
Single line comments in HTML
The simplest comment consists of one line. The text must be placed between the sequence of characters <!--
and -->
:
<!-- This is the first comment, it won't be shown on a page -->
<h1>And this is a header. It will be fully visible.</h1>
<!-- This is a second comment, it's invisible too -->
Take a note, that the exclamation mark is only placed in the beginning, but not the end of the comment.
Multiline HTML comments
If you need to add a long comment, that consists of multiple strings, it’s also possible in HTML.
If your comment does not fit on one line, then it can be made multi-line to make it easier to read. Unlike JavaScript, you don’t need new character combinations. Use the same <!--
and -->
, but add line breaks where needed:
<!--
Multiline comment.
You can place a lot of information there.
Maybe even a whole book.
-->
If some HTML tags are placed inside the comment, they disappear from the page.
<!--
<h1>Commented header</h1>
<p>This paragraph won't be shown as well</p>
-->
Errors
The mistakes in the HTML comments are most often related to an extra space or to a missing exclamation mark:
< !-- Invalid comment #1 -->
<-- Invalid comment #2 -->
Both comments are incorrect and will appear as plain text on the HTML page.