How to Work with Comments in HTML
It's helpful when writing HTML to be able to put text into a document that is just for use by yourself or future editors of the document. Such text is known as a comment. In this how-to, you'll learn how to work with HTML comments.
Comments are generally used for one of three purposes.
- To write helpful notes about the code, for example, why something is written in a specific way.
- To comment out some code that is not currently needed, but may be used sometime in the future.
- To debug a page.
HTML comments are enclosed in <!-- and -->.
For example:
<!-- This is an HTML comment -->
Follow these steps to practice using HTML comments:
- Start with a simple HTML document, like the following:
<!DOCTYPE HTML> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> </head> <body> </body> </html>
- Add a comment to the <body> element.
<!DOCTYPE HTML> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> </head> <body> <!--this is the body of the document--> </body> </html>
- Add two
<p>
elements to the document, and then comment one of them out.
<!DOCTYPE HTML> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> </head> <body> <!--this is the body of the document--> <p>this paragraph is visible</p> <!--<p>this paragraph is commented out--> </body> </html>
- Save the file and open it in a browser. It should look like the following.