How to Comment in HTML

Insert a single-line comment., Create a multiline comment., Use the comment function to quickly disable code., Use the comment function to hide scripts on unsupported browsers.

4 Steps 1 min read Medium

Step-by-Step Guide

  1. Step 1: Insert a single-line comment.

    Comments are designated by the tags <!-- and
    -->.

    You can insert quick comments to remind you what is happening with the code. <html> <head> <title>Comment test</title> </head> <body> <!-- This code makes a paragraph
    --> <p>This is the website</p> </body> </html> Make sure there are no spaces in the comment tags.

    For example < !-- will not activate the comment function.

    Inside the tags, you can have as many spaces as you'd like.
  2. Step 2: Create a multiline comment.

    Your comments can span multiple lines, which is useful for explaining complex code, or for blocking off large pieces of code. <html> <head> <title>Comment test</title> </head> <body> <!-- Your comment can be as long as you need it to be.

    Everything within the comment tags is kept from affecting the code on the page.
    --> <p>This is the website</p> </body> </html>

    If you're trying to track down a bug or want to keep code from running on the page, you can use the comment function to quickly block it off.

    That way, you can easily restore the code by deleting the comment tag. <html> <head> <title>Comment test</title> </head> <body> <p>Check out these images</p> <img src="/images/image1.jpg"> <!-- Hiding this image for now <img src="/images/image2.jpg">
    --> </body> </html>

    If you are programming in JavaScript or VBScript, you can use the comment function to hide the script on browsers that don't support it.

    Insert the comment at the start of the script, and end it with to ensure that the script works on browsers that do support it. <html> <head> <title>VBScript</title> </head> <body> <script language="vbscript" type="text/vbscript"> <!-- document.write("Hello World!") </script> </body> </html> The // in the end tag will prevent the script from executing the comment function if scripts are supported by the browser.
  3. Step 3: Use the comment function to quickly disable code.

  4. Step 4: Use the comment function to hide scripts on unsupported browsers.

Detailed Guide

Comments are designated by the tags <!-- and
-->.

You can insert quick comments to remind you what is happening with the code. <html> <head> <title>Comment test</title> </head> <body> <!-- This code makes a paragraph
--> <p>This is the website</p> </body> </html> Make sure there are no spaces in the comment tags.

For example < !-- will not activate the comment function.

Inside the tags, you can have as many spaces as you'd like.

Your comments can span multiple lines, which is useful for explaining complex code, or for blocking off large pieces of code. <html> <head> <title>Comment test</title> </head> <body> <!-- Your comment can be as long as you need it to be.

Everything within the comment tags is kept from affecting the code on the page.
--> <p>This is the website</p> </body> </html>

If you're trying to track down a bug or want to keep code from running on the page, you can use the comment function to quickly block it off.

That way, you can easily restore the code by deleting the comment tag. <html> <head> <title>Comment test</title> </head> <body> <p>Check out these images</p> <img src="/images/image1.jpg"> <!-- Hiding this image for now <img src="/images/image2.jpg">
--> </body> </html>

If you are programming in JavaScript or VBScript, you can use the comment function to hide the script on browsers that don't support it.

Insert the comment at the start of the script, and end it with to ensure that the script works on browsers that do support it. <html> <head> <title>VBScript</title> </head> <body> <script language="vbscript" type="text/vbscript"> <!-- document.write("Hello World!") </script> </body> </html> The // in the end tag will prevent the script from executing the comment function if scripts are supported by the browser.

About the Author

J

Jeremy Cruz

With a background in lifestyle and practical guides, Jeremy Cruz brings 6 years of hands-on experience to every article. Jeremy believes in making complex topics accessible to everyone.

35 articles
View all articles

Rate This Guide

--
Loading...
5
0
4
0
3
0
2
0
1
0

How helpful was this guide? Click to rate: