How to Use Comments in HTML
Working with Text
In this lesson, we will learn how to use the comments in HTML.
What is Comment in HTML
HTML comments are a way to add notes to your code that are not visible on the webpage. These comments can be useful for leaving notes for yourself or other developers who may be working on your code.
To create an HTML comment, you need to use the <!-- and --> tags. Anything between these tags will be treated as a comment and ignored by the browser.
Here's an example of how to use comments in HTML:
Example
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Comments Example</title>
</head>
<body>
<!-- We will learn about html comments.
This is a comment. It won't be visible on the webpage. -->
<h2>HTML Comments</h2>
</body>
</html>
It's important to note that comments can also be used to temporarily remove sections of code without deleting them. This can be useful when debugging or testing your code. Here's an example:
Example
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Comments Example</title>
</head>
<body>
<p>This is 1st paragraph.</p>
<p>This is 2nd paragraph.</p>
<!-- <p>This is 3rd paragraph</p> -->
</body>
</html>
In the above example, we have commented out the 3rd paragraph. It means the browser won't load it, but we can easily uncomment it by removing the <!-- and --> tags when we want to use it again.