Comments in HTML
Overview
In any programming language, the programmers have the functionality to write the comments to understand the code easily. Similarly, in HTML, the comments can be written to read and understand the complex code. These comments are ignored by the browser while rendering the web page. Also, adding comments to HTML code or any other programming language improves the readability of the code.
Introduction to HTML Comments
When the code is compiled/rendered by the browser, the browser ignores the HTML comments. Then, why should one write comments in HTML?
So if you are working on a big project that includes complex code, you may easily lose track of and purpose of the code you write. In addition to that, if you are working with the team, other developers may not understand your code, and in that instance, the comments come in handy and play a vital role in understanding the piece of code.
The comments in the HTML document are enclosed within special brackets and are not displayed on the webpage, but they will appear in our source code. Now, you must be wondering how to write the comments in HTML. Well, comments in HTML can be written like this:
In the above example, the <!-- --> is an HTML comment tag.
As you can see in the above example, the HTML code contains two lines of comments that states the meaning of the code. This comment is not visible on the browser and hence will not affect the view of the web page, as we can see in the output as well.
To summarise, we can say that to add anything as a comment in HTML; we need to insert it between <!-- and -->.
Types of Comments in HTML
There are three ways to write the comments in HTML.
- Single-line comment
- Multi-lines comment
- <comment> tag
Single-line Comments in HTML
The single-line comment means the comment's text will appear on only one line. These comments are brief and can summarise the code in a single line; hence, the name single-line comments. The examples shown in the previous section are of the single-line comments.
Let's take one more example to understand what are the single-line comments.
Multi-line Comments in HTML
The multi-line comments mean the text or the comment's information will appear in more than one line. That means some parts of the code might be so complex that you'll have to write the comments using multi-line comments.
The syntax of the multi-line comments is the same as the single-line comment; the only difference is, if you want to write a long comment, then it will be on multiple lines.
Let's take an example to understand the multi-line comments better.
Comment tag
In the earlier days, there was a tag specially for adding comments in HTML called the Comment tag. The comment tag is denoted as <comment>.
However, this tag is not supported by modern age browsers; hence you should avoid using the comment tag in HTML code.
Let's take an example of a comment tag.
Browser Support
All modern browsers support the <!-- --> tag.
We have enlisted the supported browsers below.
- Chrome (1+)
- Edge (All)
- Firefox (1+)
- IE (All)
- Opera (All)
- Safari (All)
The <comment> tag is deprecated and not supported by modern-age browsers. Always use the <!-- --> to include a comment in the HTML code.
Summary
- HTML comments make your complex code more readable and more understandable and help other developers to understand the intent behind the code.
- We learned how to write the HTML comments.
- There are mainly two ways to write the comments in HTML code supported by modern age browsers, i.e., single-line comments and multi-line comments.