How to Comment Multiple Lines of HTML Code

Shubham Vora Feb 02, 2024
  1. Use the <!- --> Tag to Comment Out Multiple Lines of HTML Code
  2. Use the Keyboard Shortcut to Comment Out Multiple Lines of HTML Code
How to Comment Multiple Lines of HTML Code

In this article, users will learn to add comments on multiple lines of HTML code in one shot. If we define the HTML comments in simple terms, they are pieces of code or statements that are not rendered or displayed by the browser.

There can be two scenarios where we need to comment on our code. The first scenario is to hide some HTML elements from rendering into the browser, and another scenario is to explain our code to our fellow developers.

Users can think about the web pages containing thousands of lines of code without comment or explanation and what happens if any new developer sees that code. Will they understand? Nope!

So, there is the need to comment out the single line or multiple lines in HTML code.

Use the <!- --> Tag to Comment Out Multiple Lines of HTML Code

In the previous decades, HTML contained the <comment> tag to comment out the code, but currently, it is deprecated.

Nowadays, <!-- –> works as a comment tag and is supported by all modern browsers. It contains mainly 3 parts.

The first part is the <!--. We have to put it before the first line from where we have to start the comment.

Here, the noticeable thing is the exclamatory mark. Users should not forget to add it.

The second part contains the comment content. It can be anything like code explanation or HTML tags, which we must hide from rendering in the browser.

The third part is the --> to close the comment, which we have to put after the last line of the comment.

Example Code:

<html>
    <head>
        <title>Multiple line comment Example.</title>
    </head>
    <body>
        <!--   This is the code explanation comment.
        <p>This will not be shown on the browser.</p>
        -->
        <p>Code to render.<p>
    </body>
</html>

In the above example, we have added comments to 2 lines of HTML code.

The first line explains the code, and the second line contains the <p> tag. So, we have also learned to comment out the HTML tags by giving an example.

Use the Keyboard Shortcut to Comment Out Multiple Lines of HTML Code

Users can use the keyboard shortcut to comment out multiple lines of HTML code.

To do it, users must select all the lines of HTML code they want to comment out. After that, if you are a macOS user, press the Command+/ keys together; otherwise, press ctrl+/, if you are a Windows or Linux user.

However, this method will also put the multiple lines which need to be commented between the <!-- --> tag.

Also, users can use the <!-- –> to comment on a single line of HTML code.

Author: Shubham Vora
Shubham Vora avatar Shubham Vora avatar

Shubham is a software developer interested in learning and writing about various technologies. He loves to help people by sharing vast knowledge about modern technologies via different platforms such as the DelftStack.com website.

LinkedIn GitHub

Related Article - HTML Comment