A web page has an HTML source code and the HTML comment tag is brought to use when there is a need to add comments within the HTML code. Also, you can see the comments only in the source code and not on the webpage. Comments are used to explain the actions of a code; thus making it easier to modify the source code later.
In the world of programming, usually, a comment is regarded as a readable piece of code that is mainly an explanation in the source code. This will make the source code easier to understand, and can be used for future integrations and upgrades.
How to Comment in Html
In this article, we will discuss how you can add comments in HTML code easily. But before that, let’s take a look at why there’s a need to add comments in the first place.
Why Do We Use Comments in HTML?
A comment is a piece of text within the HTML code that is not included in parsing but is used to provide information about a certain portion of the code. Almost all the programming languages allow coders to add comments to share or highlight some specific information. Comments make the code easy to understand and help others to become familiar with the functionality that is applied through the code.
Let’s say someone does not know why a particular code was used, but there are comments added to explain the logic or the reason why the code exists and what it does. This will make it quite easy to understand even complex and lengthy code. Comments are also important in terms of version control and tracking enhancements made in the code.
Commenting on code is also used during site testing and troubleshooting. For example, let us say there is a bug or issue; you can add remarks on a certain section of code and then fix that later. You can write comments in HTML using the following syntax:
<!– and –>
The comments added in HTML documents are mostly meant for the coders and developers. Also, comments can be used to increase the readability of the code. The comments are usually added between <! — and –> tags. So any text between these will be considered as a comment.
Syntax
<! — Write your comment here –>
Example:
<!DOCTYPE html>
<html>
<!– This is Header section –>
<head>
<!– Internal CSS –>
<style>
}
</style>
</head>
<!– This is body section, write code here which you want to display on web-page –>
<body>
<!– heading tag –>
<h2>First WebPage</h2>
<!– Paragraph tag –>
<p>Write your Content here!!!</p>
</body>
</html>
Multiline Comments
There can be multiline comments that have more than one line of comment.
Syntax
<!—
This is a sample comment.
This is another comment.
–>
Example:
<h2>Cake Gallery</h2>
<!– This is image for a yummy cake
It becomes visible on the web page.
of your favorite browser –>
<img src=”https://static.javatpoint.com/htmlpages/images/cake.png” alt=”cake image” height=”300px”
width=”300px”>
Conditional Comments
Conditional comments are a bunch of conditional statements, which are interpreted in HTML source code by Microsoft Internet Explorer versions 5 through 9. You can use these comments to write or hide code on these versions of Internet Explorer. (Conditional comments are not available on Internet Explorer version 10 and 11.)
Example:
<!DOCTYPE html>
<html>
<body>
<!–[if IE 9]>
<h1>FreeCodeCamp web</h1>
<p>FreeCodeCamp is an open-source project that needs your help</p>
<![endif]–>
</body>
</html>
Why Should You Use Comments on Python?
Comments give you an idea of what a particular code does. These comments also serve as a reference. Let’s say that after some years you want to revisit a code and you would like to know what purpose it served in the larger scheme of things, then the comments added to the code will help you easily identify the role of it.
Things to Keep in Mind When Using HTML Comments
- If you’re using a single-line comment, unlike a multi-line comment, it should be written on a single line only and must have spaces within the line. e.g. <!–…–>
- Multiline comments should not be indented.
- Do not add any proprietary information in the comments.
Conclusion
Adding comments in HTML code is basically like leaving notes. Comments can help understand what a particular code is supposed to do in a website or web application. Ideally, comments in HTML code are for developers to understand the code functionality with relative ease. It is advised to add comments wherever necessary, especially if you are creating a web app for a client.