Well, how to comment in HTML is a simple question with a simpler answer. As in any other language we can add comments in HTML code.
HTML provides comment tag (<!– ..- ->) for commenting a part of code.
The HTML comment tag makes sure that the comment tag and the part of the code commented are not displayed on browser.
Comments can be useful in two ways:
- When we don’t want to execute a part of code
- When we want to explain a part of code
Let’s understand the use of each way with example.
We Will be looking at:
Example: How to comment in HTML to not execute a part of HTML code
<p> This is a paragraph. </p> <br><!- - This is a comment - - > <p> This is another paragraph </p>
|This is a paragraph. This is another paragraph|
Here we can see that the 2nd line of code having “This is a comment” string is not displayed in the output because it is enclosed by the HTML comment tag (<!—and à)
Example: How to comment in HTML to explain a part of code
<p> This is a paragraph </p> <br><!- - The below code will create a horizontal line/rule on the HTML page to make a segregation of the page sections - - > <hr>
|This is a paragraph—————————————————————-|
Here we can see that in the HTML output the commented part of the HTML code is not displayed. Also, if someone else reads the HTML source code he/she will be able to understand the reason of creating a horizontal rule/line in the HTML page. Hence we have explained our code using comment tag in HTML.
Also note that the comments are in multiple lines, this means that the HTML comment can span multiple lines until the closing comment tag is encountered.
HTML Comment Tag Supporting Browsers: